简体   繁体   中英

Use QThread to periodically update a QTableWidget pyqt

In my application, I'm fetching records using an API call and then I add the data to a QTableWidget dynamically. Here is a snippet of my code so far:

class TriageUI(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_TriageWindow()
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
        self.move(QtGui.QApplication.desktop().screen().rect().center()- self.rect().center())
        self.ui.setupUi(self)
        self.update_records()        

    def update_records(self):
        #items are the results from the API fetch
        items = json.loads(get_triage_queue(COOKIES, SERVER, PORT))
        rows = len(items['objects'])
        self.ui.tableWidget.setColumnCount(5)
        self.ui.tableWidget.setRowCount(rows)
        index = 0
        column = 0
        for j in items['objects']:
            for key, value in j.iteritems():
                f = QtGui.QTableWidgetItem(str(value))
                self.ui.tableWidget.setItem(index, column, QtGui.QTableWidgetItem(f))
                column = column + 1

However, I want to be able to make the API call for data periodically(eg after 15 seconds) and then add any new data items in the results to the table. How can I achieve this.

Thank you in advance.

Here you have an example of doing repetitive calls to a class member function (that could be your update_records function) using a PyQt4.QtCore.QTimer . Some times the solution to a problem is more easy than we think.

Note the functions start and stop . This functions makes you able to start and stop the timer at your will.

from PyQt4 import QtGui as gui
from PyQt4 import QtCore as core


class Blinker(gui.QWidget):
    def __init__(self, parent=None):
        super(Blinker, self).__init__(parent)

        self.label = gui.QLabel(self)
        self.label.setFixedSize(200, 200)
        self.layout = gui.QHBoxLayout(self)
        self.layout.addWidget(self.label)

        self.timer  = core.QTimer(self)
        self.timer.setInterval(1000)          # Throw event timeout with an interval of 1000 milliseconds
        self.timer.timeout.connect(self.blink) # each time timer counts a second, call self.blink
        self.color_flag = True

    def start(self):
        self.timer.start()

    def stop(self):
        self.timer.stop()

    @core.pyqtSlot()
    def blink(self):
        if self.color_flag:
            self.label.setStyleSheet("background-color: blue;")
        else:
            self.label.setStyleSheet("background-color: yellow;")
        self.color_flag = not self.color_flag


if __name__ == '__main__':
    import sys
    app = gui.QApplication(sys.argv)

    w = Blinker()
    w.show()
    w.start()

    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM