Results 1 to 4 of 4

Thread: QTableWidget - problem when editing multiple items

  1. #1
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableWidget - problem when editing multiple items

    Hi,

    The QTableWidget is not notifying a change when an item is "changed" for the same value.
    It seems silly, but it's important when editing multiple items.

    Qt Code:
    1. QTableWidgetTest::QTableWidgetTest(QWidget *parent, Qt::WFlags flags)
    2. : QDialog(parent, flags)
    3. {
    4. ui.setupUi(this);
    5. //...
    6. connect(ui.tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(multipleItemsChanged(QTableWidgetItem*)));
    7. }
    8.  
    9. //...
    10.  
    11. void QTableWidgetTest::multipleItemsChanged( QTableWidgetItem *item )
    12. {
    13. ui.tableWidget->blockSignals(true);
    14. QList<QTableWidgetItem*> selectedItems = ui.tableWidget->selectedItems();
    15. foreach(QTableWidgetItem* selectItem, selectedItems)
    16. {
    17. selectItem->setText(item->text());
    18. }
    19. ui.tableWidget->blockSignals(false);
    20. }
    To copy to clipboard, switch view to plain text mode 

    Sample project
    It's important to use the same value as the last item selected.
    If you test with only one item, the result is the same.


    Does somebody know another way to solve this?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTableWidget - problem when editing multiple items

    Try connecting the signal QAbstractItemDelegate::closeEditor(QWidget *) to your slot. You'll need to change the signature of the slot and cast "QWidget *" to "QLineEdit *" to get the text.

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

    xportation (20th October 2011)

  4. #3
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget - problem when editing multiple items

    Thanks norobro, it works fine and I'm using it.
    I have tested another way by making this little "good blood" hack:

    Qt Code:
    1. class MyTableWidgetItem : public QTableWidgetItem
    2. {
    3. public:
    4. explicit MyTableWidgetItem(const QString & text, int type = Type) : QTableWidgetItem(text, type) {}
    5.  
    6. virtual void setData(int role, const QVariant &value)
    7. {
    8. if (QAbstractTableModel *model = (this->tableWidget() ? qobject_cast<QAbstractTableModel*>(this->tableWidget()->model()) : 0)) {
    9. model->blockSignals(true);
    10. QVariant nullValue;
    11. QTableWidgetItem::setData(role,nullValue);
    12. model->blockSignals(false);
    13. }
    14. QTableWidgetItem::setData(role,value);
    15. }
    16. };
    17.  
    18. //--
    19.  
    20. QTableWidgetTest::QTableWidgetTest(QWidget *parent, Qt::WFlags flags)
    21. : QDialog(parent, flags)
    22. {
    23. ui.setupUi(this);
    24.  
    25. int value= 0;
    26. for (int row= 0; row < table->rowCount(); row++) {
    27. for (int col= 0; col < table->columnCount(); col++) {
    28. QTableWidgetItem *item= new MyTableWidgetItem(QString::number(value));
    29. table->setItem(row,col,item);
    30. value++;
    31. }
    32. }
    33.  
    34. connect(ui.tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(multipleItemsChanged(QTableWidgetItem*)));
    35. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default [Solved] Re: QTableWidget - problem when editing multiple items

    [Solved]
    this thread is solved

Similar Threads

  1. Replies: 2
    Last Post: 21st September 2011, 06:32
  2. QT Tree Widget - Issues editing items
    By epsilonorion in forum Qt Programming
    Replies: 0
    Last Post: 7th July 2011, 15:19
  3. Problem loading multiple GIF images in qtablewidget.
    By sanket.mehta in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2010, 10:31
  4. Problem editing TreeWidget items
    By Moezzie in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2007, 21:22
  5. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46

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.