Qt5 Tutorial QComboBox - 2020
bogotobogo.com site search:
QComboBox
In this tutorial, we will learn QComboBox.
Qt->Qt Gui Application:
Let's make the UI as shown in the picture below.
Forms->comboboxdialog.ui:
Here are the codes.
ComboDialog.cpp:
#include "comboboxdialog.h" #include "ui_comboboxdialog.h" ComboBoxDialog::ComboBoxDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ComboBoxDialog) { ui->setupUi(this); // Fill the items of the ComboBox for(int i = 0; i < 10; i++) { ui->comboBox->addItem("item " + QString::number(i)); } } ComboBoxDialog::~ComboBoxDialog() { delete ui; } // Message popup when the button is clicked void ComboBoxDialog::on_pushButton_clicked() { QMessageBox::information(this, "Item Selection", ui->comboBox->currentText()); }
ComboDialog.h:
#ifndef COMBOBOXDIALOG_H #define COMBOBOXDIALOG_H #include <QDialog> #include <QMessageBox> namespace Ui { class ComboBoxDialog; } class ComboBoxDialog : public QDialog { Q_OBJECT public: explicit ComboBoxDialog(QWidget *parent = 0); ~ComboBoxDialog(); private slots: void on_pushButton_clicked(); private: Ui::ComboBoxDialog *ui; }; #endif // COMBOBOXDIALOG_H
First, we populated the items of the QComboBox.
ui->comboBox->addItem("item " + QString::number(i));
Then, when we click the button, a message box will popup to give information about the item that's currently selected. This message is not needed because the combo is already displaying the current item. But it's there for getting used to the QMessageBox as well as signals/slots.
QMessageBox::information(this, "Item Selection", ui->comboBox->currentText());
Qt 5 Tutorial
- Hello World
- Signals and Slots
- Q_OBJECT Macro
- MainWindow and Action
- MainWindow and ImageViewer using Designer A
- MainWindow and ImageViewer using Designer B
- Layouts
- Layouts without Designer
- Grid Layouts
- Splitter
- QDir
- QFile (Basic)
- Resource Files (.qrc)
- QComboBox
- QListWidget
- QTreeWidget
- QAction and Icon Resources
- QStatusBar
- QMessageBox
- QTimer
- QList
- QListIterator
- QMutableListIterator
- QLinkedList
- QMap
- QHash
- QStringList
- QTextStream
- QMimeType and QMimeDatabase
- QFile (Serialization I)
- QFile (Serialization II - Class)
- Tool Tips in HTML Style and with Resource Images
- QPainter
- QBrush and QRect
- QPainterPath and QPolygon
- QPen and Cap Style
- QBrush and QGradient
- QPainter and Transformations
- QGraphicsView and QGraphicsScene
- Customizing Items by inheriting QGraphicsItem
- QGraphicsView Animation
- FFmpeg Converter using QProcess
- QProgress Dialog - Modal and Modeless
- QVariant and QMetaType
- QtXML - Writing to a file
- QtXML - QtXML DOM Reading
- QThreads - Introduction
- QThreads - Creating Threads
- Creating QThreads using QtConcurrent
- QThreads - Priority
- QThreads - QMutex
- QThreads - GuiThread
- QtConcurrent QProgressDialog with QFutureWatcher
- QSemaphores - Producer and Consumer
- QThreads - wait()
- MVC - ModelView with QListView and QStringListModel
- MVC - ModelView with QTreeView and QDirModel
- MVC - ModelView with QTreeView and QFileSystemModel
- MVC - ModelView with QTableView and QItemDelegate
- QHttp - Downloading Files
- QNetworkAccessManager and QNetworkRequest - Downloading Files
- Qt's Network Download Example - Reconstructed
- QNetworkAccessManager - Downloading Files with UI and QProgressDialog
- QUdpSocket
- QTcpSocket
- QTcpSocket with Signals and Slots
- QTcpServer - Client and Server
- QTcpServer - Loopback Dialog
- QTcpServer - Client and Server using MultiThreading
- QTcpServer - Client and Server using QThreadPool
- Asynchronous QTcpServer - Client and Server using QThreadPool
- Qt Quick2 QML Animation - A
- Qt Quick2 QML Animation - B
- Short note on Ubuntu Install
- OpenGL with QT5
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part A
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part B
- Video Player with HTML5 QWebView and FFmpeg Converter
- Qt5 Add-in and Visual Studio 2012
- Qt5.3 Installation on Ubuntu 14.04
- Qt5.5 Installation on Ubuntu 14.04
- Short note on deploying to Windows
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization