Archived:How to use QComboBox in Qt
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
This code snippet demonstrates how to use QComboBox and how to connect it with some event in Qt.
Article Metadata
Code Example
Source file: Media:combobox.zip
Tested with
Devices(s): Symbian emulator
Compatibility
Platform(s): Qt
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: QComboBox, Push Button, Text Label
Created: james1980
(06 Jan 2009)
Last edited: hamishwillee
(11 Oct 2012)
Various Function
- This function holds to add the item in the ComboBox in string format.
combo->additem("X");
combo->addItem("Y");
- To set the size of the ComboBox items. If you set the maximum number to be less then the current amount of items in the ComboBox, the extra items will be truncated.
combo->setMaxCount(5);
- To remove the Item form the ComboBox removeItem(int index) at a given index.
combo->removeItem(5);
Source File
#include <QApplication>
#include <QPushButton>
#include <QComboBox>
#include <QLabel>
#include <QVBoxLayout>
#include <QWidget>
#include <QString>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *win =new QWidget;
QVBoxLayout *layout = new QVBoxLayout;
QComboBox *combo = new QComboBox();
QLabel *label = new QLabel("hello");
QPushButton *helloButton = new QPushButton("Show Label");
combo->addItem("A");
combo->addItem("B");
combo->addItem("C");
QObject::connect(combo, SIGNAL(activated(int)),label, SLOT(hide()));
QObject::connect(helloButton, SIGNAL(clicked()),label, SLOT(show()));
layout->addWidget(label);
layout->addWidget(combo);
layout->addWidget(helloButton);
win->setLayout(layout);
win->showMaximized();
return app.exec();
}
ScreenShot
- Before clicking the combo
- After Clicking the combo
- Download working example here : combobox.zip




14 Sep
2009
22 Sep
2009