Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. XML Data into QTableView: Some Problems: Empty Table, Messed up Header Data etc.
Qt 6.12 LTS is out! See what's new in the release blog

XML Data into QTableView: Some Problems: Empty Table, Messed up Header Data etc.

Scheduled Pinned Locked Moved General and Desktop
25 Posts 3 Posters 21.4k Views 3 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #12

    It won't because you are asking an index for a column that doesn't exist. Just call the function with the correct value for column and you'll be fine. No problem with the use of QXmlStreamReader

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

    MeerMusikM 1 Reply Last reply
    0
    • SGaistS SGaist

      It won't because you are asking an index for a column that doesn't exist. Just call the function with the correct value for column and you'll be fine. No problem with the use of QXmlStreamReader

      MeerMusikM Offline
      MeerMusikM Offline
      MeerMusik
      wrote on last edited by MeerMusik
      #13

      @SGaist

      Oh my. My schizophrenic Brain make such easy things some times really hard. C++ is absolutely different than Delphi.
      With the following code, it finally reads the first Entry in the XML File:

      if(xmlReader.name() == "Stadt")
      {
          QString setItemTestCity;
          setItemTestCity = xmlReader.readElementText();
      
          xmlReader.readElementText();
          QMessageBox::information(this, "Teste Abfrage Stadt:", setItemTestCity);
          modelForTableViewMainView->setItem(0,0, new QStandardItem(setItemTestCity));
      }
      

      Next step: Pull more than 1 "Stadt" Entry from the XML File.

      Thanks :)

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

        You old technique, basically, was good, it was just missing the -1 to get the last available column

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

        MeerMusikM 1 Reply Last reply
        0
        • SGaistS SGaist

          You old technique, basically, was good, it was just missing the -1 to get the last available column

          MeerMusikM Offline
          MeerMusikM Offline
          MeerMusik
          wrote on last edited by
          #15

          @SGaist The problem was, I was too dumb to see where to put the "-1". :(

          Now I must find a way to read not only 1 but all "Stadt" Elements.

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

            Follow the XBel example, it should give you what you need

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

            MeerMusikM 1 Reply Last reply
            0
            • SGaistS SGaist

              Follow the XBel example, it should give you what you need

              MeerMusikM Offline
              MeerMusikM Offline
              MeerMusik
              wrote on last edited by
              #17

              @SGaist Thanks for the hint. I will take a look at it - tomorrow. Good night. And thanks for being continuously helpful and patient :) I am sure I will be back.

              Oliver

              1 Reply Last reply
              0
              • MeerMusikM Offline
                MeerMusikM Offline
                MeerMusik
                wrote on last edited by MeerMusik
                #18

                Hello.

                I decided to try my Luck for one more time without the XBEL Example. The following Code (Snippet) puts the Data from the XML File in the QTableView:

                if(xmlReader.name() == "Stadt")
                {
                QString setItemTestStadt;
                setItemTestStadt = xmlReader.readElementText();
                
                int row = tableViewMainViewIndex.row();
                int column = tableViewMainViewIndex.column();
                row = 0;
                column = 0;
                
                QStandardItem *item = new QStandardItem(QString(setItemTestStadt));
                modelForTableViewMainView->insertRow(xmlReader.readElementText().count());
                modelForTableViewMainView->setData(modelForTableViewMainView->index(row,column,tableViewMainViewIndex), xmlReader.readElementText());
                modelForTableViewMainView->setItem(row, column, item);
                }
                

                Cant believe that I have been so close for 2 weeks. //Facepalm

                <s>Next "Problem" that I have: The Header Labels are correctly resized to fit their Content. But the Items Text is not. Already looked through the Web and through the Forum. Do I really have to subClass $Something?

                Currently I try the following Settings:

                tableViewMainView->setVisible(false);
                QRect originalViewport = tableViewMainView->viewport()->geometry();
                QRect newViewport = originalViewport;
                newViewport.setWidth(std::numeric_limits<int>::max());
                 tableViewMainView->viewport()->setGeometry(newViewport);
                 tableViewMainView->resizeRowsToContents();
                 tableViewMainView->resizeColumnsToContents();
                 tableViewMainView->viewport()->setGeometry(originalViewport);
                 tableViewMainView->horizontalHeader()->resizeSections(QHeaderView::ResizeToContents);
                 tableViewMainView->verticalHeader()->resizeSections(QHeaderView::ResizeToContents);
                
                //And after reading the Data into the TableView
                tableViewMainView->setVisible(true);
                ```</s>
                EDIT: Never  mind. I forgot that that I must call resetColumnsToContents each time I update the TableView.
                
                Thanks again :)
                Oliver
                1 Reply Last reply
                0
                • MeerMusikM Offline
                  MeerMusikM Offline
                  MeerMusik
                  wrote on last edited by
                  #19

                  Morning Folks.

                  A new Question:

                  I have different Views where the different Data from the XML File get split. 1 thing I will try is:
                  When

                  • Row 0 in tableViewMainView is active / selected, then select
                  • all Items in Row 0 in tableViewAdditionalInfo

                  I tried different things but no luck yet.

                  //Not sure which Slot to use. Or if any of those SLOTs are even correct.
                  connect(tableViewMainView,SIGNAL(pressed(QModelIndex)),this,SLOT(selectCurrentRowDependingOnTableViewMainView()));
                  //What I try right now:
                  tableViewMainView->setSelectionBehavior(QAbstractItemView::SelectRows);
                  tableViewAdditionalInfo->setSelectionBehavior(QAbstractItemView::SelectRows);
                  
                  int selectedRow;
                  selectedRow = tableViewMainView->rowAt(tableViewMainViewIndex.row());
                  tableViewMainView->rowAt(tableViewMainViewIndex.row());
                  
                  tableViewAdditionalInfo->selectRow(selectedRow);
                  

                  Thanks

                  1 Reply Last reply
                  0
                  • MeerMusikM Offline
                    MeerMusikM Offline
                    MeerMusik
                    wrote on last edited by MeerMusik
                    #20

                    And another Problem.
                    I want to display the Text of the XML Element "Name" in 2 different Views. But the Rows 0 + Column + of the 2nd View stay empty:

                    if(xmlReader.name() == "Name")
                    {
                    //Set the Name in first View:
                    QString setItemName;
                    setItemName = xmlReader.readElementText();
                    int row1 = tableViewMainViewIndex.row();
                    int column1 = tableViewMainViewIndex.column();
                    row1 = 0;
                    column1 = 3;
                    QStandardItem *item = new QStandardItem(QString(setItemName));
                    modelForTableViewMainView->insertRows(modelForTableViewMainView->rowCount(),  xmlReader.readElementText().count(), tableViewMainViewIndex);
                    modelForTableViewMainView->setData(modelForTableViewMainView->index(row1,column1,tableViewMainViewIndex), xmlReader.readElementText());
                    modelForTableViewMainView->setItem(row1, column1, item);
                    if(xmlReader.readElementText().isEmpty())
                    {
                    xmlReader.readNext();
                    }
                    
                    //Set the Name in the 2nd View:
                    QString setItemName2;
                    setItemName2 = xmlReader.readElementText();
                    int row2 = tableViewAdditionalInfoIndex.row();
                    int column2 = tableViewAdditionalInfoIndex.column();
                    row2 = 0;
                    column2 = 0;
                    QStandardItem *item2 = new QStandardItem(QString(setItemName2));
                    modelForTableViewAdditionalInfo->insertRow(xmlReader.readElementText().count(), tableViewAdditionalInfoIndex);
                                     modelForTableViewAdditionalInfo->setData(modelForTableViewAdditionalInfo->index(row2,column2,tableViewAdditionalInfoIndex), xmlReader.readElementText());
                    modelForTableViewAdditionalInfo->setItem(row2, column2, item2);
                    if(xmlReader.readElementText().isEmpty())
                    {
                    xmlReader.readNext();
                    }
                    }
                    

                    I am not sure whats wrong. Even calling the "xmlReader.name() == "Name" at a later point , let the cells stay empty.
                    ++++
                    EDIT: I finally found a workaround:

                    //Instead of:
                    QStandardItem *item2 = new QStandardItem(QString(setItemName2));
                    //I do:
                    QStandardItem *item2 = new QStandardItem(modelForTableViewMainView->item(0,3)->text());
                    

                    But I still have the Problem mentioned 2 posts above: How can I select 2 Rows in different Views simultaneously?

                    Any hint is welcome :)

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

                      You can use the first view selectionChanged signal and then the second view selection model to do what you want.

                      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
                      • MeerMusikM Offline
                        MeerMusikM Offline
                        MeerMusik
                        wrote on last edited by
                        #22

                        Morning.

                        I connected the tableViewMainView:

                        connect(tableViewMainView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(selectCurrentRowDependingOnTableViewMainView()));
                        

                        I try to scrollTo() by using the following Method:

                        void CDatabase::selectCurrentRowDependingOnTableViewMainView()
                        {
                        QPoint pos;
                        int row=tableViewMainView->rowAt(pos.y());
                        int column=tableViewMainView->columnAt(pos.x());
                        if(modelForTableViewMainView->item(row,column)->isSelectable())
                        {
                        QModelIndex selectedRow1Index=modelForTableViewMainView->index(row,column);
                        tableViewAdditionalInfo->scrollTo(selectedRow1Index,QAbstractItemView::EnsureVisible);
                        }
                        }
                        

                        I get no Error Messages but it still does not work as intended.

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

                          Should the items rows/cols from the second view be the same has the one from the main view ?

                          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
                          • MeerMusikM Offline
                            MeerMusikM Offline
                            MeerMusik
                            wrote on last edited by
                            #24

                            Evening.
                            What I want is:

                            • If I select any Column in Row 0 in Table 1 = select Row 0 in Table 2
                            • If I select any Column in Row 5 in Table 1 = select Row 5 in Table 2
                              etc. etc.
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #25

                              In that case, use the QItemSelection parameters from the selectionChanged signal to retrieve these rows you want to select

                              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

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt
                              • Unsolved