【发布时间】:2017-07-27 23:28:19
【问题描述】:
给你们的新问题。
我有一个简单的 kde (kf5) plasmoid,带有一个标签和两个按钮。
我在幕后有一个 C++ 类,我目前能够将信号从 C++ 发送到 qml。
问题:我需要从 qml 按钮向 C++ 类发送信号。
通常这可以通过使用像 QQuickView 等标准 Qt/qml 对象来完成,但在我的情况下,我没有 main.cpp。
这是我的 C++ 类头文件。使用 QTimer,我发出 textChanged_sig 信号,它告诉 qml 刷新标签的值:
class MyPlasmoid : public Plasma::Applet
{
Q_OBJECT
Q_PROPERTY(QString currentText READ currentText NOTIFY textChanged_sig)
public:
MyPlasmoid( QObject *parent, const QVariantList &args );
~MyPlasmoid();
QString currentText() const;
signals:
void textChanged_sig();
private:
QString m_currentText;
}
这是 plasmoid main.qml:
import QtQuick 2.1
import QtQuick.Layouts 1.1
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.components 2.0 as PlasmaComponents
Item {
Plasmoid.fullRepresentation: ColumnLayout {
anchors.fill: parent
PlasmaComponents.Label {
text: plasmoid.nativeInterface.currentText
}
PlasmaComponents.Button {
iconSource: Qt.resolvedUrl("../images/start")
onClicked: {
console.log("start!") *** HERE
}
}
}
}
PlasmaComponents.Label 项包含 c++ 字段 m_currentText 的正确值。
*** 这里我需要发出一些信号(或者调用一个 c++ 方法,会有同样的效果)。
有什么提示吗?
【问题讨论】:
-
当你说你没有
main.cpp,你到底是什么意思?我错过了您实例化 MyPlasmoid 的地方!您的问题是简单的“假设我使用 QML 实例化了一个 C++ 对象,如下所示:import MyPlasmoid; MyPlasmoid { id: plasmo }我如何调用 plasmo 的函数”还是您的问题是“我到底如何让我的类首先被实例化”? -
也许 KDE 以某种特定方式实例化 QML 代码?我不确定,只是假设...
-
没错,kde frameworks istantiates 一切(所以:没有 main.cpp,我的类是作为插件自动编译的)