我制作了一个非常简单的python脚本,将sqlite3数据库读入QtTableWidget。
delimiter = Word(printables, exact=1)
text = Forward() #(Word(printables) + ~delimiter)
def setTextExcludingDelimiter(s,l,t):
# define Word as all printable characters, excluding the delimiter character
# the excludeChars argument for Word is how this is done
text_word = Word(printables, excludeChars=t[0]).setName("text")
# use '<<' operator to assign the text_word definition to the
# previously defined text expression
text << text_word
# attach parse action to delimiter, so that once it is matched,
# it will define the correct expression for text
delimiter.setParseAction(setTextExcludingDelimiter)
# make the text expressions Optional with default value of '' to satisfy 3rd test case
parser = delimiter + Optional(text,'') + delimiter + Optional(text,'') + delimiter
工作正常。我现在的问题是:
有没有办法在运行时动态更改窗口标题?
setWindowTitle()不会动态更新标题栏,也不会更新()或重绘()。
非常感谢您宝贵的时间!
答案 0 :(得分:4)
另一种解决方案:
class UiDialog(object):
def setupUi(self, datadb):
datadb.setObjectName("Dialog")
datadb.resize(404, 304)
datadb.setWindowTitle("Database results")
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(datadb.sizePolicy().hasHeightForWidth())
datadb.setSizePolicy(sizePolicy)
datadb.setMinimumSize(QtCore.QSize(404, 304))
datadb.setMaximumSize(QtCore.QSize(404, 304))
self.table = QtGui.QTableWidget(datadb)
self.table.setGeometry(QtCore.QRect(2, 2, 400, 261))
self.table.setObjectName("table")
self.show = QtGui.QPushButton(datadb)
self.show.setGeometry(QtCore.QRect(2, 270, 400, 31))
self.show.setObjectName("show")
self.show.setText("Show results")
QtCore.QObject.connect(self.show, QtCore.SIGNAL("clicked()"), lambda: self.populate(datadb)) ### Using LAMBDA function to pass datadb object to the method
#self.show.pressed.connect(lambda: self.populate(datadb)) ### Another syntax for the same operation
QtCore.QMetaObject.connectSlotsByName(datadb)
def populate(self, mainWindow):
self.table.setRowCount(len(all_data))
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(['Number', 'Keys', 'Time', 'Tries'])
for i, item in enumerate(all_data):
number = QtGui.QTableWidgetItem(str(item[0]))
keys = QtGui.QTableWidgetItem(item[1])
time = QtGui.QTableWidgetItem(str(item[2]))
tries = QtGui.QTableWidgetItem(str(item[3]))
self.table.setItem(i, 0, number)
self.table.setItem(i, 1, keys)
self.table.setItem(i, 2, time)
self.table.setItem(i, 3, tries)
mainWindow.setWindowTitle("ANOTHER TITLE")
如果您使用.ui
文件中生成的代码,则可以将MAIN代码写入另一个文件,然后将生成的ui文件导入MAIN代码,以便将来的更改不会影响或更改您的主要代码代码...所以我选择的最佳解决方案是:
from PyQt4 import QtCore, QtGui
from pyUI import UiDialog ### your UI file, pyUI is the file name and UiDialog is the Ui Class name
import sys
class GUI(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.ui = UiDialog()
self.ui.setupUi(self)
self.ui.show.pressed.connect(self.populate)
def populate(self):
self.ui.table.setRowCount(len(all_data))
self.ui.table.setColumnCount(4)
self.ui.table.setHorizontalHeaderLabels(['Number', 'Keys', 'Time', 'Tries'])
for i, item in enumerate(all_data):
number = QtGui.QTableWidgetItem(str(item[0]))
keys = QtGui.QTableWidgetItem(item[1])
time = QtGui.QTableWidgetItem(str(item[2]))
tries = QtGui.QTableWidgetItem(str(item[3]))
self.ui.table.setItem(i, 0, number)
self.ui.table.setItem(i, 1, keys)
self.ui.table.setItem(i, 2, time)
self.ui.table.setItem(i, 3, tries)
self.setWindowTitle("ANOTHER TITLE")
con = db.connect('results.db', isolation_level=None)
cur = con.cursor()
cur.execute("SELECT * FROM Results")
all_data = cur.fetchall()
app = QtGui.QApplication(sys.argv)
dailog = GUI()
dailog.show()
sys.exit(app.exec_())
答案 1 :(得分:1)
def setupUi(self, datadb):
[...]
self.datadb = datadb
def populate(self):
[...]
self.datadb.setWindowTitle('new title')
这很有效。但是你正在以一种奇怪而不方便的方式构建你的小部件。更常规的是使用继承:
class DatabaseUi(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.table = QtGui.QTableWidget()
self.button = QtGui.QPushButton()
self.button.setText("Show results")
self.button.clicked.connect(self.populate)
vlay = QtGui.QVBoxLayout()
vlay.addWidget(self.table)
vlay.addWidget(self.button)
self.setLayout(vlay)
self.setFixedSize(404, 304)
self.setWindowTitle("Database results")
def populate(self):
self.setWindowTitle('new title')
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(['Number', 'Keys', 'Time', 'Tries'])
[...]
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ui = DatabaseUi()
ui.show()
sys.exit(app.exec_())
答案 2 :(得分:-3)
self.ui.btn_close.clicked.connect(self.btn_close_clicked)
def btn_close_clicked(self): self.close()