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. QListStringModel setData not set data

QListStringModel setData not set data

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.6k 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.
  • A Offline
    A Offline
    asiaron
    wrote on last edited by asiaron
    #1

    Hey, I'm making an education project text editor
    I list opened file in a dock (QListView). I need to handle clicks on rows in listview, so I store indexes of QSLM in a QMap modelListLinks. In first inserting row in QSLM everything is fine, but then setData just not works and title of files in dock is empty.
    Indexes are correct, because when I click on rows they change active tab properly.
    Function that creates rows

    void MainWindow::on_actionNew_triggered()
    {
        QTextEdit* textEdit = new QTextEdit(tabs);
        textEdits.insert(textEdit, "");
        tabs->addTab(textEdit, "Безымянный");
    
        modelList->insertRow(0);
        QModelIndex index = modelList->index(modelList->rowCount()-1);
        modelList->setData(index, "Безымянный", Qt::DisplayRole);
    
        modelListLinks.insert(index, textEdit);
        modelList->setData(index, fileInfo.fileName(), Qt::DisplayPropertyRole);
    }
    ```
    The part of constructor, which declares model
    
        tabs->addTab(textEdit, fileInfo.fileName());
        modelList->insertRow(0);
        QModelIndex index = modelList->index(modelList->rowCount()-1);
        modelList->setData(index, fileInfo.fileName(), Qt::DisplayPropertyRole);
        modelListLinks.insert(index, textEdit);
    
    ![0_1517245532900_Screenshot from 2018-01-29 19-48-47.png](https://ddgobkiprc33d.cloudfront.net/a5016df3-18f5-42fe-9199-48f4cd8b4eff.png)
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums.
      is this a custom model ?
      You might need to call
      http://doc.qt.io/qt-5/qabstractitemmodel.html#beginInsertRows

      Ah sorry its a QListStringModel ( non subclass, i assume ?)

      if we look at this sample
      http://www.bogotobogo.com/Qt/Qt5_QListView_QStringListModel_ModelView_MVC.php
      Im wondering if (from your sample)
      modelList->insertRow(0);
      QModelIndex index = modelList->index(modelList->rowCount()-1);
      Get the wrong index as
      http://doc.qt.io/qt-5/qabstractitemmodel.html#insertRow
      says it inserts before the given parent ( which is empty QModelIndex() )
      but you take out index for last row.
      When you click button, does it insert teh "empty" ones in bottom ? or top ?

      1 Reply Last reply
      2
      • A Offline
        A Offline
        asiaron
        wrote on last edited by asiaron
        #3

        @mrjj said in QListStringModel setData not set data:

        is this a custom model ?

        it's not a subclass, pure QListStringModel

        Here are declarations of used
        QMap<QModelIndex, QWidget*> modelListLinks;
        QStringListModel* modelList;

        mrjjM 1 Reply Last reply
        0
        • A asiaron

          @mrjj said in QListStringModel setData not set data:

          is this a custom model ?

          it's not a subclass, pure QListStringModel

          Here are declarations of used
          QMap<QModelIndex, QWidget*> modelListLinks;
          QStringListModel* modelList;

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @asiaron
          Ok plain standard.
          I update the answer. Im not sure you get the right index of the just inserted.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            asiaron
            wrote on last edited by
            #5

            Yes, I not properly read documentation and really mess with indexes, thank you for the answer
            .

            1 Reply Last reply
            1

            • Login

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