Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

Listview delgate signal and C++

$
0
0
There’s really not enough info in your question for a quick answer. So here’s a long one. I’m not a big fan of having C++ reach down into my QML using object names. I much prefer to just use the setContextProperty() call to give my QML access to a “View Manager” class. That class would have all the Q_PROPERTY’s I need. So assuming you have a Q_PROPERTY named currentIndex on your C++ class with all the appropriate getters, setters and notify. In your row onclicked handler you could just add MainViewMgr.currentIndex = index I’m not quite sure what your question is about having your delegate in a separate file but if your mouse area is also in the file it shouldn’t be a problem. Here’s a sample QML delegate file with a mouse area that’s setting the current index. (This is just a cut and paste of some sample code from Qt Quick course so it’s not all relevant). import QtQuick 2.3     Item{     id: rowId       height: dataRowId.implicitHeight     width : dataRowId.implicitWidth     Row{         id: dataRowId         spacing : 10         Text{             text: index + ". " + model.name             font.pixelSize : rowId.ListView.view.fontSize         }         Text{             text: category             font.pixelSize: rowId.ListView.view.fontSize         }     }     MouseArea{         anchors.fill: parent         onClicked:{ rowId.ListView.view.currentIndex = index             MainViewMgr.currentIndex = index         }     } } In your startup code would be something along these lines     auto root_context = m_engine.rootContext();     root_context->setContextProperty("MainViewMgr",&m_mainViewMgr); And then in this sample the member variable m_mainViewMgr is a reference to a custom MainViewMgr class that in this case needs a single Q_PROPERTY named currentIndex. I use my AUTO_PROPERTY macro [syncor.blogspot.com] so the class could be just this class MainViewMgr : public QObject {         Q_OBJECT         AUTO_PROPERTY(int, currentIndex)     public:         explicit MainViewMgr(QObject *parent=0);   }; Hope that covers your question. My new course on using QML with C++ (to be published next month on Pluralsight) will cover this approach in detail although I don’t use a list model in the demos.

Viewing all articles
Browse latest Browse all 4972

Trending Articles