Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Qt QComboBox example 1: use of QStringListModel

 

QComboBox example 1: use of QStringListModel is an example of how to set a QComboBox its model with a QStringListModel.

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppQComboBoxExample1.pro

 

#-------------------------------------------------
#
# Project created by QtCreator 2013-05-21T13:27:44
#
#-------------------------------------------------

QT       += core gui

TARGET = CppQComboBoxExample1
TEMPLATE = app


SOURCES += main.cpp\
        qtdialog.cpp

HEADERS  += qtdialog.h

FORMS    += qtdialog.ui

 

 

 

 

 

main.cpp

 

#include <QtGui/QApplication>
#include "qtdialog.h"

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QtDialog w;
  w.show();
  
  return a.exec();
}

 

 

 

 

 

qtdialog.h

 

#ifndef QTDIALOG_H
#define QTDIALOG_H

#include <QDialog>

namespace Ui {
  class QtDialog;
}

class QtDialog : public QDialog
{
  Q_OBJECT
  
public:
  explicit QtDialog(QWidget *parent = 0);
  ~QtDialog();
  
private slots:
  void on_pushButton_clicked();

private:
  Ui::QtDialog *ui;
};

#endif // QTDIALOG_H

 

 

 

 

 

qtdialog.cpp

 

#include "qtdialog.h"

#include <QStringListModel>
#include "ui_qtdialog.h"

QtDialog::QtDialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::QtDialog)
{
  ui->setupUi(this);
  ui->pushButton->click();
}

QtDialog::~QtDialog()
{
  delete ui;
}

void QtDialog::on_pushButton_clicked()
{
  QStringList strings;
  if ((std::rand() >> 4) % 2) strings.append("A");
  if ((std::rand() >> 4) % 2) strings.append("B");
  if ((std::rand() >> 4) % 2) strings.append("C");
  if ((std::rand() >> 4) % 2) strings.append("D");

  QStringListModel * const model = new QStringListModel(strings);
  ui->comboBox_1->setModel(model);
  ui->comboBox_2->setModel(model);
  ui->comboBox_3->setModel(model);
  ui->comboBox_4->setModel(model);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml