pyqt4 qTableWidget add items help

O

ookrin

I've been searching around the internet for an example of how to add a
list of items to the qTableWidget for the last few hours with little
success.

I have a list orders [[34,940,30,50,67], [50,56,35,30,57]] as my
example here

I built the qTableWidget in designer, so it already has the header
columns filled out.

Date | time | Number | Price | Buyer

ui.tb1_tblOrders.setRowCount(len(orders))

gives me the correct number of rows I want, but how do I fill the
rows?

I've been trying

while(len(orders)> i):
ui.tb1_tblOrders.setCurrentCell(i,0,orders[1])
i+=1

which to me, says go add in the first column row with the first order,
and it makes sense to me

It just says "Error: argument 3 of QTableWidget.setCurrenCell() has
invalid type, I know it's the orders, but I can't figure out what the
proper way of giving it what it wants is.
 
D

Diez B. Roggisch

ookrin said:
I've been searching around the internet for an example of how to add a
list of items to the qTableWidget for the last few hours with little
success.

I have a list orders [[34,940,30,50,67], [50,56,35,30,57]] as my
example here

I built the qTableWidget in designer, so it already has the header
columns filled out.

Date | time | Number | Price | Buyer

ui.tb1_tblOrders.setRowCount(len(orders))

gives me the correct number of rows I want, but how do I fill the
rows?

I've been trying

while(len(orders)> i):
ui.tb1_tblOrders.setCurrentCell(i,0,orders[1])
i+=1

which to me, says go add in the first column row with the first order,
and it makes sense to me

It just says "Error: argument 3 of QTableWidget.setCurrenCell() has
invalid type, I know it's the orders, but I can't figure out what the
proper way of giving it what it wants is.



I don't find setCurrentCell in the docs for QTableWidget - only for Q3Table.

http://doc.trolltech.com/4.0/q3table.html#setCurrentCell

However, that call isn't about setting the value of a cell, instead it's
about giving a cell the focus.

Use setItem instead.

And don't use the wicked while-loop for generating indices - this is
done in python using the enumerate-function:


for i, order in enumerate(orders):
...

Diez
 
O

ookrin

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

<ookrin – Samstag, 18. April 2009 11:58>

[...]> I've been trying
        while(len(orders)> i):
            ui.tb1_tblOrders.setCurrentCell(i,0,orders[1])
            i+=1

which to me, says go add in the first column row with the first order,
and it makes sense to me

Read the documentation [1] to learn, what ".setCurrentCell()" actually does
and what its arguments are!  And please stop this wild guessing ...

The method you're searching for is ".setItem()" [2], which adds a new
QTableWidgetItem [3] to a QTableWidget.

[1]http://doc.trolltech.com/4.5/qtablewidget.html#setCurrentCell
[2]http://doc.trolltech.com/4.5/qtablewidget.html#setItem
[3]http://doc.trolltech.com/4.5/qtablewidgetitem.html

- --
Freedom is always the freedom of dissenters.
                                      (Rosa Luxemburg)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (GNU/Linux)

iEYEARECAAYFAknpr4sACgkQGV4vxEMMOxdnawCfTXO55EffBJMQ7h91RGtMIpZ/
hcYAoLQ9yF5u/hBgNRvqxGRlIy5lPDgb
=Q6ef
-----END PGP SIGNATURE-----


I forgot to include what I tried first.
setCurrentItem was just the last thing I was on at the time.

First I filled out a row in Designer to see how it loads the items. It
loads like this:
self.tb1_tblOrders.item(0, 0).setText(QtGui.QApplication.translate
("MainWin", "Date", None, QtGui.QApplication.UnicodeUTF8))

Ok following that example:

ui.tb1_tblOrders.item(0,0).setText(QtGui.QApplication.translate
("MainWindow",order[0][1], None,QtGui.QApplication.UnicodeUTF8))

AttributeError: 'NoneType' object has no attribute 'setText'

So reading through the QTableWidget Doc I see the setItem

setItem(int,int, QTableWidgetItem * item)
QTableWidgetItem*item ???

The only example on the entire page:

QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
pow(row, column+1)));

is that setup as c++ ? Ok, it's a class.

twi = QTableWidgetItem()
newItem = twi(order[0])
ui.tb1_tb1Orders.setItem(i,0,newItem)

NameError: global name 'QTableWidgetItem' is not defined

At the top of the page
include <QTableWidgetItem>
from PyQt4 import ?? QtGui? QtCore? Those are already loaded other
options are pyqtconfig and uic and those don't sound correct...

And that's what I did first. Then I started guessing in the next
logical order, not wildly. editItem, setCurrentItem, setCurrentCell.
Then I came here. So I guess I actually stuck at the QTableWidgetItem
thing, I should have made that a little more clearer first post.

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top