View Full Version : [SOLVED] PyQT4 pulling data from QTableWidget to QLineEdit

April 9th, 2011, 08:05 AM
Hi all, I'm having a weird issue where I can't pull data from a table that I populated via mysql. Here's the thing:

1. I have a QTableWidget that has 8 columns (1 unused for now)
2. I click on load and it queries and loads the data into my QTableWidget via MySQL

conn = mdb.connect('192.168.x.x', 'xxxxxxxx', 'xxxxxxxx', 'computerrepair')
cursor = conn.cursor()

cursor.execute("SELECT * FROM comprep WHERE date='"+str(self.calendarWidget.selectedDate().toString("MM/dd/yyyy"))+"'")

rows = cursor.fetchall()

for row in rows:
self.tableWidget.insertRow(self.tableWidget.rowCou nt())
itm1 = QtGui.QTableWidgetItem(str(row[0]), 0)
self.tableWidget.setItem(self.tableWidget.rowCount ()-1, 1, itm1)
itm2 = QtGui.QTableWidgetItem(row[1], 0)
self.tableWidget.setItem(self.tableWidget.rowCount ()-1, 2, itm2)
itm2 = QtGui.QTableWidgetItem(row[2], 0)
self.tableWidget.setItem(self.tableWidget.rowCount ()-1, 3, itm2)
itm2 = QtGui.QTableWidgetItem(row[3], 0)
self.tableWidget.setItem(self.tableWidget.rowCount ()-1, 4, itm2)
itm2 = QtGui.QTableWidgetItem(row[4], 0)
self.tableWidget.setItem(self.tableWidget.rowCount ()-1, 5, itm2)
itm2 = QtGui.QTableWidgetItem(row[5], 0)
self.tableWidget.setItem(self.tableWidget.rowCount ()-1, 6, itm2)
itm2 = QtGui.QTableWidgetItem(row[6], 0)
self.tableWidget.setItem(self.tableWidget.rowCount ()-1, 7, itm2)


3. I click on a cell to get the row (which works, the row # changes) and try to load that value into a cell via:

def pullToFields(self):
if self.tableWidget.rowCount() > 0:
item = self.tableWidget.itemAt(self.tableWidget.currentRo w(), 1)
print item
print "Load Data First"

I tried doing self.tableWidget.itemAt(self.tableWidget.currentRo w(), 1).text() and .toString()

But it comes back as NONE.

What am I missing?

April 9th, 2011, 05:44 PM
itemAt() expects the x, y coordinates of the mouse cursor. You want the item() method.

itemText = self.tableWidget.item(self.tableWidget.currentRow( ), 1).text()

April 9th, 2011, 08:42 PM
Thank you that worked perfectly! I'm really liking PyQt4 it's awesome!


September 24th, 2012, 07:52 AM
My problem is that I want to join MySQL data base with Widgets in PyQt4, and, I want to be able for editing, deleting and adding of information on it and show them on this widgets.
May I ask you, if possible, send to me the related codes and some examples.