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] stack / heap object question
Forum Updated to NodeBB v4.3 + New Features

[solved] stack / heap object question

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 3.8k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    deleted28
    wrote on last edited by
    #1

    Is there a way to use an on stack created Object in event routine of the same class ?
    ... or is the only way to do this an on heap created object by using "new" ?

    mainwindow.h
    @private:

    Ui::MainWindow *ui;
    QSettings settings_stack;
    QSettings *settings_heap;@
    

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QSettings settings_stack("MySoft", "Star Runner");
    settings_stack.setValue("editor/wrapMargin", 42);
    
    qDebug() << settings_stack.organizationName();
    qDebug() << settings_stack.applicationName();
    qDebug() << settings_stack.value("editor/wrapMargin").toInt();
    

    }

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

    void MainWindow::on_pb_1_clicked()
    {
    qDebug() << settings_stack.organizationName();
    qDebug() << settings_stack.applicationName();
    qDebug() << settings_stack.value("editor/wrapMargin").toInt();
    }
    @

    thank you

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      In line 10, you are creating a local variable named settings_stack. It will be destroyed once the constructor ends (line 16). So what you access in line 25 is a completely different object.

      There are several ways to make it work, I highly recommend reading some book on C++ to get the general idea. The shortest solution right now (others would require a bit of rewriting) is this:
      @
      // Replace line 10 of your code with this:
      settings_stack = QSettings("MySoft", "Star Runner");
      @

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted28
        wrote on last edited by
        #3

        I tried this first, but:

        build error >> operator '=' is private

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Ah sure, it inherits from QObject, I forgot. OK, then you need a partial rewrite to use the heap-allocated version.

          This means that you need to change:
          @
          settings_stack. into settings_heap->
          @

          Because those will be pointers now. And the now-famous line 10 becomes:
          @
          settings_heap = new QSettings("MySoft", "Star Runner");
          @

          (Z(:^

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deleted28
            wrote on last edited by
            #5

            using the heap way is quite clear.

            I wanted to know if there is a possibility to manage this problem
            without "new".
            I guess : It's not possible, right ?

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              There is: create on stack every time you read or save your settings (so, no member variable in your class, only local variables). QSettings is quite fast, so you don't need to worry about performance (plus, settings are usually not being read or saved all that much, are they?).

              (Z(:^

              1 Reply Last reply
              0
              • D Offline
                D Offline
                deleted28
                wrote on last edited by
                #7

                This is just an example for learning stack/ heap usage.
                If i really need it, i would prefer the heap approach, anyway.

                Thank You

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  Right, then please remember that for "normal" C++ classes (QString, QByteArray, QPoint, etc.) the first approach with stack would work (see my initial answer). It does not work in the special case of QSettings, because all QObjects (including QWidgets) are non-copyable (they cannot be copied). This is because copying QObject would break the parent-child hierarchy (don't ask me how, though. Somebody made a decision about that years ago and it has stayed this way ever since).

                  (Z(:^

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    deleted28
                    wrote on last edited by
                    #9

                    Your recent post makes things clear now.
                    thanks again !

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi,

                      QSettings being a special case and to simplify a bit things, you should follow the example of the documentation that is a bit further:

                      @
                      QCoreApplication::setOrganizationName("MySoft");
                      QCoreApplication::setOrganizationDomain("mysoft.com");
                      QCoreApplication::setApplicationName("Star Runner");

                      QSettings settings;
                      @

                      It it'l make you code more readable and QSettings even easier to manage.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        deleted28
                        wrote on last edited by
                        #11

                        Ok, also interesting:

                        I set some values for in line 1, 2 and 3 for the actual application
                        and then create QSettings object in line 5 which already contains
                        the values for OrganizationName and ApplicationName, readable using QSettings::organizationName() and QSettings::applicationName() properties.

                        Whats about line 2 'OrganizationDomain' ?
                        Seems there is no property organizationDomain() in QSettings.
                        Is the domain also stored in Qsettings or what demonstrates line 2 in the context of QSettings ?

                        1 Reply Last reply
                        0
                        • sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #12

                          Please read the documentation for "QSettings":http://qt-project.org/doc/qt-5/QSettings.html Organization name is needed on Mac to properly save your settings (that is, to save them in a meaningful path/ filename).

                          (Z(:^

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            deleted28
                            wrote on last edited by
                            #13

                            bq. (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.)

                            So this part is only interesting for Mac users, right ?

                            1 Reply Last reply
                            0
                            • sierdzioS Offline
                              sierdzioS Offline
                              sierdzio
                              Moderators
                              wrote on last edited by
                              #14

                              Yes, but it is good practice to always use it. This way future porting is easier plus you can get support for new platforms for free, without refactoring.

                              (Z(:^

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                deleted28
                                wrote on last edited by
                                #15

                                ok, clear now, thx !

                                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