Results 1 to 11 of 11

Thread: How to create dependency between 2 columns using combobox delegates in MVC

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to create dependency between 2 columns using combobox delegates in MVC

    Here you go, run this code.

    I used delegate (because I used standard model) but you could do the similar in the your custom model.

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. class Delegate : public QStyledItemDelegate
    4. {
    5. public:
    6. explicit Delegate(QObject * parent = 0) : QStyledItemDelegate(parent) {}
    7.  
    8. QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    9. {
    10. QComboBox * widget = new QComboBox(parent);
    11.  
    12. widget->addItems(index.data(Qt::UserRole+1).toStringList());
    13.  
    14. return widget;
    15. }
    16.  
    17. void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
    18. {
    19. if(index.column() == 0)
    20. {
    21. QComboBox * widget = dynamic_cast<QComboBox *>(editor);
    22.  
    23. if(widget)
    24. {
    25. QString country = widget->currentText();
    26.  
    27. QVariant russia = QStringList() << "Moscow" << "Saint Petersburg" << "Novosibirsk" << "Yekaterinburg";
    28. QVariant canada = QStringList() << "Toronto" << "Parliament Hill" << "Quebec" << "Montreal";
    29. QVariant china = QStringList() << "Shanghai" << "Beijing" << "Chongqing" << "Hong Kong";
    30. QVariant states = QStringList() << "New York City" << "Los Angeles" << "Chicago" << "Houston";
    31.  
    32. QModelIndex nextColumnIndex = model->index(index.row(), 1);
    33.  
    34. if(country == "Russia")
    35. {
    36. model->setData(nextColumnIndex, russia, Qt::UserRole+1);
    37. }
    38. else if(country == "Canada")
    39. {
    40. model->setData(nextColumnIndex, canada, Qt::UserRole+1);
    41. }
    42. else if(country == "China")
    43. {
    44. model->setData(nextColumnIndex, china, Qt::UserRole+1);
    45. }
    46. else if(country == "United States")
    47. {
    48. model->setData(nextColumnIndex, states, Qt::UserRole+1);
    49. }
    50.  
    51. model->setData(index, country);
    52. model->setData(nextColumnIndex, "Select City Again");
    53. }
    54. widget->addItems(index.data(Qt::UserRole+1).toStringList());
    55. }
    56. else
    57. {
    58. QStyledItemDelegate::setModelData(editor, model, index);
    59. }
    60. }
    61. };
    62.  
    63. int main(int argc, char *argv[])
    64. {
    65. QApplication app(argc, argv);
    66.  
    67. QTableView table;
    68. QStandardItemModel model(4, 2);
    69.  
    70. for (int row = 0; row < 4; row++)
    71. {
    72. QStandardItem *item = new QStandardItem(QString("Select Country").arg(row).arg(0));
    73. item->setData(QStringList() << "Russia" << "Canada" << "China" << "United States",Qt::UserRole+1);
    74. model.setItem(row, 0, item);
    75.  
    76. item = new QStandardItem(QString("Select City").arg(row).arg(1));
    77. item->setData(QStringList());
    78. model.setItem(row, 1, item);
    79. }
    80.  
    81. table.setModel(&model);
    82. table.setItemDelegate(new Delegate(&model));
    83. table.show();
    84.  
    85. return app.exec();
    86. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  2. The following user says thank you to Santosh Reddy for this useful post:


Similar Threads

  1. Replies: 2
    Last Post: 28th June 2014, 07:21
  2. How to create a color bar in combobox ?
    By likaci in forum Newbie
    Replies: 4
    Last Post: 22nd January 2014, 16:16
  3. Replies: 0
    Last Post: 25th July 2011, 14:11
  4. ComboBox with multiple columns
    By haldrik in forum Qt Programming
    Replies: 7
    Last Post: 11th July 2009, 10:15
  5. Different delegates to different Columns of QTableView.
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 6th August 2008, 09:17

Tags for this Thread

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.