解决QString移动错误

Work around for QString mobile bug

本文关键字:错误 移动 QString 解决      更新时间:2023-10-16

我正试图用C++在QML Android上运行一个简单的按钮。

该程序使用QString/QQmlEngine进行编译和构建。当我尝试运行它时,它会给出以下信息:

kernel/qcoreapplication.cpp:418 (QCoreApplicationPrivate::QCoreApplicationPrivate(int&, char**, uint)): 
WARNING: QApplication was not created in the main() thread.

这显然是正常的,正如这里引用的:非主线程中的QApplication。然后它给出一个信息:

qrc:///calculator.qml:33 ((null)): 
qrc:///calculator.qml:33:15: Unable to assign [undefined] to QString

我读到这是一个未经证实的错误,引用如下:Qt项目-QTMOBILITY-1735。这种与bug相关的问题也会影响"模板缓冲区支持丢失,预计会出现渲染错误",因为我正在为Tablet开发。如果这真的是一个相关的bug,似乎是这样,有什么可能的方法来解决它吗?应该有某种可能的破解方法来解决这个问题。基本上,这几乎阻止了所有的运行。

代码简单小巧,因为我使用它作为框架,它只需在目标设备上运行一个按钮。这是一个短程序,我用它来尝试让事情开始运转。

main.cpp

#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlEngine>
int main(int argc, char *argv[])
{   QGuiApplication app(argc, argv);
QQmlApplicationEngine engine(QUrl("qrc:///calculator.qml"));
return app.exec();
}

计算器.qml

import QtQuick 2.0
import QtSensors 5.0
import QtQuick.Controls 1.0
Rectangle {
id: button
property bool checked: false
property alias text : buttonText.text
Accessible.name: text
Accessible.description: "This button does " + text
Accessible.role: Accessible.Button
function accessiblePressAction() {
button.clicked()
}
signal clicked
width: buttonText.width + 20
height: 30
gradient: Gradient {
GradientStop {
position: 0.00;
color: "#b0c4de";
}
}
radius: 5
antialiasing: true    
Text {
id: buttonText
text: parent.description
anchors.centerIn: parent
font.pixelSize: parent.height * .5
style: Text.Sunken
color: "white"
styleColor: "black"
}
MouseArea {
id: mouseArea
anchors.fill: parent
onClicked: parent.clicked()
}
Keys.onSpacePressed: clicked()
}

计算器.pro

QT += qml quick sensors svg xml
OTHER_FILES += 
calculator.qml
RESOURCES += 
calculator.qrc
SOURCES += 
calculator.cpp

我希望有人能提出一些建议。

当前版本中的文件calculator.pro显然是错误的。相反,它应该看起来更像这样:

# Add more folders to ship with the application, here
folder_01.source = qml/calculator
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01
QT += qml quick sensors svg xml
# Additional import path used to resolve QML modules in Creator's code model
QML_IMPORT_PATH =
# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp
# Installation path
# target.path =
# Please do not modify the following two lines. Required for deployment.
include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
qtcAddDeployment()

创建新项目时,将自动生成文件qtquick2applicationviewer。因此,您需要将其包含在您的主程序中。cpp:

#include "qtquick2applicationviewer.h"

特别是关于消息">无法将QString分配给QQuickItem",这似乎是qml文件中的一些sintax或参数定义问题,您可能想看看这里。

这应该是你的项目出现问题的原因。