Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QApplication>
- #include <QComboBox>
- #include <QPushButton>
- #include <QStringList>
- #include <QStringListModel>
- #include <QVBoxLayout>
- #include <algorithm>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- QStringList strings = { "aaa", "bbb", "ccc", "ddd" };
- //QStringListModel strings_model(strings);
- QStringList numbers = { "111", "222", "333", "444" };
- //QStringListModel numbers_model(numbers);
- QWidget main_widget;
- auto p_layout = new QVBoxLayout(&main_widget);
- auto p_combobox = new QComboBox(&main_widget);
- Q_ASSERT(p_combobox->model());
- //p_combobox->setModel(&strings_model);
- p_layout->addWidget(p_combobox);
- auto p_reverse_items_button = new QPushButton("Reverse order of items", &main_widget);
- p_layout->addWidget(p_reverse_items_button);
- bool is_current_strings = true;
- p_combobox->addItems(strings);
- Q_ASSERT(p_combobox->model()->rowCount() == 4);
- QObject::connect(p_reverse_items_button, &QAbstractButton::clicked, p_combobox, [&]
- {
- //auto p_current_model = qobject_cast<QStringListModel*>(p_combobox->model());
- //auto items = p_current_model->stringList();
- auto& items = is_current_strings ? strings : numbers;
- std::reverse(items.begin(), items.end());
- p_combobox->clear();
- p_combobox->addItems(items);
- //p_current_model->setStringList(items);
- });
- auto p_change_model_button = new QPushButton("Set another model", &main_widget);
- p_layout->addWidget(p_change_model_button);
- QObject::connect(p_change_model_button, &QAbstractButton::clicked, p_combobox, [&]
- {
- is_current_strings = !is_current_strings;
- auto& items = is_current_strings ? strings : numbers;
- p_combobox->clear();
- p_combobox->addItems(items);
- //auto p_current_model = qobject_cast<QStringListModel*>(p_combobox->model());
- //p_combobox->setModel(p_current_model == &strings_model ? &numbers_model : &strings_model);
- });
- main_widget.setLayout(p_layout);
- main_widget.show();
- return a.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement