qt - How can I display a QStringList in C++ to a QML ListView -


so i'm new qt , i'm trying improve c++ skills, decided start project can search items in qstringlist using textfield. got search function working , able move result of search qstringlist, can use display user in function declared "public slot".

the main idea list auto update user enters character textfield, does. managed resulted list slot function able display different list every time , character gets typed in textfield.


in function pass in list of search results, trying use this

m_context->setcontextproperty("resultmodel",qvariant::fromvalue(m_resultlist)); 

where resultmodel name of model in qml , m_resultlist results of search being stored, display list in listview. program compiles crashes after run it.

so, true question is: there way can display c++ qstringlist not in main.cpp qml listview?

the reason why i'm asking not in main because tried use same line above in main.cpp hard coded qstringlist , list able display, there must issue not being in main. because not able use slot function in searchclass auto update.


main.cpp

#include <qguiapplication> #include <qqmlapplicationengine> #include <qqmlcontext> #include <qdebug> #include "searchclass.h"  int main(int argc, char *argv[]) {     qguiapplication app(argc, argv);      qmlregistertype<searchclass>("b9c.backend", 1, 0, "backend");      qqmlapplicationengine engine;      searchclass obj;      engine.load(qurl(qstringliteral("qrc:/main.qml")));      qqmlcontext *context = engine.rootcontext();      obj.getcontext(context);      //the line below works if provided qstringlist      //context->setcontextproperty("resultmodel", qvariant::fromvalue(resultlist));      return app.exec(); } 

searchclass.h

#ifndef searchclass_h #define searchclass_h  #include <qobject> #include <qqmlcontext>  class searchclass : public qobject {     q_object     q_property(qstring usersearch read usersearch write setusersearch notify usersearchchanged)  public:     searchclass(qobject *parent = 0);      qstringlist resultlist;      qstring usersearch();     void setusersearch(qstring &usersearch);      void getfilenameandinput(qstring inputstring);     qstring compareinputandfilename(qstring inputstring, qstring filename);     qstringlist getfilename();      //get context     void getcontext(qqmlcontext *context);  signals:     void usersearchchanged();  public slots:     void setusersearch();  private:     qstringlist m_resultlist;     qstring m_usersearch;     qqmlcontext* m_context; };  #endif // searchclass_h 

searchclass.cpp

#include "searchclass.h" #include <qdebug> #include <qqmlcontext> #include <qguiapplication> #include <qqmlapplicationengine>   searchclass::searchclass(qobject *parent) : qobject(parent) {     connect(this, signal(usersearchchanged()), this, slot(setusersearch())); }  //the result should displayed in slot when ever user types in character textfield void searchclass::setusersearch(){      qdebug() << "slot: " << m_resultlist;  //the line below makes program crash. works when implemented in main.cpp //    m_context->setcontextproperty("resultmodel", qvariant::fromvalue(m_resultlist));  }  qstring searchclass::usersearch() {     return m_usersearch; }  void searchclass::setusersearch(qstring &usersearch) {     if (usersearch == m_usersearch)         return;      m_usersearch = usersearch;      qdebug() << "input: " <<m_usersearch;      getfilenameandinput(m_usersearch);      emit usersearchchanged(); }  qstringlist searchclass::getfilename(){  //returns items searched for...  }  void searchclass::getfilenameandinput(qstring inputstring){  //puts search results class variable m_resultlist...      m_resultlist = resultlist;  }  qstring searchclass::compareinputandfilename(qstring inputstring, qstring filename){  //search processing...   }  //gets context use setproperty in above signal, crashes void searchclass::getcontext(qqmlcontext *context){      m_context = context;  } 

main.qml

import qtquick 2.6 import qtquick.controls 2.0 import b9c.backend 1.0 import qtquick.window 2.2   applicationwindow {     id: root     width: 300     height: 480     visible: true     backend { id: backend }      textfield {         id: txtfield         text: backend.usersearch         placeholdertext: qstr("search...")         width: parent.width          ontextchanged: backend.usersearch = text     }      listview {         id:view         height: parent.height         width: parent.width         y: 5 + txtfield.height         model: resultmodel          delegate: rectangle {             border.color: "lightblue"             height: 25             width: parent.width             text {                 anchors.centerin: parent                 text: modeldata             }         }     }  } 

you doing wrong. in every possible way. name getcontext() function sets context.

m_resultlist never set in code have provided. there no way tell why application crashing, because actual data mystery.

you have qobject derived class - searchclass. should expose context property, , have string list interfaced qml being implemented q_property of searchclass.

here simple example:

// equivalent of searchclass class test : public qobject {     q_object     q_property(qstringlist model member m_model notify modelchanged)     qstringlist m_model;   public slots:     void setmodel(qstring m) {       m_model = m.split(" ");       modelchanged();     }   signals:     void modelchanged(); };  // in main.cpp   test t;   engine.rootcontext()->setcontextproperty("test", &t);  // in main.qml column {     textfield {       ontextchanged: test.setmodel(text)     }     listview {       width: 200; height: 300       spacing: 5           model: test.model       delegate: rectangle {         height: 25         width: 200         color: "lightgray"         text { text: modeldata; anchors.centerin: parent }       }     }   } 

as type text string sent test::setmodel(), splits space separated tokens , sets qstringlist, used model source list view.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -