Results 1 to 20 of 24

Thread: Populate Listview from Database

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2016
    Posts
    26
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Populate Listview from Database

    hello . I am trying something like this

    scene : when someone clicks on delete button , i handle it like this

    Qt Code:
    1. Button{
    2. text: "delete"
    3. onClicked: {
    4. dbman.del(id) // this one actually deletes file from db and from system , works fine
    5. mod.deletepages(id) // this is what i am trying to implement
    6. }
    To copy to clipboard, switch view to plain text mode 

    so what mod.deletepages(id) do is get the id of the page and send it to here

    Qt Code:
    1. void listmodel::deletepages(QString pageid)
    2. {
    3.  
    4.  
    5. qDebug() << pageid;
    6. beginRemoveRows(QModelIndex(),remove,remove);
    7. endInsertRows();
    8. }
    To copy to clipboard, switch view to plain text mode 

    now what i have is the value not the index . how to get the index of that value .

    i have tried to use m_list.indexOf(pageid) ;

    but i get error " error: no matching function for call to 'QList<list>::indexOf(QString&)'
    int remove = m_list.indexOf(pageid); "
    ^

    so how to get the index

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Populate Listview from Database

    The easiest way is to pass the row index, since the view and the model have the same concept of what a row is

    In your delegate
    Qt Code:
    1. mod.deletepages(model.index)
    To copy to clipboard, switch view to plain text mode 

    In your model
    Qt Code:
    1. void listmodel::deletepages(int row)
    2. {
    3. beginRemoveRows(QModelIndex(), row, row);
    4. m_list.remove(row);
    5. endRemoveRows();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Ideally you don't do two calls from QML though, but have one C++ method that modifies the model and the database.
    Otherwise you could forget one and model and database could get out of sync.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 16th May 2016, 23:50
  2. Replies: 24
    Last Post: 14th March 2016, 08:58
  3. Replies: 0
    Last Post: 16th October 2015, 16:51
  4. Populate QTreeView from database
    By yagabey in forum Qt Programming
    Replies: 15
    Last Post: 26th March 2015, 02:10
  5. Replies: 2
    Last Post: 26th March 2015, 01:56

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.