Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. After delay refresh QTableWidget with new query
Forum Updated to NodeBB v4.3 + New Features

After delay refresh QTableWidget with new query

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 6.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    techybri
    wrote on last edited by
    #1

    I am absolutely new to programming in general, but am loving learning python. Working on a little project as I learn just as a challenge and there's always things I cant figure out easily, but this one eludes me after days and days of trying different things.

    I want to display 10 rows of data from a database table, have that data remain displayed for 20 seconds, and then refresh the view with the next 10 rows from the database table. I have been able to get the data to display, but cannot figure out how to refresh the display with new data after a time.

    This is the code I use to get the data displayed in the first place.

    @import sys
    import MySQLdb
    from PyQt4.QtGui import *
    import time

    db=MySQLdb.connect("localhost", "user", "password", "DBNAME")
    cursor=db.cursor()
    cursor.execute("SELECT * FROM table ORDER BY column DESC LIMIT 10")
    rows=(cursor.fetchall())

    app=QApplication(sys.argv)
    table=QTableWidget(0, 6)

    table.setHorizontalHeaderLabels(["Name", "Team", "GameStatus", "GameClock", "PACmp", "FPTS"])
    table.setAlternatingRowColors(True)
    for row in rows:
    table.insertRow(table.rowCount())
    name = QTableWidgetItem(str(row[1]), 0)
    table.setItem(table.rowCount()-1, 0, name)
    team = QTableWidgetItem(str(row[2]), 0)
    table.setItem(table.rowCount()-1, 1, team)
    gameStatus = QTableWidgetItem(row[3], 0)
    table.setItem(table.rowCount()-1, 2, gameStatus)
    gameClock = QTableWidgetItem(row[4], 0)
    table.setItem(table.rowCount()-1, 3, gameClock)
    PACmp = QTableWidgetItem(str(row[5]), 0)
    table.setItem(table.rowCount()-1, 4, PACmp)
    FPTS = QTableWidgetItem(str(row[6]), 0)
    table.setItem(table.rowCount()-1, 5, FPTS)
    table.update()

    table.show()
    sys.exit(app.exec_())
    @

    My feeble mind thinks I should be able to just do that again and have it refresh, but nothing happens when I add code to the above. Here is what I have tried, appended this to the above:

    @time.sleep(20)

    cursor.execute("SELECT * FROM table ORDER BY column DESC LIMIT 10,10")
    rows=(cursor.fetchall())

    for row in rows:
    table.insertRow(table.rowCount())
    name = QTableWidgetItem(str(row[1]), 0)
    table.setItem(table.rowCount()-1, 0, name)
    team = QTableWidgetItem(str(row[2]), 0)
    table.setItem(table.rowCount()-1, 1, team)
    gameStatus = QTableWidgetItem(row[3], 0)
    table.setItem(table.rowCount()-1, 2, gameStatus)
    gameClock = QTableWidgetItem(row[4], 0)
    table.setItem(table.rowCount()-1, 3, gameClock)
    PACmp = QTableWidgetItem(str(row[5]), 0)
    table.setItem(table.rowCount()-1, 4, PACmp)
    FPTS = QTableWidgetItem(str(row[6]), 0)
    table.setItem(table.rowCount()-1, 5, FPTS)
    table.update()
    table.show()
    sys.exit(app.exec_())
    @

    Must be terribly annoying to people that know what they are doing to read this kind of garbage, but I am just getting started and am really hoping to make a little breakthrough on this.

    1 Reply Last reply
    0
    • francescmmF Offline
      francescmmF Offline
      francescmm
      wrote on last edited by
      #2

      Why not to use QSqlTableModel and QTableView? With this method you can provide the query and the model fills automatically the table.

      Then, you can reset the model without reset the table manually.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        techybri
        wrote on last edited by
        #3

        Thank you so much for the reply. I think I have taken your advice properly with the following code:

        @from PyQt4 import QtGui, QtCore, QtSql
        import sys

        db = QtSql.QSqlDatabase.addDatabase("QMYSQL")
        db.setHostName("localhost")
        db.setDatabaseName("dbname")
        db.setUserName("user")
        db.setPassword("password")
        db.open()

        model=QtSql.QSqlTableModel()
        model.setTable("table")
        model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
        query1 = QtSql.QSqlQuery("SELECT * FROM table ORDER BY column DESC LIMIT 10")
        model.setQuery(query1)

        app = QtGui.QApplication(sys.argv)
        form = QtGui.QTableView()
        form.setModel(model)

        form.show()
        sys.exit(app.exec_())@

        I know I need to enter a time.sleep(20) somewhere to get my 20 second delay before running the next query, but Im not sure where in the code to place that. Or where in the code to place the next query. Any advice is greatly appreciated.

        1 Reply Last reply
        0
        • francescmmF Offline
          francescmmF Offline
          francescmm
          wrote on last edited by
          #4

          If what you want is to refresh the table with the new that every 20 seconds, you can implement a QTimer that calls a slot with the necessary sentences. You can define the timer with a start(20000) and then connect its timeout() signal to your method.

          I don't know how to do it because of my rusted Python skills, hehehe.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            techybri
            wrote on last edited by
            #5

            Thanks again for the reply. I was sure I hit the jackpot when I trie putting "QtCore.QTimer.singleShot(30000, model, QtCore.SLOT(model.setQuery(query2)))" after setting the model to query1. Unfortunately, the app sets the model to query2 right away instead of waiting 30 seconds like I thought it would. I feel like maybe Im just putting it in the wrong place? Or perhaps I am calling it incorrectly altogether?

            Heres my code now.

            @from PyQt4 import QtGui, QtCore, QtSql
            import sys

            db = QtSql.QSqlDatabase.addDatabase("QMYSQL")
            db.setHostName("localhost")
            db.setDatabaseName("db")
            db.setUserName("user")
            db.setPassword("pw")
            db.open()

            model=QtSql.QSqlTableModel()
            model.setTable("table")
            model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)

            query1 = QtSql.QSqlQuery("SELECT *FROM table ORDER BY column DESC LIMIT 10")
            query2 = QtSql.QSqlQuery("SSELECT *FROM table ORDER BY column LIMIT 10, 10")
            model.setQuery(query1)

            QtCore.QTimer.singleShot(30000, model, QtCore.SLOT(model.setQuery(query2)))
            app = QtGui.QApplication(sys.argv)
            form = QtGui.QTableView()
            form.setModel(model)
            form.show()
            sys.exit(app.exec_())
            @

            1 Reply Last reply
            0
            • T Offline
              T Offline
              techybri
              wrote on last edited by
              #6

              Anyone know how I might be able to refresh my tableview at a specific time interval?

              Also, I am sure I am missing something vital about classes and functions. since I havent defined either in the above and I see everyone else defining both all the time. Anyone know a good explanation of when I should be using classes and functions?

              Thanks all.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved