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. QListView not showing previously inserted item in a model
Forum Updated to NodeBB v4.3 + New Features

QListView not showing previously inserted item in a model

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.3k 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.
  • R Offline
    R Offline
    rizoritis
    wrote on last edited by
    #1

    Hello,

    I am using a text box to insert commands into a file which also get appended to my model. I then want the contents of the file (which contains the contents of the model as well) to appear in a QListView as I insert each line. What I am noticing is that every time I click the button to insert a new line I typed, that line does not show up in the QListView until I insert the next line. And that keeps happening for every line. So for example, the first line I ever enter leaves the ListView blank until I enter the next line of text and click my input button. Does anyone know why this may be? I entered the code below:

    @void mdiWidget::on_inputPushButton_clicked()
    {
    mdiTableModel->clear();

    QString mdicmd;
    
    if(!(mdiFile.open(QIODevice::ReadWrite)))
    {
        generalStatusMessage = "Error opening MDI file!";
        emit updateGeneralStatusSignal();
    }
    
    mdicmd = ui->mdiLineEdit->text();
    
    QTextStream textStream(&mdiFile);
    
    if((!mdicmd.isEmpty()) & (mdicmd.at(0).toUpper().toAscii() == 'G' || mdicmd.at(0).toUpper().toAscii() == 'M' || mdicmd.at(0).toUpper().toAscii() == 'O'
            || mdicmd.at(0).toUpper().toAscii() == 'X' || mdicmd.at(0).toUpper().toAscii() == 'Y' || mdicmd.at(0).toUpper().toAscii() == 'Z'))
    {
        textStream << mdicmd + "\n";
    
        while(!textStream.atEnd())
        {
            QString line = textStream.readLine();
            line = line.toUpper();
            QList<QStandardItem *> items;
            QStringList fields = line.split("\n");
            foreach (QString text, fields) {items.append(new QStandardItem((QString)text));}
            mdiTableModel->appendRow(items);
        }
    }
    else
    {
        generalStatusMessage = "Invalid MDI command!";
        emit updateGeneralStatusSignal();
    }
    
    ui->mdiLineEdit->clear();
    ui->mdiLineEdit->setFocus();
    
    mdiFile.close();
    
    ui->mdiListView->setModel(mdiTableModel);
    ui->mdiListView->show();
    
    ui->inputPushButton->setEnabled(false);
    ui->deletePushButton->setEnabled(false);
    

    }@

    Thanks!

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

      Hi,
      Didn't read your entire code in details, but I see some possible problems and crashes.
      First of all, when new data is inserted in your model you need to notify the view about it. This is done via startInsertRows or something like it.
      That tells the View not to read current data from the model.
      In the button clicked you do not need to "reset" the model to the view! This only needs to be done ONES!!
      The standard practice is:
      @
      on_button_clicked
      {
      if (newData)
      {
      beginInsertRows();
      // Add stuff to your model safely here
      endInsertRows();
      // send signal to View to reload data and redraw!
      update(); // or maybe reset() I forgotten that ;-)
      }
      }
      @

      Greetz, Jeroen

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rizoritis
        wrote on last edited by
        #3

        Thanks, but I don't fully understand the beginInsertRows()/endInsertRows() portions. I have never done that. I always assumed that

        @ui->mdiListView->setModel(mdiTableModel);@

        was where I notified the View to be updated with the model...I am not sure how to go about appending a row using the two functions you provided. It seems you have to specify the range of rows to insert. Also, would "item" be the first parameter?

        [quote author="Jeroentje@home" date="1382017316"]Hi,
        Didn't read your entire code in details, but I see some possible problems and crashes.
        First of all, when new data is inserted in your model you need to notify the view about it. This is done via startInsertRows or something like it.
        That tells the View not to read current data from the model.
        In the button clicked you do not need to "reset" the model to the view! This only needs to be done ONES!!
        The standard practice is:
        @
        on_button_clicked
        {
        if (newData)
        {
        beginInsertRows();
        // Add stuff to your model safely here
        endInsertRows();
        // send signal to View to reload data and redraw!
        update(); // or maybe reset() I forgotten that ;-)
        }
        }
        @[/quote]

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          If the view already has a model and exactly this model, setModel will do nothing.
          You should read the documentation of the models, beginInsertRow/endInsertRow is what you need.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          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