给你们一个新问题。
我有一个简单的kde (kf5)质体,有一个标签和两个按钮。
我在幕后有一个C++类,目前我能够从C++发送信号到qml。
问题是:我需要从qml按钮向C++类发送信号。
通常,这可以通过使用标准的Qt/qml对象(如QQuickView等)来完成,但在我的例子中,我没有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;
}这是类等离子体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++方法,会产生同样的效果)。
有什么暗示吗?
发布于 2017-03-08 08:31:12
由于可以通过currentText访问plasmoid.nativeInterface属性,所以该对象几乎肯定是C++ applet类的实例,即MyPlasmoid实例。
因此,如果您的MyPlasmoid有一个插槽,它可以作为plasmoid.nativeInterface对象上的一个函数调用。
在C++中
class MyPlasmoid : public Plasma::Applet
{
Q_OBJECT
public slots:
void doSomething();
};在QML
onClicked: plasmoid.nativeInterface.doSomething()https://stackoverflow.com/questions/42653665
复制相似问题