2

给你们的新问题。

我有一个简单的 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++ 方法,会产生相同的效果)。

有什么提示吗?

4

1 回答 1

3

由于您可以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()
于 2017-03-08T08:31:12.253 回答