Results 1 to 8 of 8

Thread: Problem with new QTableWidgetItem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with new QTableWidgetItem

    An error occurs when running this:

    My h:
    Qt Code:
    1. QTableWidgetItem* itemTable[];
    To copy to clipboard, switch view to plain text mode 

    My cpp:
    Qt Code:
    1. conTable = 0;
    2. var1 = 0;
    3. itemTable[conTable] = new QTableWidgetItem("text1");
    4. ui->tableWidget->setItem(var1,0,itemTable[conTable]);
    5. conTable++;
    6. itemTable[conTable] = new QTableWidgetItem("text2");
    7. ui->tableWidget->setItem(var1,1,itemTable[conTable]);
    8. conTable++;
    To copy to clipboard, switch view to plain text mode 

    What's wrong in making a new in QTableWidgetItem?

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Problem with new QTableWidgetItem

    Normal, your
    QTableWidgetItem* itemTable[];
    is something equivalent to QTableWidgetItem **itemTable.
    But you do the allocation of QTableWidgetItem but not it's address.

    Seriously this is C code, use Qt containers instead, look to QVector or QList. Something like
    QVector<QTableWidgetItem *> itemTable;
    should be fine.

  3. #3
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with new QTableWidgetItem

    I don't understand.

    I use the normal QTreeWidgetItem and it works.

    Qt Code:
    1. itemTree[num] = new QTreeWidgetItem(ui->treeWidget);
    2. itemTree[num]->setText(0,Tag[num]->nome);
    3. num++;
    To copy to clipboard, switch view to plain text mode 

    Use vector will be a problem.
    I want use as a pointer and create indefinitely to fill a table.

  4. #4
    Join Date
    Jul 2011
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with new QTableWidgetItem

    you may have to format it more like this so that you can access the item later.

    Qt Code:
    1. ui.table->setItem( row , 0, new QTableWidgetItem() );
    2. ui.table->item(row , 0 )->setText(QString( "%1" ).arg( num ) );
    3. ui.table->item(row, 0 )->setBackgroundColor( QColor(178,34,34) );
    To copy to clipboard, switch view to plain text mode 

    Something I found out after hours of invalid pointer access

  5. #5
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with new QTableWidgetItem

    Perfect.
    That's what I needed.

    Thanks.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with new QTableWidgetItem

    Where do you allocate space to hold the array of QTableWidgetItem pointers? If you never do that then this a recipe for serious crashing. Using a QList<QTableWidgetItem*> or QVector<QTableWidgetItem*> is a far safer option.

  7. #7
    Join Date
    Jul 2011
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with new QTableWidgetItem

    Chris was that to me?

    They get held into the QTableWidget, I just reference them in the row and col of the table ( item( row, col) ), for my application they work fine, no issues, and no risk. They only get replaced when i say so.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with new QTableWidgetItem

    No, it was for the OP.

Similar Threads

  1. QTableWidgetItem
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 11th August 2011, 06:35
  2. QTableWidgetItem
    By weixj2003ld in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2011, 07:59
  3. QTableWidgetItem editing problem
    By fmariusd in forum Qt Programming
    Replies: 8
    Last Post: 4th September 2009, 08:10
  4. Problem with QTableWidgetItem
    By arunvv in forum Newbie
    Replies: 2
    Last Post: 9th May 2008, 00:04
  5. Replies: 3
    Last Post: 11th August 2007, 10:00

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
  •  
Qt is a trademark of The Qt Company.