Results 1 to 5 of 5

Thread: Properly using QTableWidget and QTableWidgetItem

  1. #1
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Properly using QTableWidget and QTableWidgetItem

    I am having a QMap through which I iterate. I would like to display the keys and values in my QTableWidget.


    Qt Code:
    1. int counter=0;
    2. QMapIterator<QChar,int> iter(ArithmeticCoding::letters_freq);
    3. while(iter.hasNext()){
    4. iter.next();
    5. QTableWidgetItem *item0=new QTableWidgetItem(iter.key());
    6. ui->table->setItem(counter,0,item0);
    7.  
    8. QTableWidgetItem *item1=new QTableWidgetItem(iter.value());
    9. ui->table->setItem(counter,1,item1);
    10. counter++;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Of course here my pointers to QTableWidgetItem get lost resulting in random garbage in my table. What would be the proper way of using them? Should I save them to an additional array? That seems like an overhead to me.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Properly using QTableWidget and QTableWidgetItem

    What do you mean "get lost"? You allocate on the heap and the table take ownership of them. To use those later you use QTableWidget::item to get a pointer to the QTableWidgetItem in a row and column.

    Anyway, do you actually get random (garbage) values in the table?

  3. #3
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Properly using QTableWidget and QTableWidgetItem

    Zlatomir thanks for answering.

    Your point about table taking ownership is correct, thanks for explaining. However, yes, I get random string garbage in in second cell (for iter.value()). Specifically, I get text from labels on my window instead of QMap values. Yes, strange. I've debugged and the values in QMap are correct so I was guessing something has to be wrong with pointers. A screenshot:

    wxhoX.jpg



    If I put in some string to second QTableWidgetItem it displays it well. Also, I've tested the iter.value() values (qDebug() << iter.value();) and they are correct. Maybe I have to cast int to String in some special way?

    ** MAJOR EDIT:
    I was able to fill the second cell properly using
    QTableWidgetItem *item1=new QTableWidgetItem(tr("%1").arg(iter.value()));

    Why Did I have to do this it's still a mystery to me
    Last edited by c0dehunter; 27th October 2012 at 16:37.

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Properly using QTableWidget and QTableWidgetItem

    You need to pass a QString there, use the static number to convert an int to QString and most likely you will need toInt too (to convert from QString to int).
    so use this code:
    Qt Code:
    1. //...
    2. QTableWidgetItem *item1=new QTableWidgetItem( QString::number(iter.value()) );
    To copy to clipboard, switch view to plain text mode 
    The problem seems to be that integet, because the QTableWidget has an constructor that takes an int as some "type" (i don't know what that means): doc link

  5. The following user says thank you to Zlatomir for this useful post:

    c0dehunter (27th October 2012)

  6. #5
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Properly using QTableWidget and QTableWidgetItem

    Thanks, this also worked!

Similar Threads

  1. Replies: 1
    Last Post: 10th January 2012, 22:59
  2. QTableWidgetItem for a QTableWidget
    By Archa4 in forum Newbie
    Replies: 1
    Last Post: 28th April 2011, 12:11
  3. QTableWidget or QTableWidgetItem CSS
    By ajayajgdeva in forum Newbie
    Replies: 0
    Last Post: 5th February 2010, 14:47
  4. QTableWidget+QTableWidgetItem trouble
    By Fastman in forum Qt Programming
    Replies: 13
    Last Post: 10th November 2007, 13:19
  5. QTableWidget QTableWidgetItem
    By TheKedge in forum Qt Programming
    Replies: 3
    Last Post: 6th September 2006, 16:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.