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. how to load and save the combobox values in XML
Forum Updated to NodeBB v4.3 + New Features

how to load and save the combobox values in XML

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.0k 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.
  • sankarapandiyanS Offline
    sankarapandiyanS Offline
    sankarapandiyan
    wrote on last edited by
    #1

    I have done the code by using QSettings apart from this i want to load and save the combobox by using XML i tried a lot but i dnt find any result

    thanks in advance.

    K 1 Reply Last reply
    0
    • sankarapandiyanS sankarapandiyan

      I have done the code by using QSettings apart from this i want to load and save the combobox by using XML i tried a lot but i dnt find any result

      thanks in advance.

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @sankarapandiyan

      Did you check out Qt's XML examples?

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • sankarapandiyanS Offline
        sankarapandiyanS Offline
        sankarapandiyan
        wrote on last edited by
        #3

        yes @koahnig i am cleared in the example but cant implemnt in my code

        K 1 Reply Last reply
        0
        • sankarapandiyanS sankarapandiyan

          yes @koahnig i am cleared in the example but cant implemnt in my code

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @sankarapandiyan

          Well, my personal opinion is that you will have a lot of overhead there. However, that is the XML functionality provided by Qt.

          You may want to explain why you got stuck. Possibly someone can help.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            You can use the "Model Serialisation" module of this library to save and load the QCombobox::model. After you have done that you'll just need to prepend an element containing the current index:

            #include <QApplication>
            #include <QPushButton>
            #include <QLabel>
            #include <QComboBox>
            #include <QXmlStreamWriter>
            #include <QXmlStreamReader>
            #include <XmlModelSerialiser>
            #include <QVBoxLayout>
            #include <QDebug>
            QString saveCombo(const QComboBox* combo){
                XmlModelSerialiser modelSerialiser(combo->model());
                modelSerialiser.setPrintStartDocument(false);
                QString result;
                QXmlStreamWriter writer(&result);
                writer.writeStartElement("combobox");
                writer.writeStartElement("currentIndex");
                writer.writeCharacters(QString::number(combo->currentIndex()));
                writer.writeEndElement(); //currentIndex
                writer.writeStartElement("comboModel");
                modelSerialiser.saveModel(writer);
                writer.writeEndElement(); //comboModel
                writer.writeEndElement(); //combobox
                return result;
            }
            void loadCombo(QComboBox* combo, const QString& data){
                QXmlStreamReader reader(data);
                int currIdx = -1;
                XmlModelSerialiser modelSerialiser(combo->model());
                QString modelData;
                while (!reader.atEnd() && !reader.error()) {
                    switch(reader.readNext()){
                    case QXmlStreamReader::StartElement:
                        if(reader.name()=="currentIndex")
                            currIdx = reader.readElementText().toInt();
                        else if(reader.name()=="comboModel"){
                            reader.readNextStartElement();
                            modelSerialiser.loadModel(reader);
                        }
                        break;
                    default:
                        break;
                    }
                }
                if(reader.error())
                    qDebug() << reader.errorString();
                combo->setCurrentIndex(currIdx);
            }
            
            int main(int argc, char **argv) {
                QApplication app(argc, argv);
                QWidget mainWid;
                QVBoxLayout *mainlay = new QVBoxLayout(&mainWid);
                mainlay->addWidget(new QLabel("Save This",&mainWid));
                QComboBox* example = new QComboBox(&mainWid);
                example->addItem("a");
                example->addItem("b");
                example->addItem("c");
                mainlay->addWidget(example);
                mainlay->addWidget(new QLabel("Load This",&mainWid));
                QComboBox* loadedCombo = new QComboBox(&mainWid);
                mainlay->addWidget(loadedCombo);
                QPushButton* testButton = new QPushButton("Test",&mainWid);
                QObject::connect(testButton,&QPushButton::clicked,[example,loadedCombo](){
                    const QString savedCombo = saveCombo(example);
                    qDebug() << savedCombo;
                    loadCombo(loadedCombo,savedCombo);
                });
                mainlay->addWidget(testButton);
                mainWid.show();
                return app.exec();
            }
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            6
            • sankarapandiyanS Offline
              sankarapandiyanS Offline
              sankarapandiyan
              wrote on last edited by
              #6

              thanks a lot @VRonin

              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