Important: Please read the Qt Code of Conduct -

Seeking for a solution faster than making databases,not familiar enough with Qsettings and Qsessionmanager,in order to save all the inputs given?

  • hello everyone,glad to find such a place,hope to reach my gole... Ive made some mainwindow.ui forms (Ui forms) with lots of lineedits,comboboxes,radiobuttons and so on, every time user runs the program and enters his inputs and makes choices in comboboxes and.... , the main goal Im looking forward to reaching is to be able to save the whole inputs and choices in a thing such as a file or a database or.... for each session and separately; I am already almost able to make a database with some tables and in each table build some columns representing each object;
    for example suppose I have only one text box,i mean lineedit, and one combobox, So i make one table with two columns,each of them for one of objects, and then insert the current text of them to the rows of the table and it is saved! but two problems exist !:

    1:creating ,inserting and selcting from them for when i want to LOAD it at the beginnig of next session and set the currents texts to those already saved in database, for this amount of data and inputs is very very difficult with so many needs of writing QUERIES in QSQLITE with SQL language, (when you write a query wrongly , for example in @query.exec("insert into PUMP values('2 cylinders')");@

    if you write intoo instead of into or...., the Qt debugger doesnt report any mistakes or bugs!!!!!!!!!!!! and you cant be aware whether you have writen your query correctly or not!!!!!!!!!! in fact whatever goes through the quotation of @query.exec("")@

    is correct but not all of them work!!!! :( ) and consequently i want to avoid database and SQL language!!!
    2:when the SAVEbutton is clicked i can open a database then create table and then insert into rows the inputs given, with this code e.g.
    @void MainWindow::on_SAVEbutton_clicked()
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    QSqlQuery query;
    query.exec("create table if not exists pump (type varchar(20) primary key)");
    query.exec("insert into PUMP values('2 cylinders')");
    query.exec("insert into PUMP values('3 cylinders')");@

    the problem is that every session the SAVEbutton is clicked this codes get run and always onle ONE database is made and open that is named standard_db.sqlite here ,but the goal was to have a unique and separate database for each session in order to be able to save all the sessions but this way not only is the last session not saved but also because each time the already-made database gets open ,new data are inserted into it without overriding and cleaning it by default ,which means after the first session your database turns into Rubbish!!!uselessssss :(

    Besides, as I said I have More than one ui form file meaning more than one set of lineedits and comboboxes and inputs and i need to save all of the inputs of all of the ui forms at once (each has its widgets and is different from others) and this makes the work even harder!! :(( Plz Help....

    now after this long story about difficulties and my problems in Database, I want a better solution for it,Ive heard some rumors about Qsettings and Qsessionmanager but no one has given a useful,prepared code of it to make me able to save the current State of my inputs and comboboxes choices....If anyone happens to perhaps have a code of it or hint about what i should do even in database solution ,I`m waiting for seeing it ...

    Thank U all ;)

  • Try QSetting , a very simple and useful class.

  • have you tried QTextStream or QDataStream? both provided with the solution to save your time by stream reading and writing ability. the only matter you should be care about due to the stream way of pushing and popping, data would be accessible by same order. so you might consider making assist functions in order to save and restore your data.

  • TanQ but as I said Ive searched a lot about it and know these two names,Qsetting and Qtextstream in Qfiles, but dont know how to use it,how to save a combobox choice or radiobutton choice in a text file!!!? isnt there a way to save the whole things automatically? since theres no efficient and useful example like of mine in Qassisstant Please answer a bit more sensible with some codes to do it....

  • Did you really see the QSettings description in Qt SDK? The example code is too clear!!
    Basic Usage

    When creating a QSettings object, you must pass the name of your company or organization as well as the name of your application. For example, if your product is called Star Runner and your company is called MySoft, you would construct the QSettings object as follows:
    QSettings settings("MySoft", "Star Runner");
    QSettings objects can be created either on the stack or on the heap (i.e. using new). Constructing and destroying a QSettings object is very fast.

    If you use QSettings from many places in your application, you might want to specify the organization name and the application name using QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName(), and then use the default QSettings constructor:
    QCoreApplication::setApplicationName("Star Runner");
    QSettings settings;
    (Here, we also specify the organization's Internet domain. When the Internet domain is set, it is used on Mac OS X instead of the organization name, since Mac OS X applications conventionally use Internet domains to identify themselves. If no domain is set, a fake domain is derived from the organization name. See the Platform-Specific Notes below for details.)

    QSettings stores settings. Each setting consists of a QString that specifies the setting's name (the key) and a QVariant that stores the data associated with the key. To write a setting, use setValue(). For example:
    settings.setValue("editor/wrapMargin", 68);
    If there already exists a setting with the same key, the existing value is overwritten by the new value. For efficiency, the changes may not be saved to permanent storage immediately. (You can always call sync() to commit your changes.)

    You can get a setting's value back using value():
    int margin = settings.value("editor/wrapMargin").toInt();

    [Edit: Added @ code wrappers -- mlong]

  • I really thank you for your attention but honestly i had read this before but the problema still exist :(
    ok,now where the Qsetings with its data in it is saved and how to access it ,as i said for each session user runs the program and gives different input and data ,one file should be created in order to make him load each one he wants in next session helping him now give the inputs and data again for the second time.....the goal is to avoid inputting repetitive data!!!

  • QSetting store datas in registry under Microsoft windows. If you want to access the data you just call the QSetting's value function. If each person have one setting , I don't think QSettings is a good choice. Try to write the datas which user input to one file ,you can use QFile, Just call QFile's open, write. read. You also can use the QDatabase to restore every person's data.

  • "QDataStream": is actually quite easy to use. Just pass a QFile and push the data as needed; combine it with a bit of introspection and you'll end up with serialization in just a few lines of code.
    void saveState(QWidget *widget, const QString &fileName)
    QFile stateFile(fileName);
    if ( | QIODevice::Truncate) == true)
    QDataStream stateStream(&stateFile);
    foreach (const QWidget childWidget, widget->findChildren<QWidget>())
    if (childWidget->property("serializable").toBool() == true)
    stateStream << childWidget->objectName()
    << childWidget->metaObject()->userProperty().read(childWidget);

    void restoreState(QWidget *widget, const QString &fileName)
    QFile stateFile(fileName);
    if ( == true)
    QDataStream stateStream(&stateFile);
    while (stateStream.atEnd() == false)
    QString name;
    QVariant value;

            stateStream >> name >> value;
            QWidget *childWidget = widget->findChild<QWidget*>(name);
            if ((childWidget != 0) && 
                (childWidget->property("serializable").toBool() == true))
                childWidget->metaObject()->userProperty().write(childWidget, value);

    Brain to terminal.

  • [quote author="henryxuv" date="1345511421"]QSetting store datas in registry under Microsoft windows. If you want to access the data you just call the QSetting's value function. If each person have one setting , I don't think QSettings is a good choice.[/quote]
    On a sidenote: QSettings "does not neccessarily": store the settings in the registry; it is just the default behaviour. It is very well suited to store values for "different users":, as well as storing "multiple versions":

  • Thanks, I got it. The QSettings is powerful than what I supposed. Are you familia with QTcpSocket? Lukas.

  • Yes, but if you have a question please create your own discussion, or, if it is related to an "already existing discussion":, raise your question there.

Log in to reply