我想要一个包含一列自动完成的表。我认为最好的方法是使用QTableWidget,然后使用默认的QTableWidgetItem(它不支持QCompleters),我会将单元格设置为QLineEdit,并使用完成器。但是,当我这样做时,单元格框轮廓闪烁,我收到以下错误:QObject::startTimer: QTimer can only be used with threads started with QThread
这是一个样本。尝试添加几行(小键盘上的+),向它们添加数据,然后快速关闭以获取错误。
import sys
from PyQt4 import QtGui, QtCore
class DevTLog(QtGui.QMainWindow):
def __init__(self):
super(DevTLog, self).__init__()
self.initUI()
def initUI(self):
# Actions
self.addItemAction = QtGui.QAction("Add row", self)
self.addItemAction.setShortcut(QtCore.Qt.Key_Plus)
# Menu
menu = self.menuBar()
menu_file = menu.addMenu("File")
menu_file.addAction(self.addItemAction)
# Table
self.log = QtGui.QTableWidget()
self.log.setColumnCount(3)
self.log.setColumnWidth(0,150)
self.setCentralWidget(self.log)
self.setGeometry(100, 100, 600, 600)
# signals/slots
self.addItemAction.triggered.connect(self.addItem)
def addItem(self):
nameItem = QtGui.QLineEdit()
completer = QtGui.QCompleter()
nameItem.setCompleter(completer)
row = self.log.rowCount()
self.log.insertRow(row)
self.log.setItem(row, 0, QtGui.QTableWidgetItem())
self.log.setCellWidget(row, 1, nameItem)
self.log.setItem(row, 2, QtGui.QTableWidgetItem())
def main():
app = QtGui.QApplication(sys.argv)
ex = DevTLog()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()