Results 1 to 9 of 9

Thread: How to populate Combobox in QTableView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to populate Combobox in QTableView

    May be I can use QSqlTableModel but in that case also still I have to run the query in Delegate class ComboBoxDelegate and I have to create one more connection to database which I want to avoid.

    Is there any way to get ComboBox in UI class AddBankTrxns? If I can get ComboBox in this class then it is straightforward to populate it as already db connection is available in this class.

    I have pasted code here for reference:

    Class : AddBankTrxns

    Qt Code:
    1. AddBankTrxns::AddBankTrxns(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::AddBankTrxns)
    4. {
    5. connectionName = "addbanktrxns";
    6. loggedInUserid = 0;
    7.  
    8. ui->setupUi(this);
    9.  
    10. headerLabels.append("Trxn Date");
    11. headerLabels.append("Trxn Description");
    12. headerLabels.append("Cheque No.");
    13. headerLabels.append("Trxn Type");
    14. headerLabels.append("Amount");
    15. headerLabels.append("Trxn Category");
    16. headerLabels.append("Remarks");
    17.  
    18. model = new QStandardItemModel(0, k_BANK_TRNX_COL_CNT);
    19. model->setHorizontalHeaderLabels(headerLabels);
    20. ui->bankTrxnsView->setModel(model);
    21.  
    22. addTransactionRow(0);
    23. setDBConnection();
    24. }
    25. void AddBankTrxns::addTransactionRow(int row){
    26. model->insertRow(row);
    27. ui->bankTrxnsView->setModel(model);
    28.  
    29. ui->bankTrxnsView->setItemDelegateForColumn(0, new DateEditDelegate());
    30. ui->bankTrxnsView->setItemDelegateForColumn(1, new LineEditDelegate());
    31. ui->bankTrxnsView->setItemDelegateForColumn(2, new LineEditDelegate());
    32. ui->bankTrxnsView->setItemDelegateForColumn(3, new ComboBoxDelegate());
    33. ui->bankTrxnsView->setItemDelegateForColumn(4, new LineEditDelegate());
    34. ui->bankTrxnsView->setItemDelegateForColumn(5, new LineEditDelegate());
    35. ui->bankTrxnsView->setItemDelegateForColumn(6, new LineEditDelegate());
    36. ui->bankTrxnsView->scrollToBottom();
    37. ui->bankTrxnsView->show();
    38.  
    39. setRowItemWidgets(row);
    40. // qDebug() << "After:rowcount=" << model->rowCount();
    41. }
    To copy to clipboard, switch view to plain text mode 

    Class : ComboBoxDelegate

    Qt Code:
    1. ComboBoxDelegate::ComboBoxDelegate(QObject *parent) :
    2. QItemDelegate(parent)
    3. {
    4. }
    5.  
    6. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
    7. const QStyleOptionViewItem &/* option */,
    8. const QModelIndex &/* index */) const
    9. {
    10. QComboBox *editor = new QComboBox(parent);
    11. return editor;
    12. }
    13.  
    14. void ComboBoxDelegate::setEditorData(QWidget *editor,
    15. const QModelIndex &index) const
    16. {
    17. // QString value = index.model()->data(index, Qt::EditRole).toString();
    18.  
    19. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    20. }
    21.  
    22. void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    23. const QModelIndex &index) const
    24. {
    25. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    26. model->setData(index, comboBox->currentText(), Qt::EditRole);
    27. }
    28.  
    29. void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
    30. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    31. {
    32. editor->setGeometry(option.rect);
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to ronak.vashi for this useful post:

    embeddedmz (31st October 2019)

  3. #2
    Join Date
    Aug 2009
    Posts
    140
    Thanks
    22
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to populate Combobox in QTableView

    You just want the user to be able to edit entries in the combo box, right? Why do you need your own QItemDelegates at all? I've never used it, but it looks like it's used for custom rendering. QComboBox as it is supports editing individual elements with the default delegates. So just have

    ui->my_combo_box->setModel(model);

    in your widget constructor, assuming the QComboBox is part of that gui.

  4. #3
    Join Date
    Apr 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to populate Combobox in QTableView

    No..I don't want user to edit entries in ComboBox. Combo box will be read-only and user just need to select any one option populated.

    Actually I just followed method shown in Qt ItemView example of spinbox shown on TableView. I am not sure if there is any other way to show widgets on QTableView w/o using ItemDelegates.

  5. #4
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to populate Combobox in QTableView

    hi,
    Qt Code:
    1. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem &/* option */,
    3. const QModelIndex &/* index */) const
    4. {
    5. QComboBox *editor = new QComboBox(parent);
    6. QComboBox::setEditable(false);
    7. // u can populate ur combo here.
    8. return editor;
    9. }
    To copy to clipboard, switch view to plain text mode 


    hope it helps
    Bala

  6. #5
    Join Date
    Apr 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to populate Combobox in QTableView

    Quote Originally Posted by BalaQT View Post
    hi,
    Qt Code:
    1. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem &/* option */,
    3. const QModelIndex &/* index */) const
    4. {
    5. QComboBox *editor = new QComboBox(parent);
    6. QComboBox::setEditable(false);
    7. // u can populate ur combo here.
    8. return editor;
    9. }
    To copy to clipboard, switch view to plain text mode 


    hope it helps
    Bala
    Hi all

    Thanks for help. It worked and I was able to populate Combobox list items from createEditor() function, but found a observation that if we are using Delegates then setEditorData is getting called twice once when view cell is double clicked and when mouse is clicked somewhere else. Not sure why it is behaving like this .

    Infact I find another easy way to set widgets. There is ready made function setIndexWidget() available to set a widget at particular cell.

    Now issue with this approach is that I am not able to get cell data. Not able to find suitable function from QTableView or way to read user entered or selected data.

    Any idea how to get data from QTableView ?

Similar Threads

  1. Replies: 3
    Last Post: 1st February 2011, 11:57
  2. Replies: 2
    Last Post: 30th June 2010, 10:48
  3. How to populate delegate with model data.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2008, 09:31
  4. Using QSettings to populate a QComboBox
    By nbkhwjm in forum Newbie
    Replies: 16
    Last Post: 4th September 2007, 22:34
  5. Populate a combobox from vector<string>
    By pkirk25 in forum Newbie
    Replies: 3
    Last Post: 15th November 2006, 23:52

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
  •  
Qt is a trademark of The Qt Company.