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
self.contactsTableWidget.setRowCount(rowcount)add something likeprint "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. - PhotonQSqlQueryModel(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 thesqliteconnection yourself. - rainer