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. Setter and Getter Style programming in Qt [solved]
QtWS25 Last Chance

Setter and Getter Style programming in Qt [solved]

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 3.0k 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
    Amry
    wrote on last edited by
    #1

    Hi guys, hope you all are having a good time.

    I am writing an application. And I am stuck at the point where I have to decide which way to go.
    So the problem:

    I have three signals coming from toolbar, namely:
    openAction - connected to open slot,
    saveAction - connected to save slot,
    startAction - connected to optimize slot,

    @
    void app::open()
    {
    filename = QFileDialogue... (get the file)
    if (!filename.isEmpty())
    {
    loadFile(fileName);
    }
    }
    bool app::loadFile(const QString &fileName)
    {
    QUiloader loader;
    file.open (QFile::Readonly);
    QWidget *myWidget = loader.load(&file, this);
    QList<QWidget *> wlist = myWidget->findchildren<QWidget *>();
    //I want to access this list wlist, also from another slot
    return true
    }

    void app::optimize()
    {
    //here I want the above wlist to be accessable
    //but I do not how to get it

    //and I am creating a window here, dialog let say
    QDialog *myDialog = new Dialog(this);
    //add some widgets on it,
    //want this mydialog to be accessible in save() slot

    }

    void app::save()
    {
    //here I would like to get the above mydialog
    //and save it with some methods
    QFormbuilder builder;
    QFile file("somefile.ui");
    builder.save(&file, mydialog)
    }
    @

    I was thinking to write setter and getter method with some class for setting the list in loadfile, getting in optimize(), and do the same for save().

    Since I am really familiar with this approach, I would like to know which way is the best to go for this problem.

    I really appreciate any help.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      I suggest to include both variables in your class app as the private members.
      Then you will be able to access them anytime you need them.
      Just don't forget to verify that they are valid at that time.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Amry
        wrote on last edited by
        #3

        Hi,

        Thanks for quick answer.
        So let me write if I got you write or not:

        Declare them in header file of app.h as private variables

        like:

        @
        private:
        QList<QWidget *> *wlist;

           QWidget *myWidget;
        

        @

        And they will be accessible everywhere inside app class?
        If yes, I have already have them as a private there, as it is written above,
        but it does not work yet.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          If you have declared them in your class then you don't need to declare them in the functions.
          @
          bool app::loadFile(const QString &fileName)
          {
          ....
          myWidget = loader.load(&file, this);
          wlist = myWidget->findchildren<QWidget *>();
          // Now it is assigned and will be available in all other
          // function-members of your class
          return true
          }
          @

          The same is for myDialog.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Amry
            wrote on last edited by
            #5

            ok,

            So let me show what I have ;
            in my app.h file:
            @
            private:
            QList<QWidget *> *wlist;
            QWidget *mywidget;
            //can it be QDialog, I assumed QDialog is also Widget
            @

            And when I remove Qlist<QWidget *> from wlist, and write as you did.
            I end up with this error:

            error: cannot convert 'QList<QWidget*>' to 'QList<QWidget*>*' in assignment.
            I could not figure out what that could mean.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #6

              You declared wlist as a pointer to QList. And trying to use it as non-pointer in the function.
              To fix it
              @
              private:
              QList<QWidget *> wlist;
              QWidget mywidget;
              QDialog
              myDialog;
              @

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Amry
                wrote on last edited by
                #7

                Hey,

                You saved a couple of hours/day of my time.

                Thanks.

                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