Creating table in QcomboBox
Solved
General and Desktop
-
I would like to create a combobox with several columns and with column headers. I point out that I am new to programming with Qt. a bit like the image below. Thank you for kindly helping me. thank you.
-
Hi and welcome to devnet,
You have two options:
- set a custom view using QComboBox::setView
- as @JonB wrote, use a custom QStyledItemDelegate
-
Hi and welcome to devnet,
You have two options:
- set a custom view using QComboBox::setView
- as @JonB wrote, use a custom QStyledItemDelegate
-
I would like to create a combobox with several columns and with column headers. I point out that I am new to programming with Qt. a bit like the image below. Thank you for kindly helping me. thank you.
@devman MWE:
#include <QtWidgets> int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow w; QComboBox *combo = new QComboBox(&w); QStandardItemModel model(10, 3); model.setHorizontalHeaderLabels({"title", "name", "company"}); for(int i=0; i< model.rowCount(); ++i){ for(int j=0; j < model.columnCount(); ++j){ QStandardItem *it = new QStandardItem(QString("%1 - %2").arg(i).arg(j)); model.setItem(i, j, it); } } combo->setModel(&model); combo->setModelColumn(1); QTableView *view = new QTableView; combo->setView(view); view->setSelectionBehavior(QAbstractItemView::SelectRows); view->setFixedWidth(350); w.show(); return a.exec(); }
-
Thank you all for answering me so quickly, I received to do it with a setView but the columns just take the size of the combobox.or me I would like to display several columns even if the combobox is small.