[solved] qobjectlist-based model explanation



  • @
    class DataObject : public QObject
    {
    Q_OBJECT

     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
     Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
     ...
    

    };

    int main(int argc, char ** argv)
    {
    QApplication app(argc, argv);

     QList<QObject*> dataList;
     dataList.append(new DataObject("Item 1", "red"));
     dataList.append(new DataObject("Item 2", "green"));
     dataList.append(new DataObject("Item 3", "blue"));
     dataList.append(new DataObject("Item 4", "yellow"));
    
     QDeclarativeView view;
     QDeclarativeContext *ctxt = view.rootContext();
     ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
     ...
    

    @

    if i wrote this so:
    @
    Q_PROPERTY(QString name READ *get_*name WRITE setName NOTIFY nameChanged)
    @
    what should i write to obtain the same result?

    now here:
    @
    class DataObject : public QObject
    {
    Q_OBJECT

     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
     Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
     ...
    

    };
    @
    should i write a DataObject.cpp with the constructor? do you declare an explicit constructor in the header file DataObject.h? how do you write the constructor in this case?
    must i declare QString name and QString color as variables in the header file DataObject.h?



  • solution: i MUST write the constructor; yes, i must declare an explicit constructor of the class, to inizializate variabols and so on; yes, i must declarare all the variables i have used in Q_PROPERTYs in the header.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.