본문 바로가기

PROGRAM/Qt with C++

Qt - Combobox

 

Form files

.h

class QtWidgetsApp_1 : public QDialog
{
    Q_OBJECT

public:
    QtWidgetsApp_1(QWidget *parent = Q_NULLPTR);

private:
    Ui::QtWidgetsApp_1Class ui;

public slots:
    void btn_run_clicked();
};

 

.cpp

#include "QtWidgetsApp.h"

static int cnt = 0;

QtWidgetsApp_1::QtWidgetsApp_1(QWidget *parent) : QDialog(parent)
{
  
    ui.setupUi(this);
    connect(ui.btn_run, SIGNAL(clicked()), this, SLOT(btn_run_clicked()));
    // 콤보박스에 들어갈 메뉴를 추가
    ui.combobox->addItem("Option 1");
    ui.combobox->addItem("Option 2");
    ui.combobox->addItem("Option 3");
    ui.combobox->addItem("Option 4");
    ui.combobox->addItem("Option 5");
    ui.combobox->addItem("Option 6");
}

// 버튼 1
void QtWidgetsApp_1::btn_run_clicked()
{
    QString str; // String 타입으로 변수를 선언
    // 선택한 콤보박스의 위치를 입력받음(0~)
    str.sprintf("position: %d", ui.combobox->currentIndex());
    ui.lbl_num->setText(str);
    ui.lbl_text->setText(ui.combobox->currentText());
    
    // label 하나로...
    //ui.label->setText(QString("position: %1 text: %2")
    //    .arg(ui.combobox->currentIndex())
    //    .arg(ui.combobox->currentText()));
}

'PROGRAM > Qt with C++' 카테고리의 다른 글

Qt - Checkbox  (0) 2021.06.21
Qt - RadioButton  (0) 2021.06.21
Qt - VisualStudio 2019 한글깨짐  (0) 2021.06.21
Qt - SpinBox  (0) 2021.06.18
Qt - PushButton  (0) 2021.06.18