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



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



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



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



  • 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..!!



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



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



  • 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!
    }
    @



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


Log in to reply
 

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