Results 1 to 14 of 14

Thread: QTableWidget+QTableWidgetItem trouble

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget+QTableWidgetItem trouble

    Quote Originally Posted by jpn View Post
    No, that shouldn't leak memory since QTableWidget deletes the old item if you set a new item into cell. But you definitely don't want to re-create all the items every time in setTableData(). Create the items once in constructor or so, and just set the data in setTableData():
    Qt Code:
    1. QTableWidgetItem* item = ui.tableWidget->item(row, col);
    2. item->setSomething(...);
    To copy to clipboard, switch view to plain text mode 
    Hmm... not work

    Qt Code:
    1. class HSM : public QDialog
    2. {
    3. Q_OBJECT
    4. ...
    5. ...
    6. ...
    7. private:
    8. QTableWidgetItem* itemArchive;
    9. QTableWidgetItem* itemRestore;
    10. QTableWidgetItem* itemDelete;
    11. ...
    12. ...
    13. ...
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. HSM::HSM(QWidget *parent, Qt::WFlags flags)
    2. : QDialog(parent, flags)
    3. {
    4.  
    5. ...
    6. ...
    7. ...
    8. itemArchive = new QTableWidgetItem(tr("Archive"));
    9. itemRestore = new QTableWidgetItem(tr("Restore"));
    10. itemDelete = new QTableWidgetItem(tr("Delete"));
    11. ...
    12. ...
    13. ...
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void HSM::setTableData()
    2. {
    3. sTskInfoGUI sTaskGUI;
    4. db_work db;
    5.  
    6. sTaskGUI = db.GetTaskToGUI();
    7. db.CloseDB();
    8.  
    9. for (int row = 0 ; row < sTaskGUI.nCmd.size() ;++row)
    10. {
    11.  
    12. if (sTaskGUI.nCmd.at(row) == 1)
    13. {
    14. itemArchive = ui.tableWidget->item(row,0);
    15. itemArchive->setIcon(QIcon(QPixmap(":/HSM/Resources/archive.png")));
    16. itemArchive->setBackground(QColor::fromRgb(200,255,104,100));
    17. //ui.tableWidget->setItem(row,0,itemArchive);
    18.  
    19. }
    20. if (sTaskGUI.nCmd.at(row) == 2)
    21. {
    22. itemRestore = ui.tableWidget->item(row,0);
    23. itemRestore->setIcon(QIcon(QPixmap(":/HSM/Resources/restore.png")));
    24. itemRestore->setBackground(QColor::fromRgb(115,150,255,100));
    25. //ui.tableWidget->setItem(row,0,itemRestore);
    26. }
    27. if (sTaskGUI.nCmd.at(row) == 3)
    28. {
    29. itemDelete = ui.tableWidget->item(row,0);
    30. itemDelete->setIcon(QIcon(QPixmap(":/HSM/Resources/del.png")));
    31. itemDelete->setBackground(QColor::fromRgb(255,100,120,100));
    32. //ui.tableWidget->setItem(row,0,itemDelete);
    33. }
    34. ...
    35. ...
    36. ...
    To copy to clipboard, switch view to plain text mode 



    Exception in code :
    d:\qt4\src\gui\itemviews\qtablewidget.cpp
    Qt Code:
    1. inline void QTableWidgetItem::setIcon(const QIcon &aicon)
    2. { setData(Qt::DecorationRole, aicon); }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget+QTableWidgetItem trouble

    QTableWidget::item() returns 0 which means there is no item in such cell and so you are dereferencing a null pointer. Did you actually call QTableWidget::setItem() anywhere?
    Qt Code:
    1. itemArchive = new QTableWidgetItem(tr("Archive"));
    2. itemRestore = new QTableWidgetItem(tr("Restore"));
    3. itemDelete = new QTableWidgetItem(tr("Delete"));
    4. ui.tableWidget->setItem(0,0,itemArchive); // <--
    5. ui.tableWidget->setItem(1,0,itemRestore); // <--
    6. ui.tableWidget->setItem(2,0,itemDelete); // <--
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget+QTableWidgetItem trouble

    Quote Originally Posted by jpn View Post
    QTableWidget::item() returns 0 which means there is no item in such cell and so you are dereferencing a null pointer. Did you actually call QTableWidget::setItem() anywhere?
    Qt Code:
    1. itemArchive = new QTableWidgetItem(tr("Archive"));
    2. itemRestore = new QTableWidgetItem(tr("Restore"));
    3. itemDelete = new QTableWidgetItem(tr("Delete"));
    4. ui.tableWidget->setItem(0,0,itemArchive); // <--
    5. ui.tableWidget->setItem(1,0,itemRestore); // <--
    6. ui.tableWidget->setItem(2,0,itemDelete); // <--
    To copy to clipboard, switch view to plain text mode 
    no effect

    Exception in code :
    d:\qt4\src\gui\itemviews\qtablewidget.cpp

    Qt Code:
    1. inline void QTableWidgetItem::setIcon(const QIcon &aicon){ setData(Qt::DecorationRole, aicon); }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget+QTableWidgetItem trouble

    There are 3 places in your code in which you pick an item at certain cell:
    Qt Code:
    1. ...
    2. itemArchive = ui.tableWidget->item(row,0);
    3. ...
    4. itemRestore = ui.tableWidget->item(row,0);
    5. ...
    6. itemRestore = ui.tableWidget->item(row,0);
    7. ...
    To copy to clipboard, switch view to plain text mode 
    On the other hand, you have created an item only into cells (0,0), (1,0) and (2,0). If "row" is anything else than 0-2, you'll get a crash. This is most likely the case of yours.
    J-P Nurmi

  5. #5
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget+QTableWidgetItem trouble

    Ok Thx for you help

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget+QTableWidgetItem trouble

    Let me correct one thing. I just re-read my previous message and I want to make sure I don't get misundestood. Of course, calling QTableWidget::item() with any possible parameters doesn't cause a crash:
    Qt Code:
    1. QTableWidgetItem* item = tableWidget->item(1234, 5678);
    To copy to clipboard, switch view to plain text mode 
    But the point is that in case no item exists in such cell, QTableWidget::item() returns 0. Then, dereferencing a null pointer is what causes the crash:
    Qt Code:
    1. item->setSomething();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. #7
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget+QTableWidgetItem trouble

    How to make so that last column it was leveled by a right edge of the table ?

    Qt Code:
    1. void HSM::setTableData()
    2. {
    3. sTskInfoGUI sTaskGUI;
    4. db_work db;
    5.  
    6. sTaskGUI = db.GetTaskToGUI();
    7. db.CloseDB();...
    8. ...
    9. ...
    10. itemRootPatch->setText(sTaskGUI.cRootPath.at(row));
    11. ui.tableWidget->setItem(row,9,itemRootPatch);
    12. }
    13.  
    14. ui.tableWidget->resizeRowsToContents();
    15. ui.tableWidget->resizeColumnsToContents();
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    Here so my form looks.
    Attached Images Attached Images

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget+QTableWidgetItem trouble

    You can either use QHeaderView::setResizeMode() and pass QHeaderView::Stretch:
    Qt Code:
    1. ui.tableWidget->horizontalHeader()->setResizeMode(10, QHeaderView::Stretch);
    To copy to clipboard, switch view to plain text mode 
    Or you can use a "shortcut" (which QTreeView uses by default):
    Qt Code:
    1. ui.tableWidget->horizontalHeader()->setStretchLastSection(true);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. #9
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget+QTableWidgetItem trouble

    many Thx !

Similar Threads

  1. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 23:29
  2. QTableWidget won't sort cellwidgets!!!
    By Arsenic in forum Qt Programming
    Replies: 7
    Last Post: 21st July 2007, 10:41
  3. A QListWidget in a QTableWidget cell ?
    By Nyphel in forum Newbie
    Replies: 4
    Last Post: 11th April 2007, 10:46
  4. QTableWidget (resizing rows, turning off selection, etc.)
    By kiss-o-matic in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2007, 01:57
  5. Problem inserting in QTableWidget
    By DPinLV in forum Qt Programming
    Replies: 2
    Last Post: 2nd August 2006, 00:10

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.