Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > pyqt4 qTableWidget add items help

Reply
Thread Tools

pyqt4 qTableWidget add items help

 
 
ookrin
Guest
Posts: n/a
 
      04-18-2009
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[i][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.
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      04-18-2009
ookrin schrieb:
> 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[i][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...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
 
Reply With Quote
 
 
 
 
ookrin
Guest
Posts: n/a
 
      04-18-2009
On Apr 18, 3:46*am, Sebastian Wiesner <(E-Mail Removed)> wrote:
> -----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[i][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)
>
> iEYEARECAAYFAknpr4sACgkQGV4vxEMMOxdnawCfTXO55EffBJ MQ7h91RGtMIpZ/
> 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.UnicodeUTF)

Ok following that example:

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

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[i][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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PyQt4.__file__ gives PyQt4/__init__.py as value wgw Python 1 08-15-2009 09:58 PM
[PyQt4] QTableWidget non editable Antonio Valentino Python 1 06-22-2008 03:37 PM
Re: PyQT / QDate / QTableWidget David Boddie Python 1 03-31-2008 10:02 PM
Setting the value of one cell in QTableWidget fills everything. Constantly Distracted Python 3 03-29-2008 08:46 AM
PyQT / QDate / QTableWidget wrightee Python 0 03-26-2008 02:13 PM



Advertisments