[solved] Load XML Data in Comboboxes?



  • 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.



  • 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)?



  • 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?


  • Lifetime Qt Champion

    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.



  • 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?



  • 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. ;)


  • Lifetime Qt Champion

    You're welcome !

    Please update the thread's title to solved :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.