#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QMenu *fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(new QAction(tr("&New"), this)); QMenu *edit = menuBar()->addMenu(tr("&Edit")); } MainWindow::~MainWindow() { delete ui; }
デザイン画面では、メニューバーのタイトル部分は日本語の入力が出来るが
項目の部分では日本語入ちょくが使えないので、コピー・ペーストで貼り付けるか、
ソースコードから追加する。
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); | |
//メニューバーに追加 | |
QMenu* help = this->menuBar()->addMenu(tr("ヘルプ(&H)")); | |
//最初のメニューを取得 | |
QMenu *fileMenu = this->menuBar()->findChildren<QMenu*>().at(0); | |
//メニューのアクションを作成 | |
QAction* fileSave = new QAction(tr("上書き保存(&S)"), this); | |
//メニューの中にアクションを設定する | |
fileMenu->addAction(fileSave); | |
//メニューバーの項目をリストで取得 | |
QList<QMenu*> menus = this->menuBar()->findChildren<QMenu*>(); | |
//メニューのアクションにスロットを設定する。 | |
QActionGroup *actions = new QActionGroup(this); | |
foreach (QMenu* m, menus) | |
{ | |
//メニューのアクション全てをアクショングループに設定 | |
foreach (QAction* a, m->actions()){ | |
actions->addAction(a); | |
} | |
} | |
//アクショングループに | |
connect(actions,SIGNAL(triggered(QAction*)),this,SLOT(onAction(QAction*))); | |
} | |
MainWindow::~MainWindow() | |
{ | |
delete ui; | |
} | |
/*メニューのアクションが選択された時の処理*/ | |
void MainWindow::onAction(QAction *action) | |
{ | |
qDebug() << action->text(); | |
} |
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 onAction(QAction *action); | |
private: | |
Ui::MainWindow *ui; | |
}; | |
#endif // MAINWINDOW_H |
0 件のコメント:
コメントを投稿