Results 1 to 14 of 14

Thread: QTableWidget+QTableWidgetItem trouble

  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 QTableWidget+QTableWidgetItem trouble

    code(QT 4.3.1):

    Qt Code:
    1. void HSM::setTable()
    2. {
    3.  
    4. ui.tableWidget->setRowCount(30);
    5. ui.tableWidget->setColumnCount(10);
    6.  
    7. ui.tableWidget->setHorizontalHeaderLabels(
    8. QStringList() << tr("Task")
    9. << tr("Time")
    10. << tr("Group")
    11. << tr("Object")
    12. << tr("File")
    13. << tr("ID_OBJ")
    14. << tr("State")
    15. << tr("Prc")
    16. << tr("ID_SYS")
    17. << tr("RootPath"));
    18.  
    19. Items = new QTableWidgetItem(tr("Task"),QTableWidgetItem::Type);
    20. Items->setIcon(QIcon(QPixmap(":/HSM/Resources/Add.png")));
    21.  
    22. ui.tableWidget->resizeRowsToContents();
    23. ui.tableWidget->resizeColumnsToContents();
    24. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void HSM::setTableData()
    2. {
    3.  
    4. for (int row = 0 ; row < ui.tableWidget->rowCount() ;++row)
    5. {
    6. ui.tableWidget->setItem(row,0,Items);
    7. }
    8. ui.tableWidget->resizeRowsToContents();
    9. ui.tableWidget->resizeColumnsToContents();
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 


    All well works but at closing the app the mistake jumps out.

    The mistake occurs here:

    d:\qt4\src\gui\itemviews\qtablewidget.cpp

    Qt Code:
    1. void QTableModel::clearContents()
    2. {
    3. for (int i = 0; i < tableItems.count(); ++i) {
    4. if (tableItems.at(i)) {
    5. tableItems.at(i)->view = 0;
    6. delete tableItems.at(i); <--ERROR
    7. tableItems[i] = 0;
    8. }
    9. }
    10. reset();
    11. }
    To copy to clipboard, switch view to plain text mode 

    and tableItems.count() = 300. (10*30 ????)
    but i'am have only 30 items !!!

    Where at me a mistake. Also what it is necessary to make to correct a code?

  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

    Yes, you do actually have 30*10=300 items. The mistake is that you set the same single item into every cell. You should create a separate item into each cell:
    Qt Code:
    1. void HSM::setTableData()
    2. {
    3.  
    4. for (int row = 0 ; row < ui.tableWidget->rowCount() ;++row)
    5. {
    6. QTableWidgetItem* item = new QTableWidgetItem(tr("Task"));
    7. item->setIcon(QIcon(QPixmap(":/HSM/Resources/Add.png")));
    8. ui.tableWidget->setItem(row,0,item);
    9. }
    10. ui.tableWidget->resizeRowsToContents();
    11. ui.tableWidget->resizeColumnsToContents();
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    Fastman (7th November 2007)

  4. #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

    i'am stupid
    Thx !

  5. #4
    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
    Yes, you do actually have 30*10=300 items. The mistake is that you set the same single item into every cell. You should create a separate item into each cell:
    Qt Code:
    1. void HSM::setTableData()
    2. {
    3.  
    4. for (int row = 0 ; row < ui.tableWidget->rowCount() ;++row)
    5. {
    6. QTableWidgetItem* item = new QTableWidgetItem(tr("Task"));
    7. item->setIcon(QIcon(QPixmap(":/HSM/Resources/Add.png")));
    8. ui.tableWidget->setItem(row,0,item);
    9. }
    10. ui.tableWidget->resizeRowsToContents();
    11. ui.tableWidget->resizeColumnsToContents();
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 
    Whether there will be memory leak if function setTableData () is caused in me each 5 seconds for display of changes in the table?

  6. #5
    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

    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 
    J-P Nurmi

  7. #6
    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 

  8. #7
    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

  9. #8
    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 

  10. #9
    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

  11. #10
    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

  12. #11
    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

  13. #12
    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

  14. #13
    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

  15. #14
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.