Results 1 to 5 of 5

Thread: Is this good programming practice? And how do I declare only one instance?

  1. #1
    Join Date
    May 2009
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Red face Is this good programming practice? And how do I declare only one instance?

    Hey guys,

    I am trying to have a common widget which is going to appear on several pages of a stackedwidget. In my example below, I did not connect the pushButtons but assume that pushButton1 will ask mStacked to show the first widget while pushButton2 will ask mStacked to show the second widget.

    I've been declaring a few widgets as follows:

    Note: Not complete code, but you get the idea.

    Qt Code:
    1. Widget::Widget(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. QPushButton *pushButton1 = new QPushButton("Page 1");
    5. QPushButton *pushButton2 = new QPushButton("Page 2");
    6. QVBoxLayout *mainLayout = new QVBoxLayout;
    7. QStackedWidget *mStacked = new QStackedWidget;
    8.  
    9. mStacked->addWidget( commonWidget() ); //Declaration of Page 1
    10. mStacked->addWidget( commonWidget() ); //Declaration of Page 2
    11.  
    12. mainLayout->addWidget(mStacked);
    13. mainLayout->addWidget(pushButton1);
    14. mainLayout->addWidget(pushButton2);
    15. setLayout(mainLayout);
    16.  
    17. }
    18.  
    19. QWidget* Widget::commonWidget()
    20. {
    21. QLabel * mLabel = new QLabel("VariedText");
    22. QHBoxLayout *layout = new QHBoxLayout;
    23. QWidget* mWidget = new QWidget();
    24. layout->addWidget(mLabel);
    25. mWidget->setLayout(layout);
    26. return mWidget;
    27. }
    To copy to clipboard, switch view to plain text mode 


    I have two main questions:

    1. Is this a good way of declaring a commonWidget?
    2. If it's the usual way, is there a way to ensure that only one instance of mLabel is created? In otherwords, when I change the value of mLabel on one page, the same value is displayed on the other pages.

    Thanks much.

    Regards,
    Pembar

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

    Default Re: Is this good programming practice? And how do I declare only one instance?

    Quote Originally Posted by Pembar View Post
    1. Is this a good way of declaring a commonWidget?
    It's fine although you could make the method const so that you can call it on constant objects and from other constant methods.

    2. If it's the usual way, is there a way to ensure that only one instance of mLabel is created?
    No, if you want to show the widget on multiple pages, you have to have multiple copies of it. You can cheat by pointing several buttons to the same page so that you can use a single widget but only if it makes sense in your case.

    In otherwords, when I change the value of mLabel on one page, the same value is displayed on the other pages.
    You can use signals and slots for that.
    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.


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

    Pembar (13th May 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Is this good programming practice? And how do I declare only one instance?

    Quote Originally Posted by wysota View Post
    In otherwords, when I change the value of mLabel on one page, the same value is displayed on the other pages.
    You can use signals and slots for that.
    And then how would you like to get a pointer to the label? I know it's possible, but for that, to subclass QWidget regularly and provide public functions could be easier.

  5. #4
    Join Date
    May 2009
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is this good programming practice? And how do I declare only one instance?

    Got it, Thanks

    Edit:

    Just to give an idea on how I solved it. Each time I declared a new *mLabel, I pushed it into a List of pointers.

    I then connected my "Change" pushbutton to go through all the Labels and changed every single one of them.

    Regards,
    Pembar

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

    Default Re: Is this good programming practice? And how do I declare only one instance?

    Quote Originally Posted by Lykurg View Post
    And then how would you like to get a pointer to the label?
    You have a pointer available when you are creating the widget. So its enough to setup the connections right after you have setup all the pages.
    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.


Tags for this Thread

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.