コアダンプの数だけ強くなれるよ

見習いエンジニアの備忘log

Qt5で始めるGUIプログラミング

Qt5を使ってGUIプログラミングに挑戦してみる。
まずはウィンドウを出すだけ。

Linuxサーバでアプリケーションを作成&実行し、表示はWindowsで行うスタイル。

実行環境

ホスト側

  • Windows7 32bit
  • Xming 6.9.0.31
  • Teraterm 4.9.2

サーバ側(実行側)

  • CentOS 6, x86_64 (64bit)
  • Qt 5.6.2

事前準備

ホスト側

下記アプリケーションをインストールする。

Xmingの起動、TeratermのSSH転送設定

インストール後、Xmingを起動しTeratermは下記設定を行う。

f:id:segmentation-fault:20170225190207p:plain

その後、一度Teratermを再起動して再度サーバにログインする。

サーバ側

Qt Downloads から実行環境に応じたバイナリをダウンロードする。今回はqt-opensource-linux-x64-5.6.2.runを選択。

$ chmod +x qt-opensource-linux-x64-5.6.2.run
$ ./qt-opensource-linux-x64-5.6.2.run



すると以下の画面が表示されるので必要事項を入力してインストール実行。

f:id:segmentation-fault:20170225190405p:plain



インストール場所の変更

デフォルトだとhome配下にインストールされるのでパスを変更する。
(インストール時に指定可能だがrootユーザor sudo実行では画面転送が上手くいかない場合がある)

/usr/local/shareへ変更

$ sudo mv Qt5.6.2 /usr/local/share



ld.so.confにライブラリパスを追記

$ sudo vi /etc/ld.so.conf
#/usr/local/share/Qt5.6.2/5.6/gcc_64/lib/ を追記
$
$ sudo ldconfig #追記したパスを反映
$


~/.bashrcにqmakeのパスを追記

$ vi ~/.bashrc
# 下記行を追記
# export QT_ROOT="/usr/local/share/Qt5.6.2/5.6/" 
# export PATH="$QT_ROOT/gcc_64/bin:$PATH"
$
$ source ~/.bashrc #追記したパスを反映
$








プログラム作成

事前準備が終ったのでプログラムを作っていく。


sample.cpp

#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QLabel *label = new QLabel("Hello Qt!");

    label->show();

    return app.exec();
}



Makefileを生成する

$ qmake -project
$ qmake qt.pro



ここでmakeを実行するとコンパイルエラーが発生。

$ make
g++ -c -pipe -O2 -std=gnu++0x -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -I/usr/local/share/Qt5.6.2/5.6/gcc_64/include -I/usr/local/share/Qt5.6.2/5.6/gcc_64/include/QtGui -I/usr/local/share/Qt5.6.2/5.6/gcc_64/include/QtCore -I. -I/usr/local/share/Qt5.6.2/5.6/gcc_64/mkspecs/linux-g++ -o sample.o sample.cpp
sample.cpp:1:24: error: QApplication: No such file or directory
sample.cpp:2:18: error: QLabel: No such file or directory
sample.cpp: In function 'int main(int, char**)':
sample.cpp:6: error: 'QApplication' was not declared in this scope
sample.cpp:6: error: expected ';' before 'app'
sample.cpp:7: error: 'QLabel' was not declared in this scope
sample.cpp:7: error: 'label' was not declared in this scope
sample.cpp:7: error: expected type-specifier before 'QLabel'
sample.cpp:7: error: expected ';' before 'QLabel'
sample.cpp:11: error: 'app' was not declared in this scope
sample.cpp: At global scope:
sample.cpp:4: warning: unused parameter 'argc'
sample.cpp:4: warning: unused parameter 'argv'



こちらによるとqt.proに下記行を追加すれば良いらしい。

$ vi qt.pro
TEMPLATE = app
TARGET = qt
INCLUDEPATH += .
QT += widgets    # この行を追記

# Input
SOURCES += sample.cpp


再度ビルド実行

$ make
/usr/local/share/Qt5.6.2/5.6/gcc_64/bin/qmake -o Makefile qt.pro
g++ -c -pipe -O2 -std=gnu++0x -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -I/usr/local/share/Qt5.6.2/5.6/gcc_64/include -I/usr/local/share/Qt5.6.2/5.6/gcc_64/include/QtWidgets -I/usr/local/share/Qt5.6.2/5.6/gcc_64/include/QtGui -I/usr/local/share/Qt5.6.2/5.6/gcc_64/include/QtCore -I. -I/usr/local/share/Qt5.6.2/5.6/gcc_64/mkspecs/linux-g++ -o sample.o sample.cpp
$


コンパイルが成功し実行するが謎のエラーが発生。

$ ./qt
This application failed to start because it could not find or load the Qt platform plugin "xcb"
in "".

Reinstalling the application may fix this problem.
Aborted


こちらによると環境変数 "QT_PLUGIN_PATH"を追加すれば良いらしい。
(stackoverflowさん、いつもありがとう)

$ vi ~/.bashrc
# export QT_PLUGIN_PATH="$QT_ROOT/gcc_64/plugins"  を追記
$

実行結果

$ ./qt


Windows上に下記画面が表示されれば成功。

f:id:segmentation-fault:20170225192900p:plain