PDA

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



slooksterpsv
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


try:
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)
self.tableWidget.update()

cursor.close()
conn.close()

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):
print "PULL TO FIELDS"
if self.tableWidget.rowCount() > 0:
#self.crName.setText(self.tableWidget.)
item = self.tableWidget.itemAt(self.tableWidget.currentRo w(), 1)
#self.crName.setText(item.text())
print item
else:
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?

benson444
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()

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

:KS:KS:KS:KS:KS

paraxhitman
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.