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. [SOLVED] Saving custom user settings in Qt
QtWS25 Last Chance

[SOLVED] Saving custom user settings in Qt

Scheduled Pinned Locked Moved General and Desktop
25 Posts 6 Posters 17.8k 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.
  • F Offline
    F Offline
    fs_tigre
    wrote on last edited by
    #1

    Hi,

    I'm currently saving and loading the position and the size of the window as follow.

    @void MainWindow::saveSettings()
    {
    QSettings settings("tuDesign", "Tool");
    settings.setValue("geometry", saveGeometry());
    }

    void MainWindow::loadSettings()
    {
    QSettings settings("tuDesign", "Tool");
    restoreGeometry(settings.value("geometry").toByteArray());
    }

    void MainWindow::on_pushButton_saveSettings_clicked()
    {
    saveSettings();
    }@

    ... everything is working fine but I would like to be able to save some other things not just the window settings for instance I have a color picker that changes the color of some recangles and I was wondering if it was possible to save the last color picked by the user.

    @void MainWindow::on_pushButton_ColorPick_clicked()
    {
    QColor myColor;
    myColor = QColorDialog::getColor(Qt:blue), this);
    }@

    How can I save the last color picked by the user when the saveSettings() function is called?

    Thanks a lot!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      QSettings can save QVariant and QColor can be converted to/from QVariant.

      So in your project, create a class that stores configuration parameters and recent values (e.g. recently browsed directory in dialogs, or your recently selected color) and that handles saving/loading them with QSettings (or more sophisticated with XML).

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fs_tigre
        wrote on last edited by
        #3

        Wow, that sounds a little complicated. Can you show me a little example?

        Thanks a lot!

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Here is an example:

          ...saving the color:

          @void MyClass::saveSettings() {

          QSettings mySettings("My Company", "App Name");
          
          mySettings.beginGroup("MainWindow");
          
          // The getColor() member function/method return a QColor object.
          
          mySettings.setValue("MyColor", getColor().rgba());
          
          mySettings.endGroup();
          

          }// saveSettings@

          ...loading the color:

          @void MyClass::loadSettings() {

          QSettings mySettings("My Company", "App Name");
          
          mySettings.beginGroup("MainWindow");
          
          if(mySettings.contains("MyColor")) {
          
              QColor myColor;
          
              myColor.setRgba(mySettings.value("MyColor").toUInt());
          
              // Now you can restore the saved color.
          
              setColor(myColor);
          
          }// if
          
          mySettings.endGroup();
          

          }// loadSettings
          @

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fs_tigre
            wrote on last edited by
            #5

            I will give it a try. Thanks a lot for your help!

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fs_tigre
              wrote on last edited by
              #6

              Hi,

              Josué V. Herrera
              I have been playing around with the code you provided but I cannot make it to load the settings.

              Originally I was getting an error saying that setColor and getColor were not declared in this scope...

              for the getColor I changed it to
              @mySettings.setValue("MyColor", myColor);@
              Where myColor is a global variable placed in the constructor.

              This is the initial value of myColor variable.
              @myColor = QColor(85, 170, 0, 110);@
              Problem solved for the getColor error.

              But I can not make it to read the last color saved. I know its working because I use the qDebug() and when I run the saveSettings function it show the last color being saved.

              Any Idea what can I do (try) to make the setColor() function work? I know the setColor is not part of the QSettings so I think I'm missing a piece of code here.

              Thanks a lot.

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Well, setColor and getColor both are just hypothetical member functions, that was just an example.

                My suggestion here was to have two public methods, where you could set/get the current color to/from a private QColor object. Something like this:

                myyclass.h:
                @public:

                inline void setColor(const QObject &myCurrentColor) {

                myColor = myCurrentColor;

                }// setColor

                inline const QColor& getColor() {

                return myColor;

                }// getColor

                private:

                QColor myColor;@

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fs_tigre
                  wrote on last edited by
                  #8

                  I see, make sense, let me give a try.

                  Thank you for your help.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    And please... try not to use global variables, especially not for things like this.

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      fs_tigre
                      wrote on last edited by
                      #10

                      I have been trying to put the code Josué V. Herrera provided to me but I'm having a hard time making it work since he presented a few C++ concepts here, such as...

                      @inline const QColor& getColor()
                      inline void setColor(const QObject &myCurrentColor)@

                      Inline functions?
                      QColor before the function name?
                      Reference functions (&getColor)?

                      All these are concepts I still don't fully understand in C++, I don't know how to prototype these type of functions nor how they work, I'm LOST.

                      I know I know, but before I give up with this I want to give it another shot, in fact I already other methods to avoid bothering you guys again, such as writing and reading the color from/to a file, but unfortunately I ran into problems, I don't thick it is possible to convert QColor to QString.

                      I think this is the most challenging thing for me so far, I have spent HOURS trying but no luck. I'm going to show my whole mess in case there is someone willing to help me. Feel free to pass since I can imagine how frustrating is got to be dealing with people like me, but I have tried hard, believe me.

                      All I need is to save the last color selected by the user (colorPart). The code presented here of course doesn't work and its presented as Josué V. Herrera gave it to me as a starting point since I don't want to create more confusion with my wonderful coding methods, also despite the fact that I have a memory leak, I'm actually deleting QGraphicsScene in my real program.

                      CPP file:
                      @MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
                      {
                      ui->setupUi(this);
                      partColor = QColor(85, 170, 0, 110);
                      }

                      MainWindow::~MainWindow()
                      {
                      delete ui;
                      }

                      void MainWindow::on_pushButton_Draw_clicked()
                      {
                      scene = new QGraphicsScene(0 ,0 ,360,180);
                      ui->graphicsView->setScene(scene);
                      QBrush partBrush(partColor);
                      QPen partPen(Qt::white);
                      partPen.setWidth(2);
                      rectangle = scene->addRect(5, 5, 50, 50, partPen, partBrush);
                      }

                      void MainWindow::on_pushButton_colorPick_clicked()
                      {
                      partColor = QColorDialog::getColor(QColor(partColor), this);
                      }

                      //***************
                      // SAVE SETTINGS
                      //***************
                      void MainWindow::on_pushButton_saveSettings_clicked()
                      {
                      QSettings mySettings("CompanyName", "myApp");
                      mySettings.beginGroup("MainWindow");
                      mySettings.setValue("MyColor", getColor().rgba());
                      mySettings.endGroup();
                      }

                      void MainWindow::on_pushButton_loadSettings_clicked()
                      {
                      QSettings mySettings("CompanyName", "myApp");
                      mySettings.beginGroup("MainWindow");
                      if(mySettings.contains("MyColor"))
                      {
                      QColor myColor;
                      myColor.setRgba(mySettings.value("MyColor").toUInt());
                      setColor(myColor);
                      }
                      mySettings.endGroup();
                      }

                      inline void setColor(const QObject &myCurrentColor)
                      {
                      myColor = myCurrentColor;
                      }

                      inline const QColor& getColor()
                      {
                      return myColor;
                      }@

                      .h File:

                      @namespace Ui {
                      class MainWindow;
                      }
                      class MainWindow : public QMainWindow
                      {
                      Q_OBJECT
                      public:
                      explicit MainWindow(QWidget *parent = 0);
                      ~MainWindow();
                      inline void setColor(const QObject &myCurrentColor);
                      inline const QColor& getColor();
                      private slots:
                      void on_pushButton_Draw_clicked();
                      void on_pushButton_colorPick_clicked();
                      void on_pushButton_saveSettings_clicked();
                      void on_pushButton_loadSettings_clicked();
                      private:
                      Ui::MainWindow *ui;
                      QGraphicsScene *scene;
                      QGraphicsRectItem *rectangle;

                      QColor myColor;
                      QColor partColor;
                      QBrush partBrush;
                      

                      };
                      #endif // MAINWINDOW_H@

                      Thank you in advance!

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        You didn't specify the class to which these methods belong, and this is mandatory. Based in your example the declaration should look like this:

                        .cpp file:

                        @inline void MainWindow::setColor(const QObject &myCurrentColor)
                        {
                        myColor = myCurrentColor;
                        }

                        inline const QColor& MainWindow::getColor()
                        {
                        return myColor;
                        }@

                        ...I didn't include this to my example because my declaration was at the .h file.

                        ...about the "inline functions":http://en.wikipedia.org/wiki/Inline_function

                        ...the QColor before the function name means this function will return a QColor object, in this case a reference to a QColor object.

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          inlines don't go in implementation files, they go in the headers.

                          fs_tigre:
                          [quote]Inline functions?[/quote]
                          inline isn't much special. It is just a hint to the compiler on how to optimize the code. The basic idea is that the actual function call is replaced with whatever is in the inline function at the place where it is used. That removes a function call (which costs time), but can increase the size of the resulting code.

                          [quote]QColor before the function name?[/quote]
                          That's just the return type of the function. If you don't understand that, please get a basic C++ book...

                          [quote]Reference functions (&getColor)?[/quote]
                          No, the & belongs to the return type, so QColor& getColor(). The idea is that the function, instead of returning a copy of the color, returns a reference to the actual QColor data in the setting. That allows you to directly change it in one go. Compare:
                          @
                          //using value-based semantics
                          QColor col = myObject.getColor();
                          col = col.setAlphaF(0.5); //change the color in some way
                          myObject.setColor(); //now set the changed color back on the object

                          //using a reference
                          myObject.getColor().setAlphaF(0.5);
                          @

                          Note that references are not used often in Qt. Qt usually uses pointers instead. There are Qt functions that return a reference though, like QPoint::rx() and QPoint::ry(). Returning references can be useful, but I would recommend not to overuse it. You are basically giving away control from your class to whoever is using the API over internal values. That is only seldom what you want. For instance, once you gave away the reference, your class no longer is notified when the value changes and thus can not react on that or make sure the value if valid.

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            fs_tigre
                            wrote on last edited by
                            #13

                            Duplicated post.

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              fs_tigre
                              wrote on last edited by
                              #14

                              Thank you both for your help and for the good information provided.

                              [quote author="Josué V. Herrera" date="1344392547"]You didn't specify the class to which these methods belong, and this is mandatory. Based in your example the declaration should look like this:

                              .cpp file:

                              @inline void MainWindow::setColor(const QObject &myCurrentColor)
                              {
                              myColor = myCurrentColor;
                              }

                              inline const QColor& MainWindow::getColor()
                              {
                              return myColor;
                              }@
                              [/quote]

                              Well I actually forgot to add that in my example but I have tried that before and I keep getting errors.

                              error: no matching function for call to 'MainWindow::setColor(QColor&)
                              candidates are: void MainWindow::setColor(const QObject&)
                              In member function 'void MainWindow::setColor(const QObject&)':

                              error: no match for 'operator=' in '((MainWindow*)this)->MainWindow::myColor = myCurrentColor'
                              candidates are: QColor& QColor::operator=(const QColor&)
                              note:QColor& QColor::operator=(Qt::GlobalColor)

                              Any idea what could be causing these errors?

                              Thanks a lot

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andre
                                wrote on last edited by
                                #15

                                Read carefully. The errors quite clearly state what the problem is.

                                1 Reply Last reply
                                0
                                • F Offline
                                  F Offline
                                  fs_tigre
                                  wrote on last edited by
                                  #16

                                  bq. error: no matching function for call to ‘MainWindow::setColor(QColor&)

                                  I do have that function!

                                  Sorry if I dont see the obvious.

                                  1 Reply Last reply
                                  0
                                  • R Offline
                                    R Offline
                                    rishygupta
                                    wrote on last edited by
                                    #17

                                    is it possible QmlApplicationViewer can be used or manipulated in other class rather than main.

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      andre
                                      wrote on last edited by
                                      #18

                                      [quote author="fs_tigre" date="1344421000"]bq. error: no matching function for call to ‘MainWindow::setColor(QColor&)

                                      I do have that function!

                                      Sorry if I dont see the obvious.
                                      [/quote]

                                      No, you don't have that function. From your error (my highlight):

                                      [quote author="fs_tigre" date="1344420694"]error: no matching function for call to 'MainWindow::setColor(QColor&)
                                      candidates are: void MainWindow::setColor(const QObject&)
                                      In member function 'void MainWindow::setColor(const QObject&)':
                                      [/quote]

                                      What makes you think QColor& is the same as a const QObject&?

                                      And, while we're at it: QObject cannot be copied, so you cannot use it as a function argument the way you do. Pass around pointers instead.

                                      1 Reply Last reply
                                      0
                                      • F Offline
                                        F Offline
                                        fs_tigre
                                        wrote on last edited by
                                        #19

                                        Oh, I see, I do have setColor function but I'm passing a different parameter of what was specified in the header which makes it a completely different function. I was just using the code provided by Josué V. Herrera, and to be quite honest, if that's wrong I wont be able to find a solution or fix the problem. I do understand the error but I do not know the solution.

                                        Thanks

                                        1 Reply Last reply
                                        0
                                        • A Offline
                                          A Offline
                                          andre
                                          wrote on last edited by
                                          #20

                                          Code people give you here on the forum is usually not mend to be used as-is. Often, it is "brain-to-terminal": something you quickly write down in the forum editor itself, untested and probably even not compiling. The idea of such code is that it is a teaching tool; a way of pointing you towards a solution, but often not the solution itself. Copy pasting such snippets is not learning, as it does not promote understanding. If you can only solve your issue by using completely ready-made code offered by others, then you have a serious problem that can only be solved by either really starting to study, or by shelling out some money to start paying a consultant to help you with your project.

                                          1 Reply Last reply
                                          0

                                          • Login

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