大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

Qtプログラミング – メインウィンドウ作成

株式会社クローバーフィールドの経営理念
著者:津路高広
公開日:2019/09/10
最終更新日:2019/09/10
カテゴリー:技術情報
タグ:

津路です。
今回からは、Qtでメインウィンドウを作成してみます。
まずは、メインウィンドウの形のみを作成します。
項目は、ウィンドウ、メニュー、ステータスバーです。
まず、main.cppを作成します。

#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow mainWin;
    mainWin.show();
    return app.exec();
}

メインウィンドウのヘッダを作成します。

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QAction;
class QLabel;

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow();
protected:

private slots:
private:
    void createMenus();
    void createStatusBar();
    QMenu *fileMenu;
    QLabel *locationLabel;
    QLabel *formulaLabel;
};
#endif

メインウィンドウのソースを作成します。

#include <QtGui>
#include "mainwindow.h"
MainWindow::MainWindow()
{
    createMenus();
    createStatusBar();
}
void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
}
void MainWindow::createStatusBar()
{
    locationLabel = new QLabel(" W999 ");
    locationLabel->setAlignment(Qt::AlignHCenter);
    locationLabel->setMinimumSize(locationLabel->sizeHint());

    formulaLabel = new QLabel;
    formulaLabel->setIndent(3);

    statusBar()->addWidget(locationLabel);
    statusBar()->addWidget(formulaLabel,1);
}

以上の3ファイルを用意して、
qmake -project; qmake mainwindow.pro;
そのあと、makeを実行しますと、mainwindowがビルドされます。
mainwindowを実行すると、以下のように、Fileメニューと、単純なステータスバーのついたウィンドウが表示されます。

次回は、リソースやアクションを作成する予定です。

    上に戻る