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. Cannot display newly loaded data
Forum Updated to NodeBB v4.3 + New Features

Cannot display newly loaded data

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.2k 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.
  • B Offline
    B Offline
    buckler
    wrote on last edited by SGaist
    #1

    Hello,
    I had a working code that loaded information from a file and then setModel to cause it to display to screen. But when I added a button to do this, replacing the temporary direct loading, the table view doesn't come up. I do set dataChanged over the whole table, or so I think, on loading. But it doesn't work. I thought that perhaps it wasn't really there, but my add record button increments the total as if it is there but I just can't see it, so I think that it really does exist.

    Here's my code.

    The constructor that sets the model initially (and where I had previously called the file loader that had worked but which now has the intention of not loading right away):

    workItem::workItem(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::workItem) 
    {
      ui->setupUi(this);
      labelTabsWith = PATIENTID; // default selection
    
      workItemListViewingModel *sourceModel = new workItemListViewingModel(this);
      QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
      proxyModel->setSourceModel(sourceModel);
      ui->workItemList->setModel(proxyModel);
      ui->workItemList->verticalHeader()->setVisible(true);
      ui->workItemList->horizontalHeader()->setVisible(true);
      ui->workItemList->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
      ui->workItemList->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
      connect(ui->workItemList->verticalHeader(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(workItemSelectedForAnalysis(int)));
    }
    

    Here is my slot that is connected to the load button:

    oid workItem::on_listFileNameButton_clicked() 
    { 
      workItemListFileName=QFileDialog::getOpenFileName(this, tr("Work Item List File"), "", tr("Work Item List Files (*_wilist.*)"));
      workItemListEntries.clear();
      if (!loadWorkItemListFromFile(workItemListFileName, workItem::Json)) {
          std::cerr << "!fromJsonworkItem.loadWorkItem(Json) test tripped" << std::endl;
          return;
      }
      QFileInfo fi(workItemListFileName);
      ui->listFileNameButton->setText(fi.fileName());
      emit reloadListToModel();
    };
    

    As you can see, I am emitting a signal to the model class, as what I think is the right way to do it. In fact, the signal is causing the following function of the model class to run as expected:

    oid workItemListViewingModel::reloadList()
    {
      qDebug() << "into reload list";
      emit dataChanged(parent->ui->workItemList->indexAt(parent->ui->workItemList->rect().topLeft()), parent->ui->workItemList->indexAt(parent->ui->workItemList->rect().topLeft()));
    }
    

    But alas, nothing is displayed in the table view!

    Thank you for any help,
    Andy

    [edit: added missing coding tags ```SGaist]

    B 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What does loadWorkItemListFromFile do ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      0
      • B buckler

        Hello,
        I had a working code that loaded information from a file and then setModel to cause it to display to screen. But when I added a button to do this, replacing the temporary direct loading, the table view doesn't come up. I do set dataChanged over the whole table, or so I think, on loading. But it doesn't work. I thought that perhaps it wasn't really there, but my add record button increments the total as if it is there but I just can't see it, so I think that it really does exist.

        Here's my code.

        The constructor that sets the model initially (and where I had previously called the file loader that had worked but which now has the intention of not loading right away):

        workItem::workItem(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::workItem) 
        {
          ui->setupUi(this);
          labelTabsWith = PATIENTID; // default selection
        
          workItemListViewingModel *sourceModel = new workItemListViewingModel(this);
          QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
          proxyModel->setSourceModel(sourceModel);
          ui->workItemList->setModel(proxyModel);
          ui->workItemList->verticalHeader()->setVisible(true);
          ui->workItemList->horizontalHeader()->setVisible(true);
          ui->workItemList->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
          ui->workItemList->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
          connect(ui->workItemList->verticalHeader(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(workItemSelectedForAnalysis(int)));
        }
        

        Here is my slot that is connected to the load button:

        oid workItem::on_listFileNameButton_clicked() 
        { 
          workItemListFileName=QFileDialog::getOpenFileName(this, tr("Work Item List File"), "", tr("Work Item List Files (*_wilist.*)"));
          workItemListEntries.clear();
          if (!loadWorkItemListFromFile(workItemListFileName, workItem::Json)) {
              std::cerr << "!fromJsonworkItem.loadWorkItem(Json) test tripped" << std::endl;
              return;
          }
          QFileInfo fi(workItemListFileName);
          ui->listFileNameButton->setText(fi.fileName());
          emit reloadListToModel();
        };
        

        As you can see, I am emitting a signal to the model class, as what I think is the right way to do it. In fact, the signal is causing the following function of the model class to run as expected:

        oid workItemListViewingModel::reloadList()
        {
          qDebug() << "into reload list";
          emit dataChanged(parent->ui->workItemList->indexAt(parent->ui->workItemList->rect().topLeft()), parent->ui->workItemList->indexAt(parent->ui->workItemList->rect().topLeft()));
        }
        

        But alas, nothing is displayed in the table view!

        Thank you for any help,
        Andy

        [edit: added missing coding tags ```SGaist]

        B Offline
        B Offline
        buckler
        wrote on last edited by
        #3

        @buckler Some more on this. My attempt to create topLeft and bottomRight index's seems to be failing. It always responds with -1,-1, in both cases. Is there a better wya to do this?

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          What does loadWorkItemListFromFile do ?

          B Offline
          B Offline
          buckler
          wrote on last edited by
          #4

          @SGaist (Sorry SGaist, I saw your question after positing my last comment, and then the forum held off my further reply to your question for a period.)

          loadWorkItemListFromFile reads the data from a file into the data structure connected o the view by the model:
          bool workItem::loadWorkItemListFromFile(QString fileName, workItem::saveFormat saveFormat)
          {
          //QFile loadFile(saveFormat == Json
          // ? QStringLiteral(fileName)
          // : QStringLiteral("junk_test_wilist.dat"));
          QFile loadFile(fileName);

          if (!loadFile.open(QIODevice::ReadOnly)) {
          QErrorMessage *error = new QErrorMessage (0);
          error->showMessage(tr("Warning: Cannot open work item list file for loading."));
          return false;
          }

          QByteArray saveData = loadFile.readAll();

          QJsonDocument loadDoc(saveFormat == Json
          ? QJsonDocument::fromJson(saveData)
          : QJsonDocument::fromBinaryData(saveData));

          readWorkItemList(loadDoc.object());

          return true;
          }

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            I didn't realized: You are trying to get indexes through a point that's the top left of the widget which is outside the region of interest. But in any case, that's completely wrong. You model should not try to access any widget, that's not its role.

            I suspect readWorkItemList translates your json to something like a QStringList ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • B Offline
              B Offline
              buckler
              wrote on last edited by
              #6

              Yes, readWorkItemList does read in a QList from a json file (not a string list, but a list of more complex objects, my "workItems").

              In further considering this since your reply, I was trying to understand the expectations of the model approach, it seemed to me that the idea of ripping out and replacing an entire dataset like loadWorkItemList does may not be the intended use case. So I tried a different approach this evening. In between load operations, the model editing, row insertion/deletion, sorting, all works fine. So, on a new load, I have succeeded by freeing up the model as well as its proxy, clearing the underlying QList, then loading a re-doing the setModel call. I now have this working fine, so in that sense, I could mark the issue solved. But I would be interested in your thoughts on this. It seems like you may agree, based on your comments that the signal which is sent to issue a dataChanged call seems to be what you say is wrong. My new method doesn't require this, so maybe I have come to a good solution?

              Thoughts welcome, but it is working now so I do not want to use your time inconsiderately.

              Andy

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                There's no need recreate the model from scratch and setting it again on the views.

                If you replace the complete list it should as simple as:

                beginResetModel();
                clear old list
                set new list
                endResetModel();
                

                and you should be good to go.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                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