解决PyQt错误(QObject::startTimer: QTimer can only be used with threads started with QThread)

来源:互联网 发布:河南奔驰网络办公平台 编辑:程序博客网 时间:2024/06/05 13:07

近日在PyQt使用Model时,发现如果Model创建时未设置Parent,则运行完退出时会报错:

QObject::startTimer: QTimer can only be used with threads started with QThread

  解决方法也很简单,创建Model时加上Parent即可。关于这个问题的原因及解决方案可以查看链接:PyQt QTimer problem {FIXED}。

测试代码如下:
  

#coding=utf-8from PyQt4.QtCore import *from PyQt4.QtGui import *if __name__ == '__main__':  import sys  app = QApplication(sys.argv)  table = QTableView()  model = QStandardItemModel(3, 3)  # 改为 model = QStandardItemModel(3, 3, table) 即可避免错误  table.setModel(model)  model.setHorizontalHeaderLabels(['Name', 'Height', 'Weight'])  model.setData(model.index(0, 0, QModelIndex()), QVariant(u'松鼠'))  model.setData(model.index(0, 1, QModelIndex()), QVariant(u'80cm'))  model.setData(model.index(0, 2, QModelIndex()), QVariant(u'12Kg'))  model.setData(model.index(1, 0, QModelIndex()), QVariant(u'树袋熊'))  table.setGeometry(80, 20, 400, 300)  table.setWindowTitle('Grid + Table Testing')  table.show()  sys.exit(app.exec_())

转载于:http://dreamisx.blog.163.com/blog/static/115004839201291224117107/

阅读全文
0 0
原创粉丝点击