从c++显示qml与qmlscene显示的内容不同
Displaying qml from c++ differs from what qmlscene shows
我正在尝试运行一个基本程序,该程序通过c++显示一个简单的qml文件。加载QQmlEngine等的代码如下:
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include <QGuiApplication>
#include <QQuickView>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlEngine engine;
QQmlComponent component(&engine);
QQuickWindow::setDefaultAlphaBuffer(true);
component.loadUrl(QUrl("qrc:///qmlFiles/main.qml"));
if ( component.isReady() )
component.create();
else
qWarning() << component.errorString();
QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(),SLOT(quit()));
return app.exec();
}
qml(简化)文件为:
import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Dialogs 1.0
ApplicationWindow
{
width:800
height:600
//background: "white"
visible:true
function selectFile()
{
fileChooser.visible=true;
}
menuBar:MenuBar {
Menu{
title:"File"
MenuItem{
text:"Choose File"
shortcut: "Ctrl+F"
onTriggered:{
selectFile();
}
}
MenuItem {
text:"Quit"
shortcut: "Ctrl+Q"
onTriggered: Qt.quit()
}
}
}
FileDialog{
id:fileChooser
visible:false
modality:Qt.WindowModal
title:"Choose data file"
onAccepted:{
console.log(fileChooser.fileUrls)
visible:false
}
onRejected:{
console.log("Cancel")
visible:false
}
}
}
当我使用qmlscene从终端运行文件时,显示的内容与从c++程序运行文件时不同。
我的猜测是,C++实现无法使用特定于平台的东西(例如QFileDialog),而只能使用这些东西的qml实现。
我想我需要以不同的方式加载qml文件,但如何加载呢?
发现QGuiApplication
存在一些重写或其他内容。
将其更改为QApplication
,看起来和qmlscene中一样好。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 密码登录程序将永远循环并显示不正确的结果
- 从c++显示qml与qmlscene显示的内容不同