Results 1 to 6 of 6

Thread: Strange behavior with QTableWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    672
    Thanks
    55
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Strange behavior with QTableWidget

    Hi Community,
    I'm having trouble with a QTableWidget that's part of a dialog.
    The table has 6 columns; The cells belonging to the first columns contains only text while the others contains a QComboBox widget (each combobox has 3 text items).
    In my code I need to detect which combobox widget was changed so I connected the cellChanged signal to my slot in this way:

    Qt Code:
    1. connect( tableBiasRadar_, SIGNAL(cellChanged(int, int)), this, SLOT(tableBiasRadar__cellChanged(int, int)));
    To copy to clipboard, switch view to plain text mode 

    And the relative slot:

    Qt Code:
    1. void OtrParametersDialogCtrl::tableBiasRadar__cellChanged(int row, int col)
    2. {
    3. std::cerr << "[DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChanged : Row: " << row << " Col: " << col << std::endl;
    4.  
    5. // If the column changed is the time offset, change all other modes of the radar
    6. if (static_cast<RadarBiasTableColums_>(col) != radarBiasTableTimeOffsetC_)
    7. {
    8. return;
    9. }
    10.  
    11. // Get the value set by the user
    12. QComboBox *item = qobject_cast<QComboBox*>(tableBiasRadar_->cellWidget(row, col));
    13. if (item == nullptr)
    14. {
    15. return;
    16. }
    17.  
    18. // Look for the radar that is being edited by this item
    19. const int radarId = tableBiasRadar_Column2RadarId_[row];
    20.  
    21. // Change the value of the combo box in all other rows that edit this item
    22. for (size_t i = 0; i < tableBiasRadar_Column2RadarId_.size(); ++i)
    23. {
    24. if (i != static_cast<size_t>(row) && tableBiasRadar_Column2RadarId_[i] == radarId)
    25. {
    26. QComboBox *itemToBeModified = qobject_cast<QComboBox*>(tableBiasRadar_->cellWidget(static_cast<int>(i), col));
    27. if (itemToBeModified != nullptr)
    28. {
    29. itemToBeModified->setCurrentIndex(item->currentIndex());
    30. }
    31. }
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    It doesn't matter which combobox I change, I always get the same output:

    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 0 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 1 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 2 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 3 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 4 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 5 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 6 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 7 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 8 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 9 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 10 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 11 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 12 Col: 0
    [DEBUG] OtrParametersDialogCtrl::tableBiasRadar__valueChan ged : Row: 13 Col: 0
    I would like to have a help in discovering why I always get the column 0 (the column 0 does not contain any combobox, only text) even when I change the combobox of other columns and why I get the output of all the rows also if I change only 1 row.

    The look&feel of the dialog looks correct, all the cells belonging to column 1 to 6 contains the combobox, so I really don't understand what's happening here. I am using 5.9.7, unfortunately this is a constraint.
    Could it be a bug in this Qt version?

    I hope to get help.
    Franco
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,110
    Thanks
    290
    Thanked 844 Times in 831 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Strange behavior with QTableWidget

    The table widget does not know anything about the cell widgets except that the cell has one. It does not respond to any signals from the cell widgets, and the cellChanged() signal is not being emitted in response to a change in the selection in the combo box. There is no connection between the cell widget and the table unless you make it. The cell widget is basically just a decoration unless you connect to its signals. I don't know what is triggering the cellChanged() signal.

    So, you have to keep track of which combobox is in which cell, implement slot(s) to handle and map the combo box selection changes to update the appropriate cells in your table.

    You should look at QItemDelegate / QStyledItemDelegate and example code using combo boxes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    672
    Thanks
    55
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange behavior with QTableWidget

    But when I change the combobox value it should trigger a change in the respective table cell or not?
    Franco Amato

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,110
    Thanks
    290
    Thanked 844 Times in 831 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Strange behavior with QTableWidget

    But when I change the combobox value it should trigger a change in the respective table cell or not?
    No, not automatically. The table widget does not know anything about the combo box except that it was told to store a QWidget in that cell. There is no automatic connection between the combo box selectionChanged() signal and some slot in the table widget.

    The QItemDelegate / QStyledItemDelegate classes provide a way to automate this - look at the example link I posted.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    franco.amato (10th January 2022)

  6. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    672
    Thanks
    55
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange behavior with QTableWidget

    Thank you for the useful reference.
    How can I initialize the tablewidget cells with the first item of the combobox ?

    Thank you
    Franco Amato

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,110
    Thanks
    290
    Thanked 844 Times in 831 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Strange behavior with QTableWidget

    How can I initialize the tablewidget cells with the first item of the combobox ?
    Are you using the delegate idea? If you are, then in the delegate's createEditor() method, you fill the combobox with the list of choices, right? Take that code and turn it into a method that can be called from outside the class, something like this:

    Qt Code:
    1. QStringList MyComboDelegate::initialContents( int row, int column ) const
    2. {
    3. QStringList contents;
    4. // fill the list
    5. return contents;
    6. }
    7.  
    8. QWidget * MyComboDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index )
    9. {
    10. QComboBox * editor = new QComboBox( parent );
    11. editor->addItems( initialContents( index.row(), index.column() );
    12.  
    13. // further initialization
    14. return editor;
    15. }
    To copy to clipboard, switch view to plain text mode 

    When you create the delegate to put into the tablewidget, call this method, then pull the first item from the list and use it initialize each of the cells in the column when you create the QTableWidgetItem instances.

    Alternatively, you could provide a function to set the combobox contents from outside the delegate:

    Qt Code:
    1. void MyComboDelegate::setContents( const QStringList & contents )
    2. {
    3. mContents = contents;
    4. }
    5.  
    6. QWidget * MyComboDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index )
    7. {
    8. QComboBox * editor = new QComboBox( parent );
    9. editor->addItems( mContents );
    10.  
    11. // further initialization
    12. return editor;
    13. }
    To copy to clipboard, switch view to plain text mode 

    and use that same list to initialize the table cells.

    There are other possibilities as well, depending on whether each combobox starts with the same contents or if each combobox can have a different set of choices, and also whether users can add to the list of choices or they are a fixed set. Qt is very flexible in the way you can use all of these classes together.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. The following user says thank you to d_stranz for this useful post:

    franco.amato (13th January 2022)

Similar Threads

  1. selectionModel() strange behavior
    By Str Anger in forum Newbie
    Replies: 3
    Last Post: 20th June 2016, 21:50
  2. strange behavior of qt 5.4
    By gisac in forum Qt Programming
    Replies: 4
    Last Post: 26th December 2015, 21:32
  3. strange behavior of QMapIterator
    By alainstgt in forum Qt Programming
    Replies: 3
    Last Post: 7th February 2015, 11:53
  4. Qt Creator Strange behavior of GDB in Creator 1.3
    By rayjc in forum Qt Tools
    Replies: 1
    Last Post: 11th March 2010, 21:42
  5. scrollbars strange behavior
    By siniy in forum Qt Programming
    Replies: 6
    Last Post: 29th December 2006, 10:27

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.