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. Seeking for a solution faster than making databases,not familiar enough with Qsettings and Qsessionmanager,in order to save all the inputs given?

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

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 3.5k Views
  • 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.
  • A Offline
    A Offline
    amin
    wrote on 19 Aug 2012, 23:13 last edited by
    #1

    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");
    db.setDatabaseName("standard_db.sqlite");
    db.open();
    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 ;)

    1 Reply Last reply
    0
    • H Offline
      H Offline
      henryxuv
      wrote on 20 Aug 2012, 02:44 last edited by
      #2

      Try QSetting , a very simple and useful class.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mohsen
        wrote on 20 Aug 2012, 05:06 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          amin
          wrote on 20 Aug 2012, 11:15 last edited by
          #4

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

          1 Reply Last reply
          0
          • H Offline
            H Offline
            henryxuv
            wrote on 20 Aug 2012, 12:42 last edited by
            #5

            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::setOrganizationName("MySoft");
            QCoreApplication::setOrganizationDomain("mysoft.com");
            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]

            1 Reply Last reply
            0
            • A Offline
              A Offline
              amin
              wrote on 20 Aug 2012, 21:15 last edited by
              #6

              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!!!

              1 Reply Last reply
              0
              • H Offline
                H Offline
                henryxuv
                wrote on 21 Aug 2012, 01:10 last edited by
                #7

                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.

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on 21 Aug 2012, 06:57 last edited by
                  #8

                  "QDataStream":http://doc.qt.nokia.com/4.7-snapshot/qdatastream.html#details 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 (stateFile.open(QIODevice::WriteOnly | 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 (stateFile.open(QIODevice::ReadOnly) == 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.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on 21 Aug 2012, 07:00 last edited by
                    #9

                    [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":http://qt-project.org/doc/qt-4.8/qsettings.html#Format-enum store the settings in the registry; it is just the default behaviour. It is very well suited to store values for "different users":http://qt-project.org/doc/qt-4.8/qsettings.html#Scope-enum, as well as storing "multiple versions":http://qt-project.org/doc/qt-4.8/qsettings.html#QSettings-4.

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      henryxuv
                      wrote on 21 Aug 2012, 08:45 last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lgeyer
                        wrote on 21 Aug 2012, 10:27 last edited by
                        #11

                        Yes, but if you have a question please create your own discussion, or, if it is related to an "already existing discussion":http://qt-project.org/forums/viewthread/19603/, raise your question there.

                        1 Reply Last reply
                        0

                        1/11

                        19 Aug 2012, 23:13

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved