Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Editable QListview using QAbstractListModel



  • Hi all
    I have implemented a QListview using the example given in link http://doc.qt.io/qt-5/model-view-programming.html#creating-new-models.I have three push buttons named as addnew,edit and delete.If i click the addnew button the listview should insert a row and it should be in edit mode so that i can enter the data.If edit button is pressed the row should be in edit mode in the same way if delete button is pressed the row should be removed.I have loaded the listview with data and also added the buttons but I am stuck in implementing the operations mentioned.Any help will be appreciated thank you.



  • You will have to use the Signal&Slot concept to connect the buttons clicked(bool) signal to a slot in your class that handles the corresponding part of the GUI. In that slot you call the insertRow()method of your model. Once you retrieved the index of the newly added row (if it was successful!) use that index to show up the editor for the corresponding item.

    The same applies to the delete button: Connect the buttons clicked(bool) signal to a slot and call the removeRow() implementation of your model in that slot.

    Note that depending on your needs you can also add custom methods to your module to increase convenience. You can for example add a new method that will automatically add a new row and change the item state to the 'edit' state. This way you don't have to manually retrieve the model index in your GUI.
    Having convenience functions like that in your model implementation is actually encouraged by the Qt documentation.

    I hope that helps somehow.



  • Hi @Joel-Bodenmann
    Thanks for your kind reply I have implemented the buttons with signals and slots. As I mentioned earlier if I edit a data in listview the data is getting vanished.Moreover you said that add new and set the state to 'edit' I am doing this by

    //for edit
    QModelIndex index = ui->listView->currentIndex();
    ui->listView->edit(index);
    
    //for add new and edit
    int row = model->rowCount();
    model->insertRows(row+1,1,QModelIndex());
    QModelIndex index = model->index(row,0,QModelIndex());
    ui->listView->setCurrentIndex(index);
    ui->listView->edit(index);
    

    I am doing this for add new and edit. Is this is the right way do it if not can you plz provide a simple example.Thank you.



  • If your data vanishes then it's most likely a fault in your model implementation.
    From what you show it seems that you are using insertRows() the wrong way. The row number you are passing appears to be wrong. The row number must be bigger or equal to 0 and smaller or equal to rowCount(). Also, for convenience you might want to use insertRow() instead.
    You might also want to check the index you get from QAbstractItemModel::index() for validity.



  • Thanks
    The data vanishes , what I meant was the display of the text goes off completely in the listview only the cursor displays in the row .So every time i have to reenter the text again. The code i have used was exactly the same as the link below
    http://doc.qt.io/qt-5/model-view-programming.html#creating-new-models
    in that under the topic 'A read-only example model' if possible can you check the link and tell me what i have to do to display the text while editing.

    I have replaced insertRows() with insertRow() and also checked the index there is no issue with the index i am able to save the data also .

    Thanks you.



  • I am not sure whether there's some built-in option that allows you to control whether the editor contains the current text or not. I have never used the default editors.
    What I can tell you is that I am sure that you can implement the behavior you want by subclassing and assigning QItemDelegate. In the createEditor() function you simply create a QLineEdit and in the setEditorData() function you use QLineEdito::setText() to set the text to the current item text. That's all.

    If you have never worked with item delegates before I'd recommend giving this example a read: https://doc.qt.io/qt-5/qtwidgets-itemviews-spinboxdelegate-example.html
    You can basically copy-paste the code and just use a QLineEdit instead of a QSpinBox.



  • Thanks @Joel-Bodenmann

    I have implemented it with QItemDelegate and i am able to edit the listview.


Log in to reply