Save Adress and Load automatically



  • Hey all,
    i need some help please.
    Here is a little programm:
    !http://s1.directupload.net/images/131104/p5xazoki.jpg(picture)!

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



  • 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:
    HOW?
    @QFile file("out.txt");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    return;

    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

    Hi,

    Please, have a look at the documentation, "QComboBox::addItem":http://qt-project.org/doc/qt-5.0/qtwidgets/qcombobox.html#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 (File_fl.open(QIODevice::ReadWrite&#41; == true)
        {
            // We have items in the list
            for (NmbrOfItems = 0; NmbrOfItems < Box_p->count(); NmbrOfItems++)
            {
                Box_p->setCurrentIndex(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.