如何使用QThread从慢速Sql语句填充自定义QTableWidget?

时间:2015-10-19 18:09:13

标签: python pyqt4 qthread qtablewidget

在这种情况下,我正在尝试填充一个QtableWidget,它被修改为使用类TableWidgetImage加载图像

from PyQt4 import QtGui
import sys
class ImageWidget(QtGui.QWidget):
    def __init__(self, imagePath, parent):
        super(ImageWidget, self).__init__(parent)
        self.picture = QtGui.QPixmap(imagePath)
    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(0, 0, self.picture)
class TableWidgetImage(QtGui.QTableWidget):
    def setImage(self, row, col, imagePath):
        image = ImageWidget(imagePath, self)
        self.setCellWidget(row, col, image)

功能是:我从“cbUser”(帐户)中选择一个Item我按“btnCargar”并且“tableSusAmigos”加载正常。我的问题集中在下一步。当我在一行中点击“tableSusAmigos”时,它应该加载第二个QTableWidget。有用。但我的问题是当有很多行时。我想使用QThread加载。我不知道如何使用QThread修改UI QTableWidget控件。这是我的其他文件Python,包含2个类:

class Ui_friendForm(QMdiSubWindow):
    def __init__(self):
        QtGui.QMdiSubWindow.__init__(self)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/mmAmistad/mmAmistad.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.setWindowIcon(icon)
        self.resize(878, 637)
        self.cbUser = QtGui.QComboBox(self)
        self.cbUser.setGeometry(QtCore.QRect(20, 70, 211, 22))
        self.cbUser.setObjectName(_fromUtf8("cbUser"))
        self.btnCargar = QtGui.QPushButton(self)
        self.btnCargar.setGeometry(QtCore.QRect(250, 60, 91, 41))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/picLoad/btnLoad.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btnCargar.setIcon(icon)
        self.btnCargar.setObjectName(_fromUtf8("btnCargar"))
        self.tableSusAmigos = TableWidgetImage(self)
        self.tableSusAmigos.setGeometry(QtCore.QRect(20, 110, 391, 461))
        self.tableSusAmigos.setColumnCount(3)
        self.tableSusAmigos.setObjectName(_fromUtf8("tableSusAmigos"))
        self.tableSusAmigos.setRowCount(0)
        self.tableSusAmigos.verticalHeader().setDefaultSectionSize(50)
        self.tableSusAmigos.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self.tableSusAmigos.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.tableSusAmigos.setSortingEnabled(1)
        item = QtGui.QTableWidgetItem()
        self.tableSusAmigos.setHorizontalHeaderItem(0, item)
        item = QtGui.QTableWidgetItem()
        self.tableSusAmigos.setHorizontalHeaderItem(1, item)
        item = QtGui.QTableWidgetItem()
        self.tableSusAmigos.setHorizontalHeaderItem(2, item)
        self.tableAmigosde = TableWidgetImage(self)
        self.tableAmigosde.setGeometry(QtCore.QRect(470, 110, 381, 461))
        self.tableAmigosde.setColumnCount(3)
        self.tableAmigosde.setObjectName(_fromUtf8("tableAmigosde"))
        self.tableAmigosde.setRowCount(0)
        self.tableAmigosde.verticalHeader().setDefaultSectionSize(50)
        self.tableAmigosde.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self.tableAmigosde.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.tableAmigosde.setSortingEnabled(1)
        item = QtGui.QTableWidgetItem()
        self.tableAmigosde.setHorizontalHeaderItem(0, item)
        item = QtGui.QTableWidgetItem()
        self.tableAmigosde.setHorizontalHeaderItem(1, item)
        item = QtGui.QTableWidgetItem()
        self.tableAmigosde.setHorizontalHeaderItem(2, item)
        self.setWindowTitle(_translate("Form", "Form", None))
        self.btnCargar.setText(_translate("Form", "Cargar", None))
        self.btnCargar.clicked.connect(self.cargarAmigos)
        item = self.tableSusAmigos.horizontalHeaderItem(0)
        item.setText(_translate("Form", "ID", None))
        item = self.tableSusAmigos.horizontalHeaderItem(1)
        item.setText(_translate("Form", "Nombre", None))
        item = self.tableSusAmigos.horizontalHeaderItem(2)
        item.setText(_translate("Form", "Foto", None))
        item = self.tableAmigosde.horizontalHeaderItem(0)
        item.setText(_translate("Form", "ID", None))
        item = self.tableAmigosde.horizontalHeaderItem(1)
        item.setText(_translate("Form", "Nombre", None))
        item = self.tableAmigosde.horizontalHeaderItem(2)
        item.setText(_translate("Form", "Foto", None))
        self.pbCarga = QtGui.QProgressBar(self)
        self.pbCarga.setGeometry(QtCore.QRect(20, 590, 391, 23))
        self.pbCarga.setProperty("value", 24)
        self.pbCarga.setValue(0)
        self.pbCarga.setObjectName(_fromUtf8("pbCarga"))
        self.setWindowTitle(_translate("Form", "List Friends", None))
        self.objDato = DBFacebook()
        self.cargarCombo()
        QtCore.QMetaObject.connectSlotsByName(self)
        self.tableSusAmigos.doubleClicked.connect(self.doubleClicked_table)
        self.thread = TaskThread(self)
        self.thread.started.connect(self.handleTaskUpdated)    
    def cargarCombo(self):
        self.objDato.__enter__()
        for row in self.objDato.SELECT_CUENTA_USER():
            self.cbUser.addItem(str(row[1]),int(row[0]))
        self.objDato.__exit__()
    def doubleClicked_table(self):
        index = self.tableSusAmigos.selectedIndexes()[0]
        self.id_us = int(self.tableSusAmigos.model().data(index).toString())
        self.tableAmigosde.setRowCount(0);
        self.pbCarga.setRange(0,0)
        self.pbCarga.setValue(0)
        self.thread.quit()
        self.thread.start()
    def handleTaskUpdated(self):
        rowIndex = 0
        self.objDato.__enter__()
        for row in self.objDato.SELECT_AMISTADES(self.id_us):
            self.tableAmigosde.insertRow(rowIndex)
            for column in range(0,3):
                if column == 2:
                    self.tableAmigosde.setImage(rowIndex, column, str(row[column]))
                else:
                    newItem = QtGui.QTableWidgetItem(str(row[column]).decode('utf-8'))
                    self.tableAmigosde.setItem(rowIndex,column,newItem)
            rowIndex = rowIndex + 1
        self.objDato.__exit__()
        self.pbCarga.setRange(0,1)
        self.pbCarga.setValue(1)
    def cargarAmigos(self):
        id_us = self.cbUser.itemData(self.cbUser.currentIndex()).toPyObject()
        self.objDato.__enter__()
        rowIndex = 0
        self.tableSusAmigos.setRowCount(0);
        for row in self.objDato.SELECT_AMISTADES(id_us):
            self.tableSusAmigos.insertRow(rowIndex)
            for column in range(0,3):
                if column == 2:
                    self.tableSusAmigos.setImage(rowIndex, column, str(row[column]))
                else:
                    newItem = QtGui.QTableWidgetItem(str(row[column]).decode('utf-8'))
                    self.tableSusAmigos.setItem(rowIndex,column,newItem)
            rowIndex = rowIndex + 1
        self.objDato.__exit__()
class TaskThread(QtCore.QThread):
    def __init__(self,parent):
        QtCore.QThread.__init__(self, parent)
    def run(self):
        self.emit(QtCore.SIGNAL('started'))        
if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ui = Ui_friendForm()
    ui.show()
    sys.exit(app.exec_())

我将照片附加到我的应用程序中。提前谢谢。

enter image description here

1 个答案:

答案 0 :(得分:0)

只能从主线程中访问窗口小部件。但是,只要使用默认连接类型,使用插槽和信号就可以安全地从另一个线程与UI交互。

检查this我之前创建的示例(在wiki中有关于代码的详细信息)。基本上,您需要做的就是在表格小部件所在的位置添加一个插槽。使用槽的参数(将为表项提供数据),您需要创建QTableWidgetItem并插入它。至于信号本身,它可以从你的工作线程发出,在那里查询你的数据库,并将检索到的数据转换成一个或多个表行,然后通过该信号发出。