c++ - QML view wont update when adding a new item to a QAbstractListModel based model -
I have learned how to tie a model from a QML view to QAbstractListModel.
But the next thing I'm tired is not working. If a new item is added to the model, the QML view will not be updated. Why is it like this?
DataObject.h AppCore.h AppCore.cpp main.cpp main.qml Before you start the call, Enter End End SimpleListModel :: addName (CastString First Name, Last String Name) {Data Object * Data Object = New Data Object (First Name, Last Name); // Tell Qt what are you going to be startingInternetRO (ModelIndex), M_Items. size (), M_Items. size ()); // do it m_items.append (dataObject); // tell qt you are ending; } <2> These 2 functions throw all the necessary signal
class data objects {public: data object (Consteststrst and first name, constaste string and last name): first (first Name), last (last name) {} firstestustustring; Final Caststring; }; Simple listload H
class SimpleListModel: public QAbstractListModel {Q_OBJECT enum / * class * / roles {FIRST_NAME = Qt:: UserRole, LAST_NAME}; Public: SimpleListDodel (QObject * Parent = 0); QVariant data (const QModelIndex and index, int role) const; Q_INVOKABLE int rowCount (const QModelIndex and parent = QModelIndex ()) const; QHash & lt; Int, QByteArray & gt; Role name () cons; Zero addName (QString firstName, QString lastName); Private: Q_DISABLE_COPY (Simple LISTODODEL); QList & LT; DataObject * & gt; M_items; }; SimpleListModeel.cpp
Simple LISTODELL :: Simple LISTODODEL (QObject * Native): QAbstractListModel (Parent) {DataObject * first = new data Data object (casts ("FirstName 02"), Caststring ("LastName 02"), Data Object * Third = New Data Object (Caststring) - Object (Custastring ("Firstname 01"), Qestring ("Last Name 01"); ("First Name 03"), Caststring ("Last Name 03"); m_items.append (First); m_items.append (second); m _items.append (third);} QUSE & Lt; Int, Cubitarere & gt; Simple LISTODELLO :: ROLLAME () CONST {QUSE & LT; INTER, CUBIETAIRRE & gt; Roles; Roles [/ * Roles :: * / FIRST_NAME] = "First name"; Roles [/ * Roles :: * / LAST_NAME] = "Last name"; Return roles;} Simple SimpleListModel :: addName {DataStat firstName, QString lastName} {DataObject * dataObject = new DataObject (first name, Last name); m_items.append (dataObject); emit the data (this-> Index (m_items.size ()), this- & gt; Index (m_items.size ())); } IntimateListributeDellet :: Pantikota (Const. Qmdile Indexes and Construct) Returns M_tes. size (); } QVariant: SimpleListModel :: Data (const QModelIndex and index, int role) const {// --- return tap variant if index is invalid (! Index.isValid ()) back to QVariant (); // --- check bound if (index.row ()> (m_items.size () - 1)) back to QVariant (); Data object * dobj = m_items.at (index.row ()); Switch (role) {case / * roles: * / FIRST_NAME: return QVariant :: fromValue (dobj-> earlier); Case / * Roles: * / LAST_NAME: Return QVariant :: fromValue (dobj-> Final); Default: Return QVariant (); }}
Class AppCore: Public QObject {Q_OBJECT Q_PROPERTY (Simple ListModel) Simple SimpleSmart Read, Simple LISTODODEL Constant) Public: Clear AppCore (QObject * parent = 0); Simple Listsmodel * Simple LISTODODEL () CONST; Public slots: zero addName (); Private: Simple Listsmodel * m_SimpleListModel; }; AppCore.cpp
AppCore :: AppCore (QObject * guardian): QObject (parent) {m_SimpleListModel = New SimpleListModel (this); } SimpleListModel * AppCore :: simpleListModel () Const {return m_SimpleListModel; } Zero AppCore :: addName () {m_SimpleListModel-> Adnem ("first name new", "latestname"); } {/ Code>
int main (int argc, char * argv []) {QGuiApplication a (argc, Argv); QQuickView * View = New QQuickView (); AppCore * appCore = new AppCore (); QRegisterMetaType & lt; Simple Listsmodel * & gt; ("Simple listmodel"); See- & gt; Engine () - & gt; Root contact () - & gt; Setcontext Property ("Epcor", Epcor); View-> SetSource (QUrl :: fromLocalFile ("main.qml")); View-> Show (); Back a.exec (); }
// ... list view {id: myListView anchors.fill: Guardian Representative: MyDelegate Model: appCore.simpleListModel} MouseArea {anchors.fill: Parents onClicked: {appCore.addName () console.log ('rowCount:' + appCore.simpleListModel.rowCount ())}} // ... <
zero
Comments
Post a Comment