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. XML Data into QTableView: Some Problems: Empty Table, Messed up Header Data etc.
Qt 6.11 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 13.5k 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.
  • p3c0P p3c0

    @MeerMusik That's fine :) We too have been there :)
    Shouldn't you use appendRow instead of setHorizontalHeaderItem ? setHorizontalHeaderItem as the name says works on header.

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

    @p3c0 appendRow puts a "hard-coded" list into the Table. But the Data, that should be put into the Table, are in the XML File. So this is not exactly what I want. Putting and getting hundreds of Names, Adresses, Links etc. into and from a QList<QStandardItem *> is just too much.
    ++
    I have setup the Horizontal Labels now. All are in correct order. So the Horizontal Labels are fine - for now.

    1 (or more?) Problem(s) still exists: After a lot of Code changes, I am sure that this Part is somehow wrong:

    if(xmlReader.name() == "Stadt")
    {
      modelForTableViewMainView->insertColumn(modelForTableViewMainView->columnCount());
      modelForTableViewMainView->setData(modelForTableViewMainView->index(modelForTableViewMainView->columnCount(),0), xmlReader.readElementText());
    }
    //And so on for the other Names in the XML Files.
    

    As 12 additional Labels [Labeled 13 to 24] get added after "Eintragnummer (ID)" [the last Name in the XML File], when xmlReader.readElementText() has been executed. One Label per Name in the XML File. But what I want here is:

    All Data found in the XML File which i.e.:

    • Have the Name "Stadt" should be put in Column 0, Row 0 (and ongoing).
    • Have the Name "Stadtteil" should be in Column 1, Row 0 (and ongoing).

    As with the rest of the Database Entries.

    Can it be, that I have mixed up / twisted some parts of QTableView and the Model for the QTableView?

    Thanks for your patience!!

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

      Alright.

      When I try:

      if(xmlReader.name() == "Stadt")
      {
      //New Part - Start
      QString lolopolo;
      lolopolo = xmlReader.readElementText();
      QMessageBox::information(this, "Teste Abfrage Stadt:", lolopolo);
      //New Part - End
      
      modelForTableViewMainView->insertColumn(modelForTableViewMainView->columnCount());
      modelForTableViewMainView->setData(modelForTableViewMainView->index(modelForTableViewMainView->columnCount(),0), xmlReader.readElementText());
      }
      

      The QMessageBox shows the correct Element Text.
      But I am stuck right now, how to put the read Element Text into the QTable.

      I think, I try to play around with reading the Database File to a QStringList and put everything from there into the TableView. Not sure if this will work.

      Any Hint(s) where I did a fatal Error, would be appreciated :)
      Oliver

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

        Hi,

        You are trying to access an index that doesn't exist. Indexes goes from 0 to columnCount() - 1 (same rule applies for rows)

        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

          Hi,

          You are trying to access an index that doesn't exist. Indexes goes from 0 to columnCount() - 1 (same rule applies for rows)

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

          @SGaist
          Ouch. I thought by using it this way, I set up a new Index. sigh
          And what is the correct way? The QXmlStreamReader should be fine for this. Or not?

          1 Reply Last reply
          0
          • 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 Extensions
                                      • Unsolved