×

Discussion Board

Results 1 to 15 of 15
  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Qt Quick SelectionDialog with dynamic content from C++

    Hi,

    I'm addressing a problem trying to have user select an item with selectiondialog. My app works with network so, that a network request starts when user presses a button and when it returns, it will get parsed. If the parsing finds out that there is multiple choises, it signals that a selectiondialog should be shown.

    The problem I have is that the Dialog is empty. Here's my QML:
    //in MainPage.qml

    Code:
    SelectionDialog {
            function showPlaceDialog(){
                console.log("Multiple dialogs called")
                placeDialog.model = myClass.getPossibleListTo()
                placeDialog.open()
            }
            Connections {
                target: reittiopas
                onMultipleChoicesForLocationSignal: placeDialog.showPlaceDialog()
            }
            id: placeDialog
        }
    With the function getPossibleListTo() returns a QList<AddressContainer *>

    Code:
    Q_INVOKABLE QList<AddressContainer *> getPossibleListFrom();
    Where AddressContainer derives from QObject and is basicly a struck-class.

    I don't fully understand why the dialog is empty. With Q_PROPERTY I should be able to access variables within AddressContainer and with a delegate of my own to visualize it? Or is there simply a better way to make a SelectionsDialog. I guess it says that is supports types that are compatible with ListView which basicly means that for my own class it has to derive from QAbstractListModel but I couldn't get that to work and that seems a bit overkill for this simple task.

    Almost all examples handle a case where a dialog content is static and it can be registered with setContentProperty to use as a model, but I guess that cannot be the case now.

    Thanks already for ideas!

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    506

    Re: Qt Quick SelectionDialog with dynamic content from C++

    its makes sense for you to read about qml data binding. especially about what kind of c++ classes supported by qml as data models
    Symbian & Qt developer. http://vladest.org

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Re: Qt Quick SelectionDialog with dynamic content from C++

    I've read it but I it handles about the case with a custom C++ class but I believe static content registered via setContentProperty

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    506

    Re: Qt Quick SelectionDialog with dynamic content from C++

    but anyway that static comtent have to be surrounded by special class, derrived from QObject
    Symbian & Qt developer. http://vladest.org

  5. #5
    Super Contributor
    Join Date
    Mar 2008
    Posts
    1,481

    Re: Qt Quick SelectionDialog with dynamic content from C++

    HI,
    If you try like this it will work
    QStringList dataList;
    dataList.append("Item 1");
    dataList.append("Item 2");
    dataList.append("Item 3");
    dataList.append("Item 4");




    QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());

    QDeclarativeContext *ctxt = viewer->rootContext();
    ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));

    in qml
    SelectionDialog {
    id: dialog1
    titleText: "Select team:"

    model : myModel // mymodel is in CPP file

    onAccepted:
    {
    mytext.text=dialog1.model[selectedIndex]
    }
    }
    MouseArea
    {
    anchors.fill: parent
    onClicked:
    {
    console.log("hi")
    dialog1.open()
    }
    }

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Re: Qt Quick SelectionDialog with dynamic content from C++

    So what am I doing wrong here. I made out the simplemost example of what I figured could work but it displays a blank list only.

    Code:
    // main.cpp
    #include "qmlapplicationviewer.h"
    #include <QDeclarativeView>
    #include <QtDeclarative>
    
    #include "testclass.h"
    
    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
        QApplication app(argc,argv);
    
        QDeclarativeView view;
        thirdObject k;
        view.rootContext()->setContextProperty("k", &k);
        view.setSource(QUrl::fromLocalFile("qml/dialogtest/main.qml"));
    
        view.show();
    
        return app.exec();
    }
    Code:
    //testclass.h
    #ifndef TESTCLASS_H
    #define TESTCLASS_H
    
    #include <QObject>
    #include <QAbstractListModel>
    
    
    class myObject : public QObject
    {
        Q_OBJECT
    public:
        explicit myObject(QString arvo,QObject *parent = 0);
        QString arvo;
    private:
    
    
    };
    
    
    class mylist : public QAbstractListModel
    {
        Q_OBJECT
    public:
        explicit mylist(QObject *parent = 0);
        QVariant data(const QModelIndex &index, int role) const;
        Qt::ItemFlags flags(const QModelIndex &index) const;
        int rowCount(const QModelIndex &parent) const;
    
        void addObject(myObject* obj);
        
    signals:
        
    public slots:
        
    private:
        QList<myObject*> lista;
    };
    
    class thirdObject : public QObject
    {
        Q_OBJECT
    public:
        explicit thirdObject(QObject *parent=0);
        Q_INVOKABLE mylist* getList();
    
    signals:
        void showSelections();
    
    };
    
    #endif // TESTCLASS_H
    Code:
    //testclass.cpp
    #include "testclass.h"
    
    
    QVariant mylist::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid()){
            return QVariant();
        }
        if(role == Qt::DecorationRole){
            return lista[index.row()]->arvo;
        }
        return QVariant();
    }
    
    Qt::ItemFlags mylist::flags(const QModelIndex &index) const
    {
        if(index.isValid()) return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        return Qt::ItemIsEnabled;
    }
    
    int mylist::rowCount(const QModelIndex &parent) const
    {
        if(!parent.isValid())
            return 0;
        else
            return lista.size();
    }
    
    myObject::myObject(QString arvo, QObject *parent)
    : arvo(arvo)
    {
    }
    
    void mylist::addObject(myObject *obj)
    {
        lista.append(obj);
    }
    
    thirdObject::thirdObject(QObject *parent)
    {
    }
    
    mylist *thirdObject::getList()
    {
        mylist *lista = new mylist;
        myObject *yksi = new myObject("oskari");
        myObject *kaksi = new myObject("antti");
        lista->addObject(yksi);
        lista->addObject(kaksi);
        return lista;
    }
    
    mylist::mylist(QObject *parent)
    {
    }
    Code:
    # main.qml
    /*import QtQuick 1.1
    import com.nokia.symbian 1.1
    
    PageStackWindow {
        id: window
        initialPage: MainPage {tools: toolBarLayout}
        showStatusBar: true
        showToolBar: true
    
        ToolBarLayout {
            id: toolBarLayout
            ToolButton {
                flat: true
                iconSource: "toolbar-back"
                onClicked: window.pageStack.depth <= 1 ? Qt.quit() : window.pageStack.pop()
            }
        }
    }*/
    
    import QtQuick 1.1
    import com.nokia.symbian 1.1
    
    Page {
        id: mainPage
        Text {
            anchors.centerIn: parent
            text: qsTr("Hello world!")
            color: platformStyle.colorNormalLight
            font.pixelSize: 20
        }
        SelectionDialog {
            function showPlaceDialog(){
                console.log("Multiple dialogs called")
                placeDialog.model = k.getList();
               // var list = reittiopas.getPossibleListTo()
                //console.log(list.rowCount())
                placeDialog.open()
    
    
            }
            Connections {
                target: k
                onShowSelections: placeDialog.showPlaceDialog()
            }
            id: placeDialog
            //delegate: addressDelegate
        }
        Button {
            text: "Test"
            onClicked: placeDialog.showPlaceDialog()
        }
    }

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    506

    Re: Qt Quick SelectionDialog with dynamic content from C++

    There is some workaround required for SelectionDialog
    see, how its implemented on my side:
    #ifndef QSTRINGLISTMODELQML_H
    #define QSTRINGLISTMODELQML_H

    #include <QObject>
    #include <QStringListModel>

    class QStringListModelQML : public QStringListModel
    {
    Q_OBJECT

    public:
    Q_PROPERTY(int count READ count NOTIFY countChanged)
    // Contrary to normal QStringListModel, this class does not
    // allow giving the strings in the model in constructor.
    // The reason is that for this model to work also on
    // MeeGo/Harmattan, the strings must be set after the
    // QML template has been loaded. Otherwise the signal
    // handlers in QML have not been registered. The default
    // role name used to display the roles is "name", which is
    // used both by Symbian and Harmattan Qt Compontents.
    QStringListModelQML(const QByteArray &displayRoleName = "name", QObject *parent = 0);

    // Overrides QStringListModel's function. Setting this will
    // emit stringAdded(QString) for each string in the list. If
    // there was non empty string list set to this model before,
    // stringsReset() signal is emitted before
    // stringAdded(QString) signals.
    void setStringList(const QStringList &strings);
    QVariant data(const QModelIndex &index, int role) const;
    Q_INVOKABLE int rowCount(const QModelIndex &parent) const;
    Q_INVOKABLE int columnCount(const QModelIndex & parent = QModelIndex()) const;
    int count() const;
    Q_INVOKABLE QVariant getSelected(int i) const;
    Q_INVOKABLE QVariant data(int index) const;

    signals:
    void countChanged();
    };

    #endif // QSTRINGLISTMODELQML_H
    #include "qstringlistmodelqml.h"
    #include <QtCore/QHash>
    #include <QtCore/QDebug>
    #include <QtCore/QVariant>
    #include <QtCore/QModelIndex>

    QStringListModelQML::QStringListModelQML(const QByteArray &displayRoleName, QObject *parent)
    : QStringListModel(parent)
    {
    QHash<int, QByteArray> roleNames;
    roleNames.insert(Qt:isplayRole, displayRoleName);
    setRoleNames(roleNames);
    }

    int QStringListModelQML::count() const
    {
    return stringList().count();
    }

    QVariant QStringListModelQML::getSelected(int i) const
    {
    qDebug() << "get" << i;
    if (i >= 0 && i < count())
    {
    return QVariant(stringList().at(i));
    }
    return QVariant();
    }

    QVariant QStringListModelQML::data(int index) const
    {
    qDebug() << "data" << index;
    if (index >= 0 && index < count())
    {
    return stringList().at(index);
    }
    return QVariant();
    }

    void QStringListModelQML::setStringList(const QStringList &strings)
    {
    QStringListModel::setStringList(strings);
    emit countChanged();
    }

    int QStringListModelQML::columnCount(const QModelIndex & parent) const
    {
    return 1;
    }

    int QStringListModelQML::rowCount(const QModelIndex &parent) const
    {
    //qDebug() << "rowCount" << stringList().count();
    return stringList().count();
    }

    QVariant QStringListModelQML::data(const QModelIndex &index, int role) const
    {
    //qDebug() << index << role;
    if (index.isValid())
    {
    return QStringListModel::data(index, role);
    }
    return QVariant();
    }
    and usage...
    QML:
    SelectionDialog {
    id: langdlg
    width: parent.width
    height: parent.height
    titleText: qsTranslate("CarVCR","Select Language")
    model: languageModel
    selectedIndex: languageIndex
    onAccepted: {
    languageIndex = langdlg.selectedIndex
    languageSignal(languageIndex)
    language = languageModel.getSelected(langdlg.selectedIndex)
    }

    }
    and C++:
    //creating model
    QStringListModelQML langsModel;
    //set model to qml
    qmlengine->rootContext()->setContextProperty("languageModel", &langsModel);
    //put data into model
    langsModel.setStringList(langs);
    dont forget about model's scope
    Symbian & Qt developer. http://vladest.org

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Re: Qt Quick SelectionDialog with dynamic content from C++

    Could similar behaviour be achieved without this line:
    Code:
    qmlengine->rootContext()->setContextProperty("languageModel", &langsModel);
    I've tried to work around by registering my type first (qmlRegisterType() ) but I can't get that to work either..

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    506

    Re: Qt Quick SelectionDialog with dynamic content from C++

    hi Oskari

    what the problem with the line?
    Symbian & Qt developer. http://vladest.org

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Re: Qt Quick SelectionDialog with dynamic content from C++

    As said, if in anyway possible I would like to create the instance of the list only via a function call. Your method requires me to set the instance as a context property before I load the QML and start displaying stuff to user. But what I originally need (I will soon change my architecture, because this seems very hard) is to create a list and possibly only register the type? But most of all I would like to have info on how the selectionDialog actually works and what data types it is able to read at all?

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Re: Qt Quick SelectionDialog with dynamic content from C++

    mahbub_s60: I couldn't get your code to work either. It throws an error: "Unable to assign QVariantList to QDeclarativeListModel*" on model : myModel // mymodel is in CPP file.
    So this implicates that the list should actually be a QDeclarativeListModel* ? I wonder how could I contact the QML developers themselves..

  12. #12
    Super Contributor
    Join Date
    Mar 2008
    Posts
    1,481

    Re: Qt Quick SelectionDialog with dynamic content from C++

    Hi

    I tried this one (not really optimum code) and looks work in my side with C7 Belle SW.
    Attached Files Attached Files

  13. #13
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,006

    Re: Qt Quick SelectionDialog with dynamic content from C++

    Are list items visible in a simple listview , if you try to use one in the qml file?

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Re: Qt Quick SelectionDialog with dynamic content from C++

    vineet.jain: No I couldn't
    mahbub_s60: I noticed you have also tried qmlRegisterType sort of thing. Did you ever get that to work?

  15. #15
    Super Contributor
    Join Date
    Mar 2008
    Posts
    1,481

    Re: Qt Quick SelectionDialog with dynamic content from C++

    The example I put work in my machine Symbian Belle C7 device, it doe not work in your side?

Similar Threads

  1. Guarana components with dynamic content
    By ivanlitovski in forum Symbian
    Replies: 2
    Last Post: 2010-01-22, 12:30
  2. dynamic list item with text content retrive from webserver
    By narendrachinni in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2009-08-17, 12:50
  3. Dynamic content of CAknSettingItemList
    By vzblk in forum Symbian User Interface
    Replies: 4
    Last Post: 2008-05-17, 14:02
  4. [S60,9.1]IAP-SelectionDialog pops up twice
    By tobias_stoeger in forum Symbian Signed Support, Application Packaging and Distribution and Security
    Replies: 14
    Last Post: 2008-03-14, 11:37
  5. [S60,9.1]IAP-SelectionDialog pops up twice
    By tobias_stoeger in forum Symbian Networking & Messaging (Closed)
    Replies: 8
    Last Post: 2006-05-24, 14:19

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×