使用 PyQt6 从 QAction QMenu 中删除箭头

问题描述 投票:0回答:1

目前我正在开发一个文件浏览器并遇到了这个问题; 箭头和小“按钮”表示我一直在尝试:

  1. 完全移除箭头
  2. 将第二个“按钮”与工具栏上的 QAction 连接
  3. QAction 将充当下拉菜单

我将如何尝试这个。到目前为止我已经尝试过:

  1. .setStyleSheet("::menu-arrow { image: none; }")
  2. .setStyleSheet("::icon { 显示:无;}")
  3. .setStyleSheet("::menu-indicator { image: none; }")

QToolBar --> QAction --> QMenu

def setUpMainWindow(self):
    self.main_toolbar = QToolBar()
    self.setUpToolBar()

    self.addToolBar(self.main_toolbar)
    self.addToolBarBreak()
    ...................
    ..............
    .........



def setUpToolBar(self):
    self.main_toolbar.setFixedHeight(45)
    self.main_toolbar.setMovable(False)
    self.main_toolbar.setContextMenuPolicy(Qt.ContextMenuPolicy.DefaultContextMenu)
    self.main_toolbar.setIconSize(QSize(25, 25))
    self.main_toolbar.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)

    t_back_i = QIcon(QPixmap("icons/back.png"))
    self.t_back_act = QAction(t_back_i, "")
    self.t_back_act.setToolTip("Back")
    self.t_back_act.triggered.connect(self.directory_back_process)

    t_fwrd_i = QIcon(QPixmap("icons/forward.png"))
    self.t_frwd_act = QAction(t_fwrd_i, "")
    self.t_frwd_act.setEnabled(False)
    self.t_frwd_act.triggered.connect(self.directory_forward_process)

    t_path_output_i = QIcon(QPixmap("icons/vert-menu.png"))
    self.t_path_output_act = QAction(t_path_output_i, "")
    #self.t_path_output_act.setFixedSize(30,30)
    self.t_path_output_act.setMenu(self.window_context_menu)

    t_srch_i = QIcon(QPixmap("icons/search.png"))
    self.t_srch_act = QAction(t_srch_i, "")

    self.dirPath_h_box = QHBoxLayout()
    self.dirPath_h_box.setContentsMargins(10, 0, 10, 0)
    self.dirPath_h_box.setSpacing(5)
    self.dirPath_h_box.setAlignment(Qt.AlignmentFlag.AlignLeft)




    pathOutput_widget = QWidget()
    pathOutput_widget.setLayout(self.dirPath_h_box)
    self.pathOutput = QScrollArea()
    self.pathOutput.setFixedSize(415,30)
    self.pathOutput.setObjectName("tbFrame")

    self.pathOutput.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
    self.pathOutput.horizontalScrollBar().setStyleSheet("QScrollBar {height:0px}")
    self.pathOutput.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
    self.pathOutput.setWidgetResizable(True)
    self.pathOutput.setWidget(pathOutput_widget)



    spacer_1 = QWidget()
    spacer_1.setFixedWidth(5)
    spacer_2 = QWidget()
    spacer_2.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
    spacer_3 = QWidget()
    spacer_3.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)

    self.main_toolbar.addWidget(spacer_1)
    self.main_toolbar.addAction(self.t_back_act)
    self.main_toolbar.addAction(self.t_frwd_act)
    self.main_toolbar.addWidget(spacer_2)
    self.main_toolbar.addWidget(self.pathOutput)
    self.main_toolbar.addAction(self.t_path_output_act)
    self.main_toolbar.addAction(self.t_srch_act)
    self.main_toolbar.addWidget(spacer_3)


    self.displayPath(QDir.homePath())

def setUpContextMenu(self):
    self.window_context_menu = QMenu()
    self.window_context_menu.setObjectName("idk")
    self.window_context_menu.addAction(self.copy_directory_act)
    #self.window_context_menu.addAction(self.paste_directory_act)
    self.window_context_menu.addAction(self.new_directory_act)
    self.window_context_menu.addAction(self.delete_directory_act)
    #self.window_context_menu.addAction(self.select_all_act)


 def setUpActions(self):
    self.copy_directory_act = QAction("Copy")
    self.copy_directory_act.triggered.connect(self.copy_directory)
    self.paste_directory_act = QAction("Paste")
    #self.paste_directory_act.triggered.connect(self.paste_directory)
    self.new_directory_act = QAction("New Folder")
    self.new_directory_act.triggered.connect(self.new_directory)
    self.delete_directory_act = QAction("Delete")
    self.delete_directory_act.triggered.connect(self.delete_directory)
    self.select_all_act = QAction("Select All")
    #self.select_all_act.triggered.connect(self.select_all)

** 我知道它说的是“setUpContextMenu”,尽管与 QToolbar 的实际上下文菜单无关。

python python-3.x qt pyqt pyqt6
1个回答
0
投票

如果我正确理解您的要求,您需要一个工具栏按钮(没有额外的箭头),它只打开一个菜单,不执行任何其他操作。此功能由 ToolButtonPopupMode.InstantPopup 提供 - 但要激活它,您需要访问为操作创建的 QToolButton 对象。完成后,您还可以在其上设置样式表以删除菜单箭头。

这是一个基本演示:

screenshot

from PyQt6 import QtCore, QtGui, QtWidgets

class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        toolbar = self.addToolBar('Test')
        menu = QtWidgets.QMenu(self)
        menu.addAction('This')
        menu.addAction('That')
        menu.addAction('Thus')
        action = QtGui.QAction(self.style().standardIcon(
            QtWidgets.QStyle.StandardPixmap.SP_DirOpenIcon), 'Open', self)
        action.setMenu(menu)
        toolbar.addAction(action)
        button = toolbar.widgetForAction(action)
        button.setPopupMode(
            QtWidgets.QToolButton.ToolButtonPopupMode.InstantPopup)
        button.setStyleSheet('::menu-indicator {image: none}')

if __name__ == '__main__':

    app = QtWidgets.QApplication(['Test'])
    window = Window()
    window.setGeometry(600, 100, 300, 200)
    window.show()
    app.exec()
© www.soinside.com 2019 - 2024. All rights reserved.