QThread并且无法设置父级,新父级位于不同的线程中

时间:2016-08-06 19:55:38

标签: python pyqt

我尝试使用QThread添加一个繁琐的任务,即将QWidget个对象添加到我的QTreeWidget中,因为我认为QThread会有所有这些必需品。但没有运气!我能够阅读的内容是我不能在QMain方法之外进行突然操作,但是可以采用这种方法。

下面是QThread classclass,它将项目添加到QTreeWidget。这很容易。但每当我尝试添加QPushUpButton时,它就会给我一个育儿错误。

QThread无法实现这一目标吗?

import sys
from PyQt4 import QtGui as qt
from PyQt4.QtCore import QThread

class YourThreadName(QThread):

def __init__(self, QTreeWidget, obj):
    QThread.__init__(self)
    self.Tree = QTreeWidget
    self.obj = obj
def __del__(self):
    self.wait()

def run(self):
    item = qt.QTreeWidgetItem()
    item.setText(0, "hi")

    self.Tree.addTopLevelItem(item)

    bttn = qt.QPushButton("Graph")
    bttn.clicked.connect(self.obj.printer)
    bttn.setObjectName("Test")

    self.Tree.setItemWidget(item, 1, bttn)

class Window(qt.QMainWindow):

def __init__(self, parent=None):
    super(Window, self).__init__(parent)

    frameWrapper = qt.QFrame(self)    
    self.setCentralWidget(frameWrapper)

    self.frameAlgorithm = qt.QFrame(frameWrapper)
    self.frameAlgorithm.setGeometry(0, 0, 700, 400)
    self.faLayout = qt.QGridLayout(self.frameAlgorithm)

    bttn = qt.QPushButton("Add Rows", self)
    bttn.clicked.connect(self.StartThread)
    self.faLayout.addWidget(bttn, 0, 0, 1, 1)

    self.Tree = qt.QTreeWidget()
    self.Tree.setHeaderLabels(["Name", "Button"])
    self.faLayout.addWidget(self.Tree, 1, 0, 1, 1)

    self.setGeometry(600, 400, 700, 400)
    self.show()

def StartThread(self):    
    qThread = YourThreadName(self.Tree, self)
    qThread.start() 

def printer(self):
    print(self.sender().objName())

app = qt.QApplication(sys.argv)
w = Window()
sys.exit(app.exec_())

0 个答案:

没有答案