Results 1 to 5 of 5

Thread: Best way to access members in MainWindow from a child class?

  1. #1
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Best way to access members in MainWindow from a child class?

    In my MainWindow class, I'm creating a class and passing QLabels like so:

    Qt Code:
    1. new_class = new NewClass(QLabel1, QLabel2);
    To copy to clipboard, switch view to plain text mode 

    NewClass constructor:

    Qt Code:
    1. NewClass::NewClass(QLabel *qLabel1, QLabel *qLabel2) : qLabel1(qLabel1), qLabel2(qLabel2)
    2. {
    3. }
    To copy to clipboard, switch view to plain text mode 
    NewClass definition
    Qt Code:
    1. class NewClass : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. NewClass(QLabel *qLabel1, QLabel *qLabel2);
    7. private:
    8. QLabel *qLabel1;
    9. QLabel *qLabel2;
    10. }
    To copy to clipboard, switch view to plain text mode 

    The above method works fine and I am able to access and directly modify the QLabels in the main window from new_class. However, I am wondering if there is a better way to do this?

    I believe this method will get quite tedious when I may need to access and modify many more objects contained in the MainWindow (as I will have to pass every single widget!).

    Can I just pass a reference to the entire MainWindow?

    Any help or insight would be kindly appreciated

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Best way to access members in MainWindow from a child class?

    Assuming that you only want to update label's text, maybe better approach could be not passing any single widget pointer at all ?
    MainWindow owns the labels, so better design IMHO would be to define a class / struct for data modified in NewClass:
    Qt Code:
    1. typedef struct{
    2. QString labelText1;
    3. QString labelText2;
    4. QWhatever otherData;
    5. } data_to_pass;
    To copy to clipboard, switch view to plain text mode 
    Then define slot in MainWindow:
    Qt Code:
    1. void MainWindow::updateData(const data_to_pass& newData);
    To copy to clipboard, switch view to plain text mode 
    and signal in NewClass:
    Qt Code:
    1. signals:
    2. void sendNewData(const data_to_pass& newData);
    To copy to clipboard, switch view to plain text mode 
    Then you could add new data just by modifying the struct and MainWindow's updateData slot implementation.

    You can just modify the struct slightly to get control over more label's parameters ( size, position, text color ect. )

  3. The following user says thank you to stampede for this useful post:

    dmginc (19th January 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Best way to access members in MainWindow from a child class?

    I'd say that you should never need to pass pointers to widgets to some other widgets. Either use signals and slots directly or delegate the access to the widget to a class that owns the widget. In your case MainWindow should be the only class accessing the labels directly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #4
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Best way to access members in MainWindow from a child class?

    Quote Originally Posted by stampede View Post
    Assuming that you only want to update label's text, maybe better approach could be not passing any single widget pointer at all ?
    MainWindow owns the labels, so better design IMHO would be to define a class / struct for data modified in NewClass:
    Qt Code:
    1. typedef struct{
    2. QString labelText1;
    3. QString labelText2;
    4. QWhatever otherData;
    5. } data_to_pass;
    To copy to clipboard, switch view to plain text mode 
    Then define slot in MainWindow:
    Qt Code:
    1. void MainWindow::updateData(const data_to_pass& newData);
    To copy to clipboard, switch view to plain text mode 
    and signal in NewClass:
    Qt Code:
    1. signals:
    2. void sendNewData(const data_to_pass& newData);
    To copy to clipboard, switch view to plain text mode 
    Then you could add new data just by modifying the struct and MainWindow's updateData slot implementation.

    You can just modify the struct slightly to get control over more label's parameters ( size, position, text color ect. )
    That's a nice idea.
    However, I am actually using setPixmap() to show frames in the QLabel

    i.e.
    Qt Code:
    1. qLabel1->setPixmap(QPixmap::fromImage(frame));
    To copy to clipboard, switch view to plain text mode 

    So I would like to work DIRECTLY with qLabel1 within new_class.

    @wysota: So passing a reference to QLabel to new_class isn't a good idea?

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Best way to access members in MainWindow from a child class?

    Quote Originally Posted by dmginc View Post
    @wysota: So passing a reference to QLabel to new_class isn't a good idea?
    No, it isn't. If you want to show frames of an animation then either use QMovie or emit frames with a signal and connect them to a slot in QLabel.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    dmginc (19th January 2011)

Similar Threads

  1. Replies: 1
    Last Post: 9th January 2011, 07:20
  2. Abstract base class and inherited class members
    By JovianGhost in forum General Programming
    Replies: 3
    Last Post: 19th March 2010, 12:32
  3. How to access QGroupBox members?
    By gren15 in forum Newbie
    Replies: 2
    Last Post: 20th March 2009, 17:08
  4. access the members of the Form from a loop.
    By cbarmpar in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2008, 01:35
  5. Replies: 4
    Last Post: 26th June 2007, 19:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.