【发布时间】:2017-11-27 09:20:50
【问题描述】:
设置
我的系统是 KUbuntu 17.10,Qt 5.9。
我有一个 stackview,它实际上包含更多组件,但为了简洁起见,我将它们删掉了。基本上,当“返回项目”按钮被按下时,submitReturnButton 信号在 C++ 端被捕获。捕获端没有任何状态,它只是尝试发送文本以设置在标签上。
代码
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QObject>
#include <QQuickWindow>
#include "returnserver.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
//boilerplate ends here
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
QObject* returnStatusLabel = engine.findChild<QObject*>("returnStatusLabel");
ReturnServer returner(returnStatusLabel);
QObject::connect(window, SIGNAL(submitReturnForm(QString)), &returner, SLOT(returnPressed(QString)));
return app.exec();
}
returnserver.h
#ifndef RETURNSERVER_H
#define RETURNSERVER_H
#include <QObject>
#include <QVariant>
#include <QString>
#include <QQuickWindow>
class ReturnServer : public QObject
{
Q_OBJECT
QObject* target;
public:
ReturnServer(QObject* target, QObject* parent = nullptr);
public slots:
void returnPressed(const QString& str);
};
#endif // RETURNSERVER_H
returnserver.cpp
#include "returnserver.h"
#include <QString>
#include <QVariant>
ReturnServer::ReturnServer(QObject* target, QObject *parent) :
target(target),
QObject(parent)
{
}
void ReturnServer::returnPressed(const QString& str)
{
//qDebug() << "the signal got to slot correctly";
QVariant status = "Returned";
// emit statusReady(QVariant::fromValue(status));
QMetaObject::invokeMethod(target, "setReturnStatus", Q_ARG(QVariant, status));
}
QML
main.qml 并不真正相关,只是为了表明该应用具有 stackview 并分解为组件
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Borrowment terminal")
signal submitReturnForm(string itemID)
property int lineEditLength: 120
background: Rectangle
{
id: mainBackground
color: "gray"
}
StackView
{
id: stack
initialItem: returnPageComponent
}
Component
{
id: returnPageComponent
ReturnPage{}
}
}
ReturnPage.qml:
import QtQuick 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.2
Item {
id: returnPageView
ColumnLayout {
id: returnPageMenu
x: 213
y: 99
width: 200
height: 282
Layout.margins: 20
//redundant, just to make it compile
Rectangle {
width: lineEditLength
height: 40
color: "white"
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
TextInput {
id: itemIDEdit
width: parent.width
height: parent.height
font.pixelSize: 20
maximumLength: 8
}
}
//this label I want to set
Label
{
id: returnStatusLabel
objectName: "returnStatusLabel"
function setReturnStatus(status) {
console.log("set return status to " + status)
ReturnPage.returnStatus = status
}
text: "empty"
width: 100
height: 20
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
font.pointSize: 14
}
Button {
id: returnItemButton
width: 75
height: 30
text: "Return the Item"
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
onClicked: submitReturnForm(itemIDEdit.text)
}
}
}
截图
问题
如果returnStatusLabel 在不同的组件中,我如何调用/设置从 C++ 端发送的某些文本?
我尝试了什么
-
在
main.qml中创建函数并以根对象为目标从C++ 调用它。这确实调用了函数,但没有设置文本。我怀疑这与未使用正确的对象有关,因此与生命周期有关。
-
在标签本身中创建函数,在 C++ 中找到对象并以对象作为参数调用函数
在这种情况下,该函数根本没有被调用,尽管调试器没有抱怨连接设置不正确。
【问题讨论】: