gpt4 book ai didi

python - 如何在PyQt4中连接QTreeWidget和QStackedWidget?

转载 作者:行者123 更新时间:2023-12-01 02:28:45 24 4
gpt4 key购买 nike

很抱歉,我只是 Python 的初学者。我只想通过 QTreeWidget 的项目单击来更改 QStackedWidget 的索引。我在网上搜索了SIGNAL和SLOT的教程,但就是无法解决问题。QTreeWidget信号和QStackedWidget槽中的参数没有拟合。

self.connect(qtree, QtCore.SIGNAL("itemClicked(QTreeWidgetItem*,int)"), stack, QtCore.SLOT("setCurrentIndex(int)"))

我尝试了这个:

qtree.itemClicked.connect(stack.setCurrentIndex)

它只是显示错误:

TypeError: setCurrentIndex(self, int): argument 1 has unexpected type 'QTreeWidgetItem'

我想可能有一个方法,但我在网络上找不到。

像这样:

 from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

class StockDialog(QDialog):
def __init__(self,parent=None):
super(StockDialog,self).__init__(parent)

mainSplitter=QSplitter(Qt.Horizontal)

treewidget = QTreeWidget(mainSplitter)
treewidget.setHeaderLabels(["Tree"])
treeroot = QTreeWidgetItem(treewidget, ["Stack"])
treeitem1 = QTreeWidgetItem(["WorkSpace"])
treeitem2 = QTreeWidgetItem(["About"])
treeroot.addChild(treeitem1)
treeroot.addChild(treeitem2)

stack=QStackedWidget(mainSplitter)
stack.setFrameStyle(QFrame.Panel|QFrame.Raised)

stackworkspace=StackWorkSpace()
stackabout=StackAbout()
stack.addWidget(stackworkspace)
stack.addWidget(stackabout)

closePushButton=QPushButton(self.tr("Close"))

self.connect(treewidget,
SIGNAL("itemClicked(int)"),
stack,SLOT("setCurrentIndex(int)"))
self.connect(closePushButton,
SIGNAL("clicked()"),
self,SLOT("close()"))

layout=QVBoxLayout(self)
layout.addWidget(mainSplitter)
layout.addWidget(closePushButton)
self.setLayout(layout)

class StackWorkSpace(QWidget):
def __init__(self,parent=None):
super(StackWorkSpace,self).__init__(parent)
widget1=QTextEdit(self.tr("WorkSpace"))
widget2=QTextEdit(self.tr("WorkSpace"))

layout=QGridLayout(self)
layout.addWidget(widget1,0,0)
layout.addWidget(widget2,0,1)

class StackAbout(QDialog):
def __init__(self,parent=None):
super(StackAbout,self).__init__(parent)
self.setStyleSheet("background: red")

app=QApplication(sys.argv)
main=StockDialog()
main.show()
app.exec_()

当将StockDialog类中的QTreeWidget更改为QListWidget时,它可以工作。

class StockDialog(QDialog):
def __init__(self,parent=None):
super(StockDialog,self).__init__(parent)

mainSplitter=QSplitter(Qt.Horizontal)

listwidget=QListWidget(mainSplitter)
listwidget.insertItem(0,self.tr("WorkSpace"))
listwidget.insertItem(1,self.tr("About"))

stack=QStackedWidget(mainSplitter)
stack.setFrameStyle(QFrame.Panel|QFrame.Raised)

stackworkspace=StackWorkSpace()
stackabout=StackAbout()
stack.addWidget(stackworkspace)
stack.addWidget(stackabout)

closePushButton=QPushButton(self.tr("Close"))

self.connect(listwidget,
SIGNAL("currentRowChanged(int)"),
stack,SLOT("setCurrentIndex(int)"))
self.connect(closePushButton,
SIGNAL("clicked()"),
self,SLOT("close()"))

layout=QVBoxLayout(self)
layout.addWidget(mainSplitter)
layout.addWidget(closePushButton)
self.setLayout(layout)

现在,我想用 QTreeWidget 来做到这一点,我该怎么做?

最佳答案

解决这个问题的策略是在QTreeWidgetItem中保存与每个widget相关的索引信息。 QTreeWidgetItem 具有 setData()方法允许我们保存项目中的信息,在本例中我们将保存索引。每次通过 addWidget() 将小部件添加到 QStackedWidget 时都会返回索引。 ,所以总而言之,我们将执行以下操作:

treeitem1.setData(0, Qt.UserRole, stack.addWidget(stackworkspace))
treeitem2.setData(0, Qt.UserRole, stack.addWidget(stackabout))

连接itemClickedQTreeWidget 的信号,这将返回按下的列和项目,利用此信息我们获得它的 QStackedWidget 索引,我们恢复通过函数 data() 保存的数据:

treewidget.itemClicked.connect(lambda item, column: stack.setCurrentIndex(item.data(column, Qt.UserRole))
if item.data(column, Qt.UserRole) is not None else None)

必要的代码可以在以下部分中找到:

class StockDialog(QDialog):
def __init__(self, parent=None):
super(StockDialog, self).__init__(parent)

mainSplitter = QSplitter(Qt.Horizontal)

treewidget = QTreeWidget(mainSplitter)
treewidget.setHeaderLabels(["Tree"])
treeroot = QTreeWidgetItem(treewidget, ["Stack"])
treeitem1 = QTreeWidgetItem(["WorkSpace"])
treeitem2 = QTreeWidgetItem(["About"])
treeroot.addChild(treeitem1)
treeroot.addChild(treeitem2)

stack = QStackedWidget(mainSplitter)
stack.setFrameStyle(QFrame.Panel | QFrame.Raised)

stackworkspace = StackWorkSpace()
stackabout = StackAbout()

treeitem1.setData(0, Qt.UserRole, stack.addWidget(stackworkspace))
treeitem2.setData(0, Qt.UserRole, stack.addWidget(stackabout))

closePushButton = QPushButton(self.tr("Close"))

treewidget.itemClicked.connect(lambda item, column: stack.setCurrentIndex(item.data(column, Qt.UserRole))
if item.data(column, Qt.UserRole) is not None else None)

layout = QVBoxLayout(self)
layout.addWidget(mainSplitter)
layout.addWidget(closePushButton)
self.setLayout(layout)

关于python - 如何在PyQt4中连接QTreeWidget和QStackedWidget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47076370/

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