PyQt - 在QThread中设置代理模型的模型

时间:2013-12-20 16:02:54

标签: python qt pyqt qthread

在我的项目中,我注意到主要对话框在设置某些ProxyModel的模型时冻结,所以我决定为此任务创建一个新线程以提供窗口的响应,但是现在它不断弹出错误那说:
TypeError:QTableView.setModel(QAbstractItemModel):参数1具有意外类型'tuple'

我不知道为什么......
这是我的代码:

这是用于使用我提供的参数

更新proxyModel的QThread
class ThreadedProxyModel(QThread):
    def __init__(self, contacts, contactsProxyModel, groups, groupsProxyModel,
                 chatSession, chatSessionProxyModel, msgs, msgsProxyModel):
        QThread.__init__(self)
        self.contacts = contacts
        self.contactsProxyModel = contactsProxyModel
        self.groups = groups
        self.groupsProxyModel = groupsProxyModel
        self.chatSession = chatSession
        self.chatSessionProxyModel = chatSessionProxyModel
        self.msgs = msgs
        self.msgsProxyModel = msgsProxyModel

    def run(self):
        self.contactsProxyModel.setSourceModel(recordsTableModel(self.contacts))
        self.contactsProxyModel.setFilterKeyColumn(-1)
        self.contactsProxyModel.setFilterCaseSensitivity(Qt.CaseInsensitive)

        self.groupsProxyModel.setSourceModel(recordsTableModel(self.groups))
        self.groupsProxyModel.setFilterKeyColumn(-1)
        self.groupsProxyModel.setFilterCaseSensitivity(Qt.CaseInsensitive)

        self.chatSessionProxyModel.setSourceModel(recordsTableModel(self.chatSession))
        self.chatSessionProxyModel.setFilterKeyColumn(-1)
        self.chatSessionProxyModel.setFilterCaseSensitivity(Qt.CaseInsensitive)

        self.msgsProxyModel.setSourceModel(recordsTableModel(self.msgs))
        self.msgsProxyModel.setFilterKeyColumn(-1)
        self.msgsProxyModel.setFilterCaseSensitivity(Qt.CaseInsensitive)

    def getContactsProxyModel(self):
        return self.contactsProxyModel,

    def getGroupsProxyModel(self):
        return self.groupsProxyModel

    def getChatSessionProxyModel(self):
        return self.chatSessionProxyModel

    def getMsgsProxyModel(self):
        return self.msgsProxyModel

这是在对话框类中调用setProxyModel线程的方法。请注意,所有数据(联系人,群组,聊天......)都可以:

def setProxyModel(self):
    progress = QProgressDialog("Initializing UI ...", "Abort", 0, 0, self)
    progress.setWindowTitle("WhatsApp Browser ...")
    progress.setWindowModality(Qt.WindowModal)
    progress.setMinimumDuration(0)
    progress.setCancelButton(None)
    progress.show()

    queryTh = ThreadedProxyModel(self.contacts, self.contactsProxyModel, self.groups, self.groupsProxyModel,
                                 self.chatSession, self.chatSessionProxyModel, self.msgs, self.msgsProxyModel,)
    queryTh.start()
    while queryTh.isRunning():
        QApplication.processEvents()

    self.contactsProxyModel = queryTh.getContactsProxyModel()
    self.groupsProxyModel = queryTh.getGroupsProxyModel()
    self.chatSessionProxyModel = queryTh.getChatSessionProxyModel()
    self.msgsProxyModel = queryTh.getMsgsProxyModel()

    progress.close()  

这是在我的对话框中的 init 方法中,我创建了proxymodels并调用方法在Thread中更新它们然后我在各种QTableView中设置它们:

self.contactsProxyModel = QSortFilterProxyModel(self)
self.groupsProxyModel = QSortFilterProxyModel(self)
self.groupMembersProxyModel = QSortFilterProxyModel(self)
self.chatSessionProxyModel = QSortFilterProxyModel(self)
self.chatMsgsProxyModel = QSortFilterProxyModel(self)
self.msgsProxyModel = QSortFilterProxyModel(self)

self.setProxyModel()

self.contactsTableView.setModel(self.contactsProxyModel)
self.contactsTableView.resizeColumnsToContents()

self.groupsTableView.setModel(self.groupsProxyModel)
self.groupsTableView.resizeColumnsToContents()

self.chatSessionTableView.setModel(self.chatSessionProxyModel)
self.chatSessionTableView.resizeColumnsToContents()

self.chatSessionTableView.clicked.connect(self.setChatMsgsProxyModel)
self.chatMsgsTableView.resizeColumnsToContents()

self.groupsTableView.clicked.connect(self.setGroupMembersProxyModel)
self.groupMembersTableView.resizeColumnsToContents()

self.msgsTableView.setModel(self.msgsProxyModel)
self.msgsTableView.resizeColumnsToContents()

感谢您的任何建议,我很困难......

1 个答案:

答案 0 :(得分:1)

不确定,但似乎

def getContactsProxyModel(self):
   return self.contactsProxyModel,

返回一个元组,尝试删除逗号