QListStringModel setData not set data



  • 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)

  • Qt Champions 2017

    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 ?



  • @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;


  • Qt Champions 2017

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.