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. [solved] Load XML Data in Comboboxes?
Forum Updated to NodeBB v4.3 + New Features

[solved] Load XML Data in Comboboxes?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 3.8k 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.
  • I Offline
    I Offline
    itsmemax
    wrote on 14 May 2013, 08:20 last edited by
    #1

    Hi, I need your help again. Because my English is too bad to explain it correctly, I've made a little picture out of it:
    http://i.imgur.com/SFBlLfe.png
    So I want the names in this XML file in my ComboBoxes (which are implemented in a for-loop in my tablewidget).. Here are my codes.

    untitled3.pro
    @#-------------------------------------------------

    Project created by QtCreator 2013-05-13T11:00:38

    #-------------------------------------------------

    QT += core gui
    QT += xml

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = untitled3
    TEMPLATE = app

    SOURCES +=
    main.cpp
    mainwindow.cpp
    ../uisConfTool/CuisList.cpp
    ../uisConfTool/CUisConf.cpp

    HEADERS += mainwindow.h
    ../uisConfTool/CUisConf.h
    ../uisConfTool/CUisList.h

    FORMS += mainwindow.ui

    OTHER_FILES +=
    @

    main.cpp
    @#include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "../../Qt-Creator/uisConfTool/CUisList.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

    {
    ui->setupUi(this);

    //falls Digital IO (Konstruktor)
    if (ui->comboBox->currentText() == "Digital I/O")
        ui->pushButton->setVisible(false);
    else
        ui->pushButton->setVisible(true);
    
    //Versionsnummer setzen
    int version = 1;
    ui->label_3->setText(QString("Version %1").arg(version));
    
    
    //Objekt UisList
    CUisList UisList;
    
    
    //Comboboxen laden
    //Start
    
    
    const int ROWS = 64;
    
    QComboBox *combos[ROWS];
    for (int r = 0; r < ROWS; ++r) {
        combos[r] = new QComboBox;
        ui->tableWidget->setCellWidget (r, 2, combos[r]);
    
    }
    
    //End
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
    {
    //falls Digital IO
    if (ui->comboBox->currentText() == "Digital I/O")
    ui->pushButton->setVisible(false);
    else
    ui->pushButton->setVisible(true);

    //falls Otto
    if (ui->comboBox->currentText() == "Otto")
        ui->tabWidget->setVisible(false);
    else
        ui->tabWidget->setVisible(true);
    if (ui->comboBox->currentText() == "Otto")
        ui->tableWidget->setVisible(false);
    else
        ui->tableWidget->setVisible(true);
    

    }
    @

    CuisList.cpp (I think it could help, I've got it from my superior)
    @#include <QFile>
    #include <QPair>
    #include "CUisList.h"

    /**

    • @author censored
    • Constructor.<br />
    • Initialisation.
      */
      CUisList::CUisList()
      {
      m_sAbsFileName = "";
      m_pFile = new QFile;
      m_pDoc = new QDomDocument;
      }

    /**

    • @author censored

    • open the xml configurationfile.

    • @param sFileName Name of the file
      */
      int CUisList::openFile(QString sAbsFileName)
      {
      qDebug() << QString(" open File: %1").arg(sAbsFileName);

      m_pFile->setFileName(sAbsFileName);

      if (m_pFile->open(QIODevice::ReadOnly) == true){
      qDebug() << QString("sucsessfull opened");
      m_sAbsFileName = sAbsFileName;
      m_pDoc->setContent(m_pFile);
      return 0;
      } else {
      qDebug() << QString("Error at open");
      return 1;
      }
      }

    /**

    • @author censored
    • parse the configuration file and build the mapping table.
    • @return Pointer to maping array
      */
      void CUisList::buildList()
      {
      qDebug() << QString("parse file ...");
      QDomElement GroupElement;
      QDomElement IoElement;

    // QString m_pArray = (QString*) calloc(iMaxArraySize,sizeof(QString));

    IoElement = m_pDoc->documentElement().firstChildElement("IO");
    if(IoElement.isElement()) {
        qDebug() <<  QString("found IO");
        while(IoElement.isElement()) {
            m_Pair.first = IoElement.firstChildElement("VAL").text().toInt();
            m_Pair.second = IoElement.firstChildElement("NAME").text();
            qDebug() << "fouund " << m_Pair.first << " - " << m_Pair.second;
            m_DescriptionList.append(m_Pair);
            IoElement = IoElement.nextSiblingElement("IO");
        }
    }
    

    }

    /**

    • @author censored

    • returns a pointer to a string idendifed by an id.

    • @return Pointer to the string
      */
      QString CUisList::getStringById(int iId)
      {
      QPair <int,QString> temp;
      QList <QPair<int,QString> >::iterator Iterator;
      for (Iterator = m_DescriptionList.begin(); Iterator != m_DescriptionList.end(); Iterator++) {
      temp = *Iterator;
      if(temp.first == iId) return temp.second;
      }

      return NULL;
      }

    /**

    • @author censored
    • Close the configuration file.

    */
    void CUisList::closeFile()
    {
    if(m_pFile) m_pFile->close();
    }
    @

    I would really appreciate your help.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      itsmemax
      wrote on 14 May 2013, 08:35 last edited by
      #2

      I've got a bit further... I reached to write in one String the first name.. Here's my code:

      @//Objekt UisList
      CUisList UisList;
      QString String;
      UisList.openFile("R:/source/Qt-Creator/uisConfTool/digiolst.xml");
      UisList.buildList();
      String = UisList.getStringById(1);@

      Now, how can I put this string in a Combobox (which was created in the for-loop)?

      1 Reply Last reply
      0
      • I Offline
        I Offline
        itsmemax
        wrote on 14 May 2013, 09:16 last edited by
        #3

        Got further again.

        @//Objekt UisList
        CUisList UisList;
        QString String;
        UisList.openFile("R:/source/Qt-Creator/uisConfTool/digiolst.xml");
        UisList.buildList();

        //Comboboxen laden
        //Start
        
        int sListSize = UisList.listSize();
        const int ROWS = sListSize;
        
        QComboBox *combos[ROWS];
        qDebug()<<String;
        for (int r = 0; r < ROWS; ++r) {
            String = UisList.getStringById(r);
            combos[r] = new QComboBox;
            ui->tableWidget->setCellWidget (r, 2, combos[r]);
            combos[r]->addItem(String, r);
        
        
        }
        
        //End@
        

        But, now it looks like this:
        http://i.imgur.com/1ZJwJ1O.png

        But, my superior wants it like this:
        http://i.imgur.com/TQnkCGS.png

        So, ALL of the names in the XML should be in ALL of the ComboBoxes... (Not used, RegH taste 1, RegH Taste 2... in ComboBox1 // Not used, RegH taste 1, RegH Taste 2... in ComboBox2 // Not used, RegH taste 1, RegH Taste 2... in ComboBox3 // ...)

        Please help?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 May 2013, 09:30 last edited by
          #4

          Hi,

          @
          for (int r = 0; r < ROWS; ++r) {
          String = UisList.getStringById(r);
          combos[r] = new QComboBox;
          ui->tableWidget->setCellWidget (r, 2, combos[r]);
          combos[r]->addItem(String, r); << you are only adding the item for the current row.

          }
          

          @

          Either add a method to your UisList that returns all the strings you need and use QComboBox's addItems or have a second loop that retrieve all the strings one by one to add them.

          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
          • I Offline
            I Offline
            itsmemax
            wrote on 14 May 2013, 09:32 last edited by
            #5

            Second loop was the thing, that i've been thinking of, too. But I've got no plan how to implement it. Could you do an example?

            1 Reply Last reply
            0
            • I Offline
              I Offline
              itsmemax
              wrote on 14 May 2013, 10:08 last edited by
              #6

              Finally... It's done:

              @//Objekt UisList
              CUisList UisList;
              QString String;
              QStringList AllStrings;
              UisList.openFile("R:/source/Qt-Creator/uisConfTool/digiolst.xml");
              UisList.buildList();

              //Comboboxen laden
              //Start
              
              int sListSize = UisList.listSize();
              const int ROWS = sListSize;
              
              QComboBox *combos[ROWS];
              
              for (int r = 0; r < ROWS; ++r) {
                  AllStrings << UisList.getStringById(r);
              }
              
              for (int r = 0; r < ROWS; ++r) {
                  String = UisList.getStringById(r);
                  combos[r] = new QComboBox;
                  ui->tableWidget->setCellWidget (r, 2, combos[r]);
                  combos[r]->addItems(AllStrings);
              
              }@
              

              Thanks to SGaist anyway. ;)

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 14 May 2013, 13:11 last edited by
                #7

                You're welcome !

                Please update the thread's title to solved :)

                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

                1/7

                14 May 2013, 08:20

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved