4

My objective is to update the QTableQidget when new contacts are added. I have no problem adding the contacts or them showing up in the QTableWidget. My problem is that I have to exit out the program and then start it up again in order to see the newly added contacts. Is their a way to update or refresh the QTableWidget to show new contacts when they are added to the database without having to quit the program. I've tried update() and repaint and nothing changes.

class BrowseContacts(QtGui.QWidget):
    #Display New Contacts Widget
    def __init__(self):
        super(BrowseContacts, self).__init__()
        self.initUI()
        self.contactsData()


    #User Interface
    def initUI(self):
        self.new_layout = QtGui.QGridLayout()

        self.contactsTableWidget = QtGui.QTableWidget()
        self.contactsTableWidget.setColumnCount(10)

        self.contacts_label = ['First Name', 'Last Name', 'Home Phone', 'Cell Phone', 'Business Name', 'Email Address', 'Address', 'City', 'State', 'Zip Code']
        self.contactsTableWidget.setHorizontalHeaderLabels(self.contacts_label)
        self.contactsTableWidget.setSortingEnabled(True)

        self.new_layout.addWidget(self.contactsTableWidget)
        self.setLayout(self.new_layout)

        self.setStyleSheet('QTableWidget::item {background-color: #ffffff; color: #000000}' 'QTableWidget::item:selected {background-color: #3aa8ad; color: #ffffff;}')

    def contactsData(self):
        #Connect to Database
        connection = sqlite3.connect('contacts.db')
        cur = connection.cursor()

        rowcount = cur.execute('''SELECT COUNT(*) FROM contacts''').fetchone()[0]

        self.contactsTableWidget.setRowCount(rowcount)
        cur.execute('''SELECT  * FROM contacts''')

        for row, contacts in enumerate(cur):
            for column, contact in enumerate(contacts):
                self.contactsTableWidget.setItem(row, column, QtGui.QTableWidgetItem(str(contact)))

        cur.close()
        connection.commit()
        connection.close()

Thank You

irunintotrees
  • 45
  • 1
  • 1
  • 3
  • Doesn't look like something is missing. Try to add some prints to verify that the function actually runs, and the number of rows of data actually changes – Photon Nov 30 '14 at 11:18
  • Thank You. Can you give me a hint on how I go about doing that. What do I print out and where would I put the print function. Sorry if that's a stupid question. I'm just a little confused. – irunintotrees Nov 30 '14 at 11:47
  • before the line `self.contactsTableWidget.setRowCount(rowcount)` add something like `print "Setting rowcount to {}".format(rowcount)`. It will show that the function is called and sets the new number of rows. If you don't see the print, or the number is not changed, you'll know that the problem is elsewhere. Hopefully it would be something like that. – Photon Nov 30 '14 at 12:03
  • 4
    How about using a `QSqlQueryModel` (http://qt-project.org/doc/qt-4.8/sql-model.html)? This should take care of all those things for you, and, at the same time, make your life easier since you wouldn't have to deal with the `sqlite` connection yourself. – rainer Nov 30 '14 at 13:35
  • Thanks. I will try both of your suggestions. – irunintotrees Nov 30 '14 at 20:44
  • i have the same problem how did you solve it ? – Lucky Aug 24 '17 at 08:18

1 Answers1

3

You call contactsData() once, and it fills qtablewidget with data from database. If you want to refresh qtablewidgetyou need to call that method again. You could create pushbutton and connect it to contactsData(), so when you press that button, contacts are reloaded from your database without having to quit the program.

If you need it to be done automatically you can create timer to call contactsData().

EDIT

just add this line at the end of contactsData() method:

QtCore.QTimer.singleShot(10000, self.contactsData)

it will reload the data every 10 seconds

Aleksandar
  • 3,541
  • 4
  • 34
  • 57