I'm using Q_PROPERTY with QML. My code is:
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*/
};
and one of the Pages in QML is
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 is an AZLyricsDownloader object
The codes runs correctly in C++, the function returns the text which should be in TextEdit.
But unfortunately, the TextEdit is blank. No text is shown there. there is no body for the signal, but AFAIK signal doesn't need it.
If I use
Q_PROPERTY(QString lyrics READ lyrics CONSTANT)
the result is the same.
What am I doing wrong?
When you change the value of the lyrics
property in C++ code, you have to send the NOTIFY
signal of the property (here void lyricsChanged();
) :
this->setProperty("lyrics", myNewValue);
emit lyricsChanged();
In this case, QML should update the value of the property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With