pyqt4 qTableWidget add items help

Discussion in 'Python' started by ookrin, Apr 18, 2009.

  1. ookrin

    ookrin Guest

    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.
     
    ookrin, Apr 18, 2009
    #1
    1. Advertising

  2. 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[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
     
    Diez B. Roggisch, Apr 18, 2009
    #2
    1. Advertising

  3. ookrin

    ookrin Guest

    On Apr 18, 3:46 am, Sebastian Wiesner <> 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[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
     
    ookrin, Apr 18, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wrightee

    PyQT / QDate / QTableWidget

    wrightee, Mar 26, 2008, in forum: Python
    Replies:
    0
    Views:
    614
    wrightee
    Mar 26, 2008
  2. David Boddie

    Re: PyQT / QDate / QTableWidget

    David Boddie, Mar 28, 2008, in forum: Python
    Replies:
    1
    Views:
    954
    wrightee
    Mar 31, 2008
  3. Constantly Distracted
    Replies:
    3
    Views:
    722
    Constantly Distracted
    Mar 29, 2008
  4. Antonio Valentino

    [PyQt4] QTableWidget non editable

    Antonio Valentino, Jun 22, 2008, in forum: Python
    Replies:
    1
    Views:
    461
    Antonio Valentino
    Jun 22, 2008
  5. wgw
    Replies:
    1
    Views:
    466
Loading...

Share This Page