Skip to content

Instantly share code, notes, and snippets.

@yamato8
Created December 28, 2013 20:51
Show Gist options
  • Save yamato8/8164070 to your computer and use it in GitHub Desktop.
Save yamato8/8164070 to your computer and use it in GitHub Desktop.
Combo Box をソースコードから使う:Qt
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QComboBox>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// コンボボックスがの追加
QComboBox *my_comboBox = new QComboBox(this);
my_comboBox->addItem("1つ目のアイテム");// アイテムの追加
my_comboBox->addItem("2つ目のアイテム");// アイテムの追加
my_comboBox->addItem("3つ目のアイテム");// アイテムの追加
my_comboBox->setGeometry(0,30,150,20);//位置の調整
connect(my_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(handleSelectionChanged(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::handleSelectionChanged(int index)
{
qDebug() << "アイテム:" << index << " が選択された"; // 0からカウント
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void handleSelectionChanged(int index);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment