【问题标题】:Q_PROPERTY not shownQ_PROPERTY 未显示
【发布时间】:2012-07-18 17:15:13
【问题描述】:

我将 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*/
};

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 /**/}
}

azdownloader 是一个 AZLyricsDownloader 对象

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

但不幸的是,TextEdit 是空白的。那里没有显示任何文字。 信号没有主体,但 AFAIK 信号不需要它。

如果我使用

Q_PROPERTY(QString lyrics READ lyrics CONSTANT)

结果是一样的。

我做错了什么?

【问题讨论】:

  • 你有没有把 LyricsChanged 信号连接到什么东西上?

标签: qt qt4 qml qtdeclarative


【解决方案1】:

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

this->setProperty("lyrics", myNewValue);
emit lyricsChanged();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    相关资源
    最近更新 更多