Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QListStringModel setData not set data

    General and Desktop
    2
    5
    1090
    Loading More Posts
    • 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
      asiaron last edited by asiaron

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        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 Reply Quote 2
        • A
          asiaron last edited by 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;

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @asiaron last edited by

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

            1 Reply Last reply Reply Quote 0
            • A
              asiaron last edited by

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

              1 Reply Last reply Reply Quote 1
              • First post
                Last post