Save Adress and Load automatically

  • Hey all,
    i need some help please.
    Here is a little programm:

    What the programm should do?

      1. I write a String (16 characters for example: SWT1:SET:YV:LKN=5404) into the textEdit Widget.
      1. Afterthat i want to save this Adress
      1. I press the Button "Speichern" [Save]
      1. Now programm should save this adress in a .txt file or similar file AND the adress must be written in the combobox now. So for example I saved 5 Adresses, so the combobox must have 5 Adresses, that i can select...
    • programm should load saved adresses (.txt file) automatically when I do start the programm

    Can someone help me please`?

  • Two ways to do so, use the QFile with QTextStream to store/read data from a text file.
    Otherwise use QSettings class to store the files in registery/ini file etc depending on OS used.
    Hope this gives some idea what to do.

  • EDIT: Or maybe more easyer:

    I save the QStrings in the ComboBox... Someone idea how to do?

    Yes, thaks for your answer...
    But can someone give me a concret exapmle please?

    The first what I have to do is to read the text from the textbox:
    @void MainWindow::speichereText(QString text) {
    QString a;
    a = (ui->lineEdit_3->text());
    text = a;
    The second step: I have to write it into the .txt File:
    @QFile file("out.txt");
    if (! | QIODevice::Text))

    QTextStream out(&file);
    out << "The magic number is: " << 49 << "\n";@

    How Can I include here the Text, that i read from textbox?

    The third: I have to write this text from .txt file in the Combobox as a List:
    Here I dont also know HOW I can combine the text from .txt to ComboBox

  • Lifetime Qt Champion


    Please, have a look at the documentation, "QComboBox::addItem":

  • Hi,
    Don't expect us to code the simplest of things. The QFile and QCombobox documentation is perfectly clear, but since your a noob, I give it a short example code:
    void MainWindow::SaveDataToFile(QComboBox * Box_p)
    int NmbrOfItems (0);

    if (Box_p->count() > 0)
        // STart a file &#40;try static function for new/open file?
        QFile File_fl(&"Your filename here"&#41;;
        QTextStream outFile(&File_fl&#41;;
        if (; == true)
            // We have items in the list
            for (NmbrOfItems = 0; NmbrOfItems < Box_p->count(); NmbrOfItems++)
                // Add all items to the file
                outFile << Box_p->currentText();
                outFile << "/n";    // or any other seperation character!

    The reading of data goes the same way, but then do read from file until end.

Log in to reply

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