Question

I've designed the GUI using Qt Designer and while trying to create a combo box with multiple selection, I've noticed that I can not seem to modify GUI elements created in a .UI file through the equivalent .CPP file.

So basically I'm wondering whether that is a known feature and I'll need to implement the GUI solely through the .CPP file or whether I'm not implementing my multiple selection properly? I'm new to Qt and a tad confused.

I tried this but it's not working when I try to implement it within my existing app: ComboBox of CheckBoxes?

Here is a section of my code. I initialise the GUI designed in Qt Designer and then create a new model and assign data to it, as per the linked answer, and then try to change the model of one of my combo boxes to be the new one. When I run the application the combo box is there but is empty.

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ...

    QStandardItemModel model(3, 1); // 3 rows, 1 col
    for (int r = 0; r < 3; ++r)
    {
        QStandardItem* item = new QStandardItem(QString("Item %0").arg(r));

        item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item->setData(Qt::Unchecked, Qt::CheckStateRole);

        model.setItem(r, 0, item);
    }

    ui->comboBox_2->setModel(&model);
}
Was it helpful?

Solution

I managed to fix it by declaring the new model as a pointer in the header file and then instantiating it and assigning it to the combo box. :D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top