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. [Resolved] Problem with listView->model()->setData
Forum Updated to NodeBB v4.3 + New Features

[Resolved] Problem with listView->model()->setData

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.9k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    LeoMauroDev
    wrote on last edited by
    #1

    Hi,

    I need create a function which insert new rows (with text) in my ListView.
    I do:

    @
    void MainWindow::addItemListData(QString itemList){
    if(itemView == 0) ui->listViewData->setModel(new QStandardItemModel(1, 1));
    else ui->listViewData->model()->insertRow(1);

    ui->listViewData->model()->setData(ui->listViewData->model()->index(itemView, 0, QModelIndex()), itemList);
    itemView++;
    

    }
    @

    Note: variable itemView is a global int and star with 0 (zero).

    But my function has a big problem. When i put more than 2 items, the items of middle (between the first and last) disappear.

    I call this function:
    @
    addItemListData("1");
    addItemListData("2");
    addItemListData("3");
    addItemListData("4");
    @

    The item 2 and 3 disappear.

    Thaks.

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Hi,
      What is itemView and how do you set it? IYAM it's better to keep the model in a class variable instead of asking the view all the time who the model is. Now the 5th line almost becomes unreadable.
      Could you debug the application and check what itemView does?
      Your logic seems to be oke.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tilsitt
        wrote on last edited by
        #3

        Hi,

        QStandardItemModel::insertRow() returns a boolean indicating if the row was successfully inserted. You should check it to see if your row is effectively inserted.

        And, as Jeroentje@home asked you, you should check what itemView does (and how it is initialized)?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          LeoMauroDev
          wrote on last edited by
          #4

          Hi Jeroentje,

          I begin this days programing in Qt Creator, i nob. =D
          I don't think do that. When i'm back, i'll try this.

          Hi tilsitt,
          I'll try this too.

          Thanks a lot..!!

          1 Reply Last reply
          0
          • L Offline
            L Offline
            LeoMauroDev
            wrote on last edited by
            #5

            I see this function:
            void QAbstractItemModel::beginInsertRows ( const QModelIndex & parent, int first, int last ) [protected]

            How use this in my function, yesterday i try, but does't work.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tilsitt
              wrote on last edited by
              #6

              This is a protected method, so you can't use it unless you sub-class QAbstractItemModel.

              1 Reply Last reply
              0
              • JeroentjehomeJ Offline
                JeroentjehomeJ Offline
                Jeroentjehome
                wrote on last edited by
                #7

                It's when you inherit the QAbstractItemModel and generate your own model. The function is handled by the QStandardItemModel for you, so don't bother ;-)
                Oke, your a noob, so some code as example (didn't debug it!!)
                @
                void MainWindow::addItemListData(QString Item_str)
                {
                if (m_Model == NULL)
                {
                m_Model = new QStandardItemModel(); // m_Model is a QStandardItemModel * in your class definition
                ui->itemView->setModel(&m_Model);
                }
                QStandardItem CellItem(Item_str); // This will generate a QStandardItem with the given text

                m_Model->appendRow(&CellItem); // this only works for single column views!
                }
                @

                Greetz, Jeroen

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  LeoMauroDev
                  wrote on last edited by
                  #8

                  Jeroentje and tilsitt,
                  Really thanks, you save my project!!

                  Jeroentje,
                  I adapted your function and work perfectly!

                  @
                  void MainWindow::addItemListData(QString itemList){

                  if (ui->listViewData->model() == NULL){
                      ui->listViewData->setModel(&m_Model);
                  }
                  
                  m_Model.appendRow(new QStandardItem(itemList));
                  

                  }
                  @

                  Note: m_Model is global.

                  Now i kwon the difference between QStandardItemModel and QAbstractItemModel.

                  Thanks,
                  One day i'll contribute this favor.

                  1 Reply Last reply
                  0

                  • Login

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