×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    Qt + QML + ListView + Refresh ListView

    Hello,

    I'm writing tiny app. Which has QML ListView and after I adding new element (on different page) want see changes on ListView.

    I have QAbstractListModel named ServerListModel with method which inserts new value:

    Code:
    void ServerListModel::insertValue(int k, Host &host)
     {
         beginInsertRows(QModelIndex(), k, k);
         servers.append(host);
         endInsertRows();
         emit dataChanged();
         reset();
     }
    Insert method is called from QML which calls remove method (of QAbstractListModel subclass). getModel() methods passes model to QML ListView.

    Code:
    void ServerModel::insert(QString title, QString hostname, QString username, QString password, qint32 port )
    {
        Host host = Host(title, hostname, username, port) ;
        servers.append(host);
        int val = servers.size() -1 ;
        model->insertValue(val, host);
        save() ;
    }
    
    QObject * ServerModel::getModel()
    {
        return qobject_cast<QObject*>(model) ;
    }
    In main.cpp I have qmlRegisterType:

    Code:
    qmlRegisterType<ServerModel>("lt.ServerModel", 1, 0, "ServerModel");
    The problem is that ListView not changing. After executing beginInsertRows or beginDeleteRows I see what rowCount (of QAbstractListModel) is triggered. But getModel() method not. So any suggestions how to fix this problem ?

  2. #2
    Regular Contributor
    Join Date
    Mar 2012
    Posts
    144

    Re: Qt + QML + ListView + Refresh ListView

    try to emit layoutCanged() signal

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    Re: Qt + QML + ListView + Refresh ListView

    How to correctly add signal ?

    Added to my QAbstractListModel subclass ServerListModel this code:

    Code:
    Q_SIGNALS:
        void countChanged(int count);
        void rowsInserted() ;
        void dataChanged() ;
        void layoutChanged();
    And just emitting them... emit layoutChanged();

    I'm did some debugging:

    After I insert new element with Insert method to servers structure (data structure which holds values) it changes (row count makes bigger). I added to QML onStatusChanged callback:

    Code:
    onStatusChanged:
        {
            list.model = server.getModel()
        }
    getModel() gets called, but row count not changing.
    Code:
    QObject * ServerModel::getModel()
    {
        qDebug("getModel()");
        int i=0;
        int count = model->rowCount() ;
        qDebug("Model count: " + QString::number(count).toAscii());
        return qobject_cast<QObject*>(model) ;
    }
    Any new suggestions? So simple think and I stuck. Thanks.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    Re: Qt + QML + ListView + Refresh ListView

    I found the problem. I changed:
    Code:
    qmlRegisterType<ServerModel>("lt.ServerModel", 1, 0, "ServerModel");
    to
    Code:
    ServerListModel *model = new ServerListModel() ;
    QDeclarativeContext *ctxt = viewer.rootContext();
    ctxt->setContextProperty("server", model);
    And it started to work.

Similar Threads

  1. xml listview
    By Evaldas12 in forum [Archived] Qt Mobility Project
    Replies: 1
    Last Post: 2012-04-04, 04:38
  2. ListView never gets refreshed
    By guillaume.lachaud in forum Qt
    Replies: 5
    Last Post: 2011-11-15, 13:52
  3. Question about ListView
    By xe0on in forum [Archived] Beginning Qt
    Replies: 2
    Last Post: 2011-11-13, 17:37
  4. ListView inside ListView
    By hassan kassem in forum [Archived] Qt Quick
    Replies: 2
    Last Post: 2011-10-20, 10:53
  5. ListView in QML
    By Siddharth5 in forum [Archived] Qt Quick
    Replies: 1
    Last Post: 2011-10-15, 15: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
  •  
×