Results 1 to 6 of 6

Thread: Problems in understanding how to use a QComboBox in my QTableView derived class

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

    Default Problems in understanding how to use a QComboBox in my QTableView derived class

    Hi community,
    in my code I have added a view derived from QTableView having 2 columns and x rows ( x is not fixed but grows ).
    The first column contains text and there I show the content of the model that stores internally a string of strings.
    The second columns have show a QComboBox where the user should be able to choose one of the combobox options (also a QString). I am using a class derived from QStyledItemDelegate for that purpose.
    I have no problems with the first column, I can correctly add new rows with the right text.
    My problem is with the column #2, I can show the combobox with the options but, after choosing one, if I click anywhere in the view, the combobox value clears.
    So seems I am doing something wrong in storing and read the corect combobox value.

    Here the code I used to create the item delegate

    *.h

    Qt Code:
    1. #ifndef SELECTEDLISTITEMDELEGATE_H
    2. #define SELECTEDLISTITEMDELEGATE_H
    3.  
    4. #include <QStyledItemDelegate>
    5.  
    6. class SelectedListItemDelegate : public QStyledItemDelegate
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. SelectedListItemDelegate(QObject *parent = nullptr);
    12. ~SelectedListItemDelegate() override;
    13.  
    14. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    15. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    16. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
    17. };
    18.  
    19. #endif
    To copy to clipboard, switch view to plain text mode 

    and the cpp

    Qt Code:
    1. #include "SelectedListItemDelegate.h"
    2. #include <QComboBox>
    3. #include <QDebug>
    4.  
    5. SelectedListItemDelegate::SelectedListItemDelegate(QObject* parent)
    6. : QStyledItemDelegate(parent)
    7. {
    8. }
    9.  
    10. SelectedListItemDelegate::~SelectedListItemDelegate()
    11. {
    12. }
    13.  
    14. QWidget *SelectedListItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    15. {
    16. Q_UNUSED(option)
    17.  
    18. QComboBox *comboBox = new QComboBox(parent);
    19.  
    20. // I considere 3 options just as an example
    21. const int row = index.row();
    22. comboBox->addItem(QString("Option %1").arg(row));
    23. comboBox->addItem(QString("Option %1").arg(row));
    24. comboBox->addItem(QString("Option %1").arg(row));
    25.  
    26. return comboBox;
    27. }
    28.  
    29. // This routine does not set any value in the combobox
    30. void SelectedListItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    31. {
    32. QString value = index.model()->data(index, Qt::EditRole).toString();
    33. qDebug() << "Value:" << value; // Value is empty
    34. QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
    35. comboBox->setCurrentIndex(comboBox->findText(value));
    36. }
    37.  
    38. // Seems that the combobox value is not correctly written because is not kept in the cell. If I click outside its cleared
    39. void SelectedListItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    40. {
    41. QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
    42. QString value = comboBox->currentText();
    43. model->setData(index, value, Qt::EditRole);
    44. }
    To copy to clipboard, switch view to plain text mode 

    Can I have a help in understanding what's I am doing wrong?
    Thanx
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems in understanding how to use a QComboBox in my QTableView derived class

    Can I have a help in understanding what's I am doing wrong?
    In your model's setData() method, are you issuing the signals to tell views that the data has changed? QAbstractItemModel::dataChanged(), QAbstractItemModel::layoutAboutToBeChanged(), QAbstractItemModel::layoutChanged() or the protected methods when rows / columns are added or removed?

    If the model does not send out the signals to tell views that its content has changed, then the view does not know it has to update. And when editing is finished, the delegate is destroyed along with whatever it was showing on screen. So the view remains the same as it was before editing.
    <=== 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
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems in understanding how to use a QComboBox in my QTableView derived class

    Thank you for the answer.
    Can I have a piece of code to better understand?
    Franco Amato

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems in understanding how to use a QComboBox in my QTableView derived class

    Can I have a piece of code to better understand?
    It would be better if you posted the code you have in your model's setData() method. That's where whatever needs to be done to notify views of a change in the model has to occur.
    <=== 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. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems in understanding how to use a QComboBox in my QTableView derived class

    Quote Originally Posted by d_stranz View Post
    It would be better if you posted the code you have in your model's setData() method. That's where whatever needs to be done to notify views of a change in the model has to occur.
    Below the code of setData()

    Qt Code:
    1. bool SelectedListModel::setData(const QModelIndex &index, const QVariant &value, int role)
    2. {
    3. if (role == Qt::EditRole && index.isValid())
    4. {
    5. m_reports[index.row()].replace("actionName", value.toString());
    6.  
    7. emit dataChanged(index, index, {role});
    8. return true;
    9. }
    10.  
    11. return false;
    12. }
    To copy to clipboard, switch view to plain text mode 

    And data()

    Qt Code:
    1. QVariant SelectedListModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. {
    5. return QVariant();
    6. }
    7. if (index.row() >= m_reports.size() || index.row() < 0)
    8. {
    9. return QVariant();
    10. }
    11. if (role == Qt::DisplayRole)
    12. {
    13. const QMultiMap<QString, QString>& report = m_reports.at(index.row());
    14.  
    15. QString reportName = report.value("reportName");
    16. QString actionName = report.value("actionName");
    17.  
    18. if (index.column() == 0)
    19. {
    20. return reportName;
    21. }
    22. else if (index.column() == 1)
    23. {
    24. return actionName;
    25. }
    26. }
    27. else if (role == Qt::BackgroundRole)
    28. {
    29. if (index.column() == 1)
    30. {
    31. QString cellContent = index.data().toString();
    32. if (cellContent.contains("Click here"))
    33. {
    34. return QVariant(QColor("#FF4500"));
    35. }
    36. else
    37. return QVariant(QColor("#32CD32"));
    38. }
    39. }
    40. else if (role == Qt::TextAlignmentRole && index.column() == 1)
    41. {
    42. return Qt::AlignHCenter;
    43. }
    44. else if (role == Qt::FontRole)
    45. {
    46. QFont font;
    47. font.setFamily("Optima");
    48. font.setPointSize(10);
    49. return font;
    50. }
    51.  
    52. return QVariant();
    53. }
    To copy to clipboard, switch view to plain text mode 

    In the data() method I have a condition to specify the cells of column 1 background color but, the color does't not change until i click anywhere in the tableview.
    How can I fix it?
    Thank you
    Franco Amato

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems in understanding how to use a QComboBox in my QTableView derived class

    emit dataChanged(index, index, {role});
    I think your "role" here needs to be Qt::DisplayRole. EditRole is used only when editing, not when updating the view after editing.

    In the data() method I have a condition to specify the cells of column 1 background color but, the color does't not change until i click anywhere in the tableview.
    How can I fix it?
    The Qt::BackgroundRole is used when drawing items that have the default delegate. Since you have a custom delegate, you may need to implement the QStyledItemDelegate::paint() method to draw the delegate with the correct background color. Note that if you do this, you also have to implement the sizeHint() method.
    <=== 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.

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

    franco.amato (24th November 2021)

Similar Threads

  1. Problems with signals and slots in derived class
    By ittod in forum Qt Programming
    Replies: 6
    Last Post: 21st May 2013, 23:16
  2. Replies: 1
    Last Post: 6th December 2012, 20:56
  3. Replies: 2
    Last Post: 16th February 2012, 11:08
  4. Problems understanding QGraphicsScene
    By Bocki in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2008, 13:43
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 08:36

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.