ダイアログ UI に QML を使用する C++ アプリケーションを作成したいと考えています。
UI コードをmain.cppの外側に配置しようとしているので、後で分離してスレッドで実行できます。
ビルドして実行: コンパイルでエラーは発生せず、アプリケーションの出力でもエラーは発生しません。
ただし、画面には何も表示されません。しかし、main.cppに記述されている場合、このコードのチャンクは QML ダイアログを正しく表示します:
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/Kiosk/main.qml"));
viewer.showExpanded();
私がやること:
新規プロジェクト -> アプリケーション -> Qt Quick 2 アプリケーション (ビルトイン エレメント)
main.qmlはそのままにしておきます。
新しいクラス「ダイアログ」を追加します
Dialog.hコード:
#ifndef DIALOG_H
#define DIALOG_H
#include <QObject>
#include "qtquick2applicationviewer.h"
class Dialog : public QObject
{
Q_OBJECT
public:
explicit Dialog(QObject *parent = 0);
void show();
signals:
public slots:
};
#endif // DIALOG_H
Dialog.cppコード:
#include "dialog.h"
Dialog::Dialog(QObject *parent) :
QObject(parent)
{
}
void Dialog::show()
{
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/Kiosk/main.qml"));
viewer.showExpanded();
}
main.cppコード:
#include <QtGui/QGuiApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
Dialog *dia = new Dialog();
dia->show();
return app.exec();
}
QtQuick 1.0に戻り、 QtQuick2ApplicationViewerを使用するコードのチャンクをQDeclarativeViewに置き換えると:
view = new QDeclarativeView();
view->rootContext()->setContextProperty("Dialog", this); //this
view->setSource(QUrl("qml/Kiosk/main.qml"));
view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
QML アプリが正しく表示されます。しかし、私はQtQuick 2.0を使いたいです。私は Qt プログラミングが初めてなので、どんな助けでも大歓迎です。ありがとうございました。