在PyQt4中如何绑定QTreeWidget和QStackedWidget

Mersper
  • 13

我想通过点击QTreeWidget中不同的item来切换QStackedWidget中的index。如果是QListWidget很好做到:

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)

        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)
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_()`

但是在QtreeWidget里折腾了很久也找不到好方法:

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)

这样完全无法实现

回复
阅读 2.6k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏