c++ - Getting the ListView index of a QList object -


i've exposed qlist<myitem*> model listview. how can listview index of myitem* object?

context: i'm making findobject() function in c++ , when i've found object (a myitem*), want scroll corresponding item in listview.

some code (full source code , qt creator project can found @ https://github.com/joncol/qml_test).

the item class:

class myitem : public qobject {     q_object     q_property(qint32 data read data notify datachanged)  public:     myitem() : m_data(s_counter++) {}      quint32 data() const { return m_data; }  signals:     void datachanged();  private:     static int s_counter;     int m_data; }; 

the model:

class mycppmodel : public qobject {     q_object      q_property(qqmllistproperty<myitem> itemlist read itemlist notify itemlistchanged)  public:     mycppmodel()     {         m_specialitem = new myitem;          m_model.append(new myitem);         m_model.append(new myitem);         m_model.append(new myitem);         m_model.append(m_specialitem);         m_model.append(new myitem);     }      qqmllistproperty<myitem> itemlist()     {         return qqmllistproperty<myitem>(this, m_model);     }      q_invokable qvariant specialitem()     {         return qvariant::fromvalue(m_specialitem);     }  signals:     void itemlistchanged();  private:     qlist<myitem*> m_model;     myitem* m_specialitem; // simulate found item }; 

the 'main' function:

int main(int argc, char *argv[]) {     qapplication app(argc, argv);      qqmlapplicationengine engine;     qqmlcontext* c = engine.rootcontext();      mycppmodel* mycppmodel = new mycppmodel;     c->setcontextproperty("mycppmodel", mycppmodel);      qmlregisteruncreatabletype<myitem>("cppmodel", 1, 0, "myitem", "");      engine.load(qurl(qstringliteral("qrc:/main.qml")));      return app.exec(); } 

and, qml:

applicationwindow {     title: qstr("testing")     width: 640     height: 480     visible: true      columnlayout {         anchors.centerin: parent          rectangle {             width: 150             height: 25             color: "#e67e22"             border.width: 1             border.color: "black"              text {                 anchors.centerin: parent                 text: "mark special item"             }             mousearea {                 anchors.fill: parent                 onclicked: {                     var item = mycppmodel.specialitem()                     console.debug("how color special item: " + item)                 }             }         }          listview {             id: itemlist             width: 200             height: 25 * count              model: mycppmodel.itemlist              delegate: item {                 width: parent.width                 height: 25                  rectangle {                     width: parent.width                     height: 20                     color: "#34495e"                     border.width: 1                     border.color: "black"                      text {                         x: 10                         anchors.verticalcenter: parent.verticalcenter                         text: modeldata.data                         color: "white"                     }                 }             }         }     } } 

there few ways this, depending on application logic.

1. add isspecialitem property myitem

class myitem : public qobject {     q_object     q_property(qint32 data read data notify datachanged)     q_property(bool isspecialitem read isspecialitem write setisspecialitem notify isspecialitemchanged)     // ... } 

your delegate this:

rectangle {     width: parent.width     height: 20     color: "#34495e"     border.width: 1     border.color: isspecialitem ? "red" : "black"      text {         x: 10         anchors.verticalcenter: parent.verticalcenter         text: modeldata.data         color: "white"     } } 

this easiest approach, pollutes myitem might consider more ui-specific.

2. make mycppmodel derive qabstractitemmodel

with option, have specialitem role available in delegate, qml code delegate being identical option #1.

i call canonical approach problem, if itemlist not need qqmllistproperty. doesn't require adding potentially ui-specific isspecialitem property myitem, , can stored in same way you're storing it; in data() function, you'd write like:

if (role == specialitem) {     return qvariant(m_specialitem == m_model.at(index.row())); } 

3. expose special item index property of list

to way, you'd turn specialitem() function property of mycppmodel:

q_property(int specialitemindex read specialitemindex write specialitemindex notify specialitemindexchanged) 

then, delegate similar option 2, except 1 line:

border.color: mycppmodel.specialitemindex == index ? "red" : "black" 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -