Combo Box をダブルクリックして、アイテムを追加する。
アイテムがクリックされた時の処理
mainwindow.h の slots に void on_comboBox_currentIndexChanged(const QString &arg1); がとうろくされて、
mainwindow.cpp に
void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1) {..} が作成されているので、クリックされた時の処理を書く。
実行結果:
ソースコード:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mainwindow.h" | |
#include "ui_mainwindow.h" | |
#include <QDebug> | |
MainWindow::MainWindow(QWidget *parent) : | |
QMainWindow(parent), | |
ui(new Ui::MainWindow) | |
{ | |
ui->setupUi(this); | |
} | |
MainWindow::~MainWindow() | |
{ | |
delete ui; | |
} | |
void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1) | |
{ | |
qDebug() << "選択されたアイテム:" << arg1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 on_comboBox_currentIndexChanged(const QString &arg1); | |
private: | |
Ui::MainWindow *ui; | |
}; | |
#endif // MAINWINDOW_H |
ソースコードからCombo Box を追加してみる。
ソースコードでコンボボックスを作成する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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からカウント | |
} | |
スロットを追加する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
0 件のコメント:
コメントを投稿