gpt4 book ai didi

python - 如何在 PyQt5 中通过单击按钮将项目设置为正在编辑

转载 作者:行者123 更新时间:2023-12-01 06:36:07 24 4
gpt4 key购买 nike

我正在尝试在我一直在开发的 GUI 中创建一个“重命名”按钮。单击此按钮时,我希望所选项目进入“编辑模式”,您可以在其中键入新名称。

我已经到了一切正常的地步,但我不知道如何正确设置标志以便从索引进入编辑模式。下面是一个独立的工作示例。

在此示例中,如果我选择了要编辑的项目,然后按 F2,则进入编辑模式。当我单击“重命名”按钮时,我想实现同样的目标。

class MainWindow(QMainWindow):

def __init__(self, app):
super().__init__()

self.main_widget = QWidget()
self.tree = DataBrowserTree()
self.setCentralWidget(self.tree)
self.resize(QSize(1340, 1080))
self.show()


class DataBrowserTree(QTreeView):
def __init__(self):
super(DataBrowserTree, self).__init__()

datafiles_path = r'C:\Repositories\QDscript\DotGUI\TestProjects\Proj_3\datafiles'

self.model = QFileSystemModel()
self.model.setRootPath(datafiles_path)
self.model.setReadOnly(False)
index = self.model.index(datafiles_path)
self.setObjectName('dataBrowserTree')

self.setModel(self.model)
self.setRootIndex(index)
self.setAnimated(False)
self.setIndentation(20)
self.setSortingEnabled(True)
self.sortByColumn(0, Qt.AscendingOrder)
self.setHeaderHidden(True)
self.setSortingEnabled(False)
self.setColumnHidden(1, True)
self.setColumnHidden(2, True)
self.setColumnHidden(3, True)
self.setWindowTitle("Dir View")

def mouseReleaseEvent(self, QMouseEvent):
if QMouseEvent.button() == 2:
self.show_rightclick_menu(QMouseEvent.globalPos())

def show_rightclick_menu(self, pos):
self.rightclick_menu = QMenu()
self.action_rename = QAction(self)
self.action_rename.setText('Rename')
self.action_rename.triggered.connect(lambda: self.clicked('rename'))
self.rightclick_menu.addAction(self.action_rename)
self.rightclick_menu.move(pos.x(), pos.y())
self.rightclick_menu.show()


def clicked(self, click_str):
if click_str == 'rename':
index = self.selectedIndexes()[0] # This gets the correct QModelIndex
print(index) # <PyQt5.QtCore.QModelIndex object at 0x1823D930>

def edit(self, index, trigger, event):
# When F2 is pressed I get:
# index --> QModelIndex object
# trigger --> 8 (the key code)
# event --> QKeyEvent object

return QTreeView.edit(self, index, trigger, event)

if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow(app)
sys.exit(app.exec_())

最佳答案

您不必覆盖 edit()方法但使用它。另一方面,不要覆盖 mouseReleaseEvent覆盖默认行为的方法 customContextMenuRequested设置 contextMenuPolicy 时应使用信号至Qt::CustomContextMenu如下图:

class DataBrowserTree(QTreeView):
def __init__(self):
super(DataBrowserTree, self).__init__()

datafiles_path = r'C:\Repositories\QDscript\DotGUI\TestProjects\Proj_3\datafiles'

self.model = QFileSystemModel()
self.model.setRootPath(datafiles_path)
self.model.setReadOnly(False)
index = self.model.index(datafiles_path)
self.setObjectName("dataBrowserTree")

self.setModel(self.model)
self.setRootIndex(index)
self.setAnimated(False)
self.setIndentation(20)
self.setSortingEnabled(True)
self.sortByColumn(0, Qt.AscendingOrder)
self.setHeaderHidden(True)
self.setSortingEnabled(False)
for i in (1, 2, 3):
self.setColumnHidden(i, True)
self.setWindowTitle("Dir View")

self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_rightclick_menu)

def show_rightclick_menu(self, pos):
ix = self.indexAt(pos)

rightclick_menu = QMenu()
action_rename = QAction(self)
action_rename.setText("Rename")
rightclick_menu.addAction(action_rename)
action = rightclick_menu.exec_(self.viewport().mapToGlobal(pos))
if action == action_rename:
self.edit(ix)

关于python - 如何在 PyQt5 中通过单击按钮将项目设置为正在编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59657665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com