Setter and Getter Style programming in Qt [solved]



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



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



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



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



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



  • 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;
    @



  • Hey,

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

    Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.