[SOLVED]Remember Last LineEdit data



  • My App takes input for Id and Birthdate in QLineEdit and sends the form data as http post request.
    Is it possible for the last entered input to stay when app is closed and then restarted??



  • Of course. Make it persistant via storing it to a file, or using the QSettings class. Either way, you will need to load the information back on the next run.



  • Andre , Thanks for the Reply....but i really couldn't make out how can QSettings be used for this purpose...cazn u please help me with some example code? I ll be grateful to you :)



  • To effectively use QSettings, it's best to first make sure you use QApplication properly. After creating your QApplication instance, be sure to set the application name, organization, url, etc.

    To store a value, you create a QSettings instance, and use setValue(). You can group values into subgroups if you want, but for the purpose of this excersise, don't bother.

    Then, if you want to read back your value (on first show), you also create a QSettings instance, and use the value() method with an appropriate default value. If there is data found, the default value would be used.



  • This the main.cpp file of my application ...Here idline is the pointer to QLineEdit taking input for Id
    @#include<QApplication>
    #include<websis.h>
    #include"gui.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    a.setApplicationName("Websis Desktop Application");
    a.setOrganizationName("LUG Manipal");
    a.setOrganizationDomain("rajatgupta431@gmail.com");
    qApp->addLibraryPath("C:/Qt/4.8.4/plugins/imageformats");

    QSettings *settings;
    settings = new QSettings;
    
    makegui gogoi;
    QVariant idvar;
    idvar.setValue(gogoi.idline->text());
    settings->setValue("Id",&idvar);
    gogoi.idline->setText(settings->value("id","not set").toString());
    
    return a.exec&#40;&#41;;
    

    }
    @
    Results are not as expected...Please look into it :)



  • You're not telling us what is "expected", so it is hard to judge where your code deviates from your plan

    Just use a QSettings object on the stack (line 14 & 15)

    What is makegui gogoi; doing? (line 17)

    Why do you set a pointer to a QVariant in your settings? (line 20)


  • Moderators

    Also, you're using "Id" in line 20, and "id" in line 21.



  • [quote author="Andre" date="1363711273"]# You're not telling us what is "expected", so it is hard to judge where your code deviates from your plan

    Just use a QSettings object on the stack (line 14 & 15)

    What is makegui gogoi; doing? (line 17)

    Why do you set a pointer to a QVariant in your settings? (line 20)[/quote]

    Expected is that , when user closes and restarts the application , the QLineEdit fiels already has the previously Entered Text...makegui gogoi ; declares the object of the class makegui that is responsible for making the gui part and then calling other neseccary funtions.....



  • [quote author="mlong" date="1363711569"]Also, you're using "Id" in line 20, and "id" in line 21.
    [/quote]

    Fixed It :)


  • Moderators

    You can't just "bind" a QSettings value to a widget, which I think is what you're trying to do. QSettings doesn't work that way.

    You'll need to store the text from your QLineEdit by calling QSettings::setValue() each time the text changes (either as the user type it, or when you process the values -- however it's done in your gui.) This won't typically be done in main(). It is most likely best handled in your makegui class. Also, at the time you build/initialize your gui is when you'll use QSettings::value() to set your text.



  • [quote author="Andre" date="1363698916"]Of course. Make it persistant via storing it to a file, or using the QSettings class. Either way, you will need to load the information back on the next run.[/quote]
    Am unable to use QSettings for this...Can u guide me with the other Way of storing in A text file and then retrieving it from there?



  • Using a text file is not going to be any easier.



  • Can I get a line of code that saves text of QLineedit and on restarting the app , it is retrieved ?
    Please help , its very important for my project ...



  • No. The point is, that there isn't any such one line of code.

    You've already been given many hints, but you refuse to take the hints given.



  • This is What was needed, its Done :)
    @//read
    QSettings settings("settingName");
    QString aValue=settings.value("aKey").toString();

    //write
    QSettings settings("settingName");
    QString somevalue=textedit.text();
    settings.setValue("aKey",somevalue);
    @


Log in to reply
 

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