Search code examples
pythonuser-interfacepyqttitlebar

PyQT change dynamically a QMainWindow title


I made a very simple python script to read a sqlite3 database into a QtTableWidget.

import sqlite3 as db
from PyQt4 import QtCore, QtGui
import sys

con = db.connect('results.db', isolation_level=None)
cur = con.cursor()
cur.execute("SELECT * FROM Results")
all_data = cur.fetchall()


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()"), self.populate)
        QtCore.QMetaObject.connectSlotsByName(datadb)

    def populate(self):
        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)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main_conf = QtGui.QDialog()
    ui = UiDialog()
    ui.setupUi(main_conf)
    main_conf.show()
    ret = app.exec_()
    sys.exit(ret)

It works fine. My question is now:

Is there any way to change the window title dynamically on run time?

setWindowTitle() is not updating on the fly the title bar, nor update() or repaint().

Thank you very much for your precious time!


Solution

  • Another solution:

    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")
    

    If you're using a generated code from .ui file, you can write your MAIN Code into another file and just import the generated ui file into your MAIN Code, so future changes will not affect or change your Main code... so the best solution on my option would be this:

    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_())