Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Editable QListview using QAbstractListModel
Forum Updated to NodeBB v4.3 + New Features

Editable QListview using QAbstractListModel

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 7.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shiv
    wrote on 30 Mar 2016, 11:13 last edited by
    #1

    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.

    J 1 Reply Last reply 30 Mar 2016, 11:27
    0
    • S Shiv
      30 Mar 2016, 11:13

      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.

      J Offline
      J Offline
      Joel Bodenmann
      wrote on 30 Mar 2016, 11:27 last edited by Joel Bodenmann
      #2

      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.

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Shiv
        wrote on 30 Mar 2016, 12:27 last edited by Shiv
        #3

        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.

        J 1 Reply Last reply 30 Mar 2016, 13:19
        0
        • S Shiv
          30 Mar 2016, 12:27

          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.

          J Offline
          J Offline
          Joel Bodenmann
          wrote on 30 Mar 2016, 13:19 last edited by Joel Bodenmann
          #4

          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.

          Industrial process automation software: https://simulton.com
          Embedded Graphics & GUI library: https://ugfx.io

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Shiv
            wrote on 30 Mar 2016, 14:53 last edited by
            #5

            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.

            J 1 Reply Last reply 30 Mar 2016, 15:17
            0
            • S Shiv
              30 Mar 2016, 14:53

              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.

              J Offline
              J Offline
              Joel Bodenmann
              wrote on 30 Mar 2016, 15:17 last edited by
              #6

              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.

              Industrial process automation software: https://simulton.com
              Embedded Graphics & GUI library: https://ugfx.io

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Shiv
                wrote on 31 Mar 2016, 12:24 last edited by
                #7

                Thanks @Joel-Bodenmann

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

                1 Reply Last reply
                0

                1/7

                30 Mar 2016, 11:13

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved