我想制作一个使用 QML 进行对话 UI 的 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 编程的新手,所以任何帮助将不胜感激。谢谢你。