你们的新问题。
我有一个简单的kde(kf5)plasmoid,带有标签和两个按钮。
我在幕后有一个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;
}
这是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 ++方法,会产生同样的效果)。
任何提示?
答案 0 :(得分:2)
由于您可以通过currentText
访问plasmoid.nativeInterface
属性,该对象几乎肯定是您的C ++小程序类的实例,即MyPlasmoid
实例。
因此,如果您的MyPlasmoid
有一个广告位,则可以将其作为plasmoid.nativeInterface
对象上的函数进行调用
在C ++中
class MyPlasmoid : public Plasma::Applet
{
Q_OBJECT
public slots:
void doSomething();
};
在QML中
onClicked: plasmoid.nativeInterface.doSomething()