Q_PROPERTY未显示

mar*_*trz 4 qt qt4 qml qtdeclarative

我正在使用Q_PROPERTY和QML.我的代码是:

using namespace std;

typedef QString lyricsDownloaderString; // this may be either std::string or QString

class lyricsDownloader : public QObject
{
    Q_OBJECT
public:
    Q_PROPERTY(QString lyrics READ lyrics NOTIFY lyricsChanged)
    Q_INVOKABLE virtual short perform() = 0;
    inline void setData(const string & a, const string & t); // set artist and track
    Q_INVOKABLE inline void setData(const QString & a, const QString & t); // for QStrings
    Q_INVOKABLE inline bool anyFieldEmpty(); // check whether everything is filled
    inline QString lyrics()
    {
        return lyrics_qstr;
    }

 /*some more data*/
signals:
    void lyricsChanged(QString);
};

class AZLyricsDownloader : public lyricsDownloader
{
    Q_OBJECT
public:
    AZLyricsDownloader() : lyricsDownloader("", "") {}
    AZLyricsDownloader(const string & a, const string & t) : lyricsDownloader(a, t) {}
    Q_INVOKABLE short perform();
    //Q_INVOKABLE inline void setData(const string & a, const string & t);// set artist and track

protected:
    /*some more data*/
};
Run Code Online (Sandbox Code Playgroud)

QML中的一个页面是

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.0

Page
{
    id: showLyricsPage
    tools: showLyricsToolbar

    Column
    {
        TextEdit
        {
            id: lyricsview
            anchors.margins: 10
            readOnly: true
            text: azdownloader.lyrics
        }
    }

    Component.onCompleted:
    {
        azdownloader.perform()
        busyind.visible = false
    }

    BusyIndicator
    {id: busyind /**/ }

    ToolBarLayout
    {id: showLyricsToolbar/**/}

    // Info about disabling/enabling edit mode

    InfoBanner {id: editModeChangedBanner /**/}
}
Run Code Online (Sandbox Code Playgroud)

azdownloader是一个AZLyricsDownloader对象

代码在C++中正确运行,函数返回应该在TextEdit中的文本.

但不幸的是,TextEdit是空白的.那里没有显示文字.信号没有机身,但AFAIK信号不需要它.

如果我使用

Q_PROPERTY(QString lyrics READ lyrics CONSTANT)
Run Code Online (Sandbox Code Playgroud)

结果是一样的.

我究竟做错了什么?

air*_*dex 8

当您lyrics在C++代码中更改属性的值时,您必须发送NOTIFY属性的信号(此处void lyricsChanged();):

this->setProperty("lyrics", myNewValue);
emit lyricsChanged();
Run Code Online (Sandbox Code Playgroud)

在这种情况下,QML应该更新属性的值.