Namespaces
Variants
Actions

Archived:How to use QComboBox in Qt

Jump to: navigation, search
Archived.png
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.

This code snippet demonstrates how to use QComboBox and how to connect it with some event in Qt.

QComboBox *combo=new QComboBox();
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

Qtcombo1.jpg

  • After Clicking the combo

Qtcombo2.jpg

This page was last modified on 11 October 2012, at 04:14.
1072 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved