2

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);
}
Community
  • 1
  • 1
Jarrod Cabalzar
  • 408
  • 1
  • 5
  • 24
  • `I tried this but it's not working when I try to implement it within my existing app: ComboBox of CheckBoxes?` You will have to give some more info about that. What is not working? Show some code of what you tried. – thuga Aug 19 '13 at 08:20
  • I have run the code given in the other question's answer by itself and it, of course, works perfectly however when I try to create a model, with the exact code provided in that answer, and then assign it to an existing combo box within my application the box appears empty. (As in, I clicked on the dropdown menu and nothing happens) – Jarrod Cabalzar Aug 19 '13 at 08:44
  • Show us your code. Show us the part where you create the model, add items to it and set it to the combobox. – thuga Aug 19 '13 at 09:07
  • Any ideas as to what's wrong? – Jarrod Cabalzar Aug 20 '13 at 01:11

1 Answers1

0

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

Jarrod Cabalzar
  • 408
  • 1
  • 5
  • 24
  • Your `QStandardItemModel` object went out of scope after your `MainWindow` constructor finished. That is why you had to initialize your object with `new`. – thuga Aug 20 '13 at 06:52
  • since you have multiple selection in combo box which item is shown when combo box is inactive (when there is no popup)? – Aleksandar Jan 16 '14 at 12:30
  • @Aleksander I think I just repopulated it to "Select Assessment first" or something. Finished that project months ago. – Jarrod Cabalzar Jan 17 '14 at 09:56