我正在尝试使用 C++ 在 QML Android 上运行一个简单的按钮。
程序编译构建使用QString/QQmlEngine .当我尝试运行它时,它会显示以下消息:
kernel/qcoreapplication.cpp:418 (QCoreApplicationPrivate::QCoreApplicationPrivate(int&, char**, uint)):
WARNING: QApplication was not created in the main() thread.
这里引用的显然是正常的:QApplication In Non-Main Thread .然后它给出消息:
qrc:///calculator.qml:33 ((null)):
qrc:///calculator.qml:33:15: Unable to assign [undefined] to QString
我读到这是一个未经证实的错误,引用如下:Qt Project - QTMOBILITY-1735 .这种与错误相关的问题也会影响“Stencil buffer support missing, expect rendering errors”,因为我正在为平板电脑开发。如果这真的是一个相关的错误,似乎是这样,那么有什么可能的解决方法吗?应该有某种可能的黑客来解决这个问题。基本上,这几乎阻止了一切运行。
代码简单而小巧,因为我将它用作框架,它只是让一个按钮在目标设备上运行。这是我用来尝试让事情开始工作的简短程序。
主要.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文件。因此,您需要将它包含到您的 main.cpp 中:
#include "qtquick2applicationviewer.h"
特别是关于消息“无法将 QString 分配给 QQuickItem”,这似乎是您的 qml 文件中的一些语法或参数定义问题,因为您可能想看看 here .
这应该是您的项目出现问题的原因。
关于android - 解决 QString 移动错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20734482/