PDA

View Full Version : QComboBox drop down list as table



ROCKSTAR
3rd February 2014, 07:56
Guys, do you know how could i make drop down table for QComboBox where selected values from different columns and rows would have different Indexes and were displayed as current item?
It's not necessary to transform list to table, but there should be multiple columns and rows with different values and different indexes for each.
It would be awesome if you have some examples :)

anda_skoa
3rd February 2014, 12:10
You can set your own model on the combobox and set your own item view.
See QComboBox::setModel() and QComboBox::setView()

Try using a table model and a table view

Cheers,
_

ROCKSTAR
4th February 2014, 06:45
You can set your own model on the combobox and set your own item view.
See QComboBox::setModel() and QComboBox::setView()

Try using a table model and a table view

Cheers,
_

I get it



QStandardItemModel *model = new QStandardItemModel(5,5);

QStandardItem* col0 = new QStandardItem(QString("t").arg(0).arg(0));
QStandardItem* col1 = new QStandardItem(QString("M").arg(0).arg(1));
QStandardItem* col2 = new QStandardItem(QString("H").arg(0).arg(2));
QStandardItem* col3 = new QStandardItem(QString("P1").arg(1).arg(0));
QStandardItem* col4 = new QStandardItem(QString("T1").arg(1).arg(1));
model->setItem(0,0,col0);
model->setItem(0,1,col1);
model->setItem(0,2,col2);
model->setItem(1,0,col3);
model->setItem(1,1,col4);

QTableView* coilView = new QTableView(this); // create the tableview
ui->comboBox->setView(coilView); // set it to the comboBox before making changes
coilView->setSelectionMode(QAbstractItemView::SingleSelectio n);
coilView->setSelectionBehavior(QAbstractItemView::SelectItem s);
ui->comboBox->setModel(model); //set the model


but when i build this code a current item in combobox does not change if i select items in columns 2-5 (1st item of a row becomes a current item).

ChrisW67
4th February 2014, 07:21
A QComboBox is for selecting one item from a list. Not many items from many lists. The custom view ability allows you to display a table that, for example, shows the combo box value and meaning in two columns.

If you want the user to be able to select one option from each column of a model then use multiple QComboBoxes with a shared tabular model and tie each to a different column: QComboBox::setModelColumn()

If you want to be able to select any single value from among all those in the model then make it a simple list.

BTW: Your use of QString::arg() without place markers makes no sense at all.

ROCKSTAR
6th February 2014, 08:01
A QComboBox is for selecting one item from a list. Not many items from many lists. The custom view ability allows you to display a table that, for example, shows the combo box value and meaning in two columns.

If you want the user to be able to select one option from each column of a model then use multiple QComboBoxes with a shared tabular model and tie each to a different column: QComboBox::setModelColumn()

If you want to be able to select any single value from among all those in the model then make it a simple list.

BTW: Your use of QString::arg() without place markers makes no sense at all.

I think i don't get it :( I've tried this code and had an empty list


QTableView* coilView = new QTableView(this); // create the tableview
ui->comboBox->setView(coilView);
QStandardItemModel *model = new QStandardItemModel(5,5);

QStringList itemsList=(QStringList()<<"time"<<"M"<<"H"<<"P1"<<"T1");
ui->comboBox->addItems(itemsList);
ui->comboBox->setCurrentIndex(1);
QStringList itemsList_2=(QStringList()<<"N1OTN"<<"N2OTN"<<"GT"<<"R"<<"DKY");
ui->comboBox_2->addItems(itemsList_2);
ui->comboBox_2->setCurrentIndex(2);

ui->comboBox->setModel(model); //set the model
ui->comboBox->setModelColumn(0);
ui->comboBox_2->setModel(model); //set the model
ui->comboBox_2->setModelColumn(1);

anda_skoa
6th February 2014, 09:49
I think i don't get it :( I've tried this code and had an empty list

There is no content in "model" so the combobox showing that model is empty as well.

You don't add any data into the model, where to you expect it to come from?

Cheers,
_

ROCKSTAR
6th February 2014, 10:17
Oh, now i understand you guys, so i could only make 3 comboboxes (for 3 columns model) and user must check each of them to find what he need (cause i need tabular combobox to choose Y-axis title of a plot), and that is sad.

ROCKSTAR
11th February 2014, 08:50
I've had an idea to connect signals from combobox->view() to function which changes currentText and currentIndex, but it seems that signal does not emit


connect (ui->comboBox->view(), SIGNAL(clicked(QModelIndex)), this, SLOT(setComboText(QModelIndex)));



void MainWindow::setComboText(const QModelIndex & index) {
ui->comboBox->setCurrentText(comboModel->data(index).toString());
}

Could you help me, please?

anda_skoa
11th February 2014, 10:59
Maybe try signal activated?

Cheers,
_

ROCKSTAR
11th February 2014, 11:22
Maybe try signal activated?

Cheers,
_

I tried this one, but it doesn't work too. Full code:


comboModel = new QStandardItemModel(5,5);

QStandardItem* col0 = new QStandardItem(QString("t"));
QStandardItem* col1 = new QStandardItem(QString("M"));
QStandardItem* col2 = new QStandardItem(QString("H"));
comboModel->setItem(0,0,col0);
comboModel->setItem(0,1,col1);
comboModel->setItem(1,2,col2);

coilView = new QTableView(this); // create the tableview
ui->comboBox->setView(coilView); // set it to the comboBox before making changes

/* TableView configuration */
coilView->setSelectionMode(QAbstractItemView::SingleSelectio n);
coilView->setSelectionBehavior(QAbstractItemView::SelectItem s);
/********************/

ui->comboBox->setModel(comboModel); //set the model

coilView->setModel(comboModel);

// connect (ui->comboBox, SIGNAL(activated(int)), this, SLOT(setComboText_2(int))); // this one works but it's not what i want

connect (ui->comboBox->view(), SIGNAL(activated(QModelIndex)), this, SLOT(setComboText(QModelIndex)));
// connect (coilView, SIGNAL(activated(QModelIndex)), this, SLOT(setComboText(QModelIndex))); // doesn't emit too




void MainWindow::setComboText(const QModelIndex & index) {
// ui->comboBox->setCurrentText(comboModel->data(index).toString()); // commented, because i'm trying to test emmiting of signal by simple code below
std::cout << "hello!";
}