Results 1 to 11 of 11

Thread: QStackedLayout doesn't show pages

  1. #1
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QStackedLayout doesn't show pages

    Hi there,
    i created a QMainWindow with a toolbar and a QStackedWidget as central widget. This widget holds the content that should be displayed if one clicks a button in the toolbar. So for now, the main page that is dispayed at startup shows 5 different splash screens (simple images). Lets say it should. I'm using a connection from the actiongroup in the toolbar, to switch splashes when hovering a button in the toolbar.
    My first version simply used a QLabel that got equipped with a pixmap. Problem: Its slow and if the user is to fast in moving over the toolar, the splashes come one after the other with some noticable delay.
    I wanted to rewrite it using a QStackedLayout, but now the labels don't show up. If i set up a label without adding it to the Layout, it shows up just as it should. My code:
    Qt Code:
    1. mainPage::mainPage( QWidget *parent ): QWidget( parent ) {
    2. stack = new QStackedLayout( this );
    3. setLayout( stack );
    4. area0 = new QLabel( this );
    5. area1 = new QLabel( this );
    6. area2 = new QLabel( this );
    7. area3 = new QLabel( this );
    8. area4 = new QLabel( this );
    9.  
    10. stack->addWidget( area0 );
    11. stack->addWidget( area1 );
    12. stack->addWidget( area2 );
    13. stack->addWidget( area3 );
    14. stack->addWidget( area4 );
    15.  
    16. stack->setCurrentIndex( 0 );
    17. // QLabel *test = new QLabel(this);
    18. // test->setPixmap(QPixmap::fromImage(QImage(":/images/splash0.jpg")));
    19. }
    20.  
    21. void mainPage::loadSplashes() {
    22. area0->setPixmap( QPixmap::fromImage( QImage( ":images/splash0.jpg" ) ) );
    23. area1->setPixmap( QPixmap::fromImage( QImage( ":images/splash1.jpg" ) ) );
    24. area2->setPixmap( QPixmap::fromImage( QImage( ":images/splash2.jpg" ) ) );
    25. area3->setPixmap( QPixmap::fromImage( QImage( ":images/splash3.jpg" ) ) );
    26. area4->setPixmap( QPixmap::fromImage( QImage( ":images/splash4.jpg" ) ) );
    27. }
    28.  
    29. void mainPage::showContent( QAction* action ) {
    30. qDebug() << "showContent called: " << action->objectName();
    31. if ( action->objectName() == "action_Show_Main" ) {
    32. stack->setCurrentIndex( 0 );
    33. } else if ( action->objectName() == "action_Show_Photos" ) {
    34. stack->setCurrentIndex( 1 );
    35. } else if ( action->objectName() == "action_Show_Movies" ) {
    36. stack->setCurrentIndex( 2 );
    37. } else if ( action->objectName() == "action_Show_Games" ) {
    38. stack->setCurrentIndex( 3 );
    39. } else if ( action->objectName() == "action_Show_Tools" ) {
    40. stack->setCurrentIndex( 4 );
    41. } else {
    42. stack->setCurrentIndex( 0 );
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 
    Why that? it's set up exactly like the central Widget.
    Last edited by C167; 7th January 2008 at 21:11. Reason: reformatted to look better

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStackedLayout doesn't show pages

    Why don't you use QStackedWidget?
    and why not just :
    Qt Code:
    1. area0->setPixmap( QPixmap( ":images/splash0.jpg" ) );
    To copy to clipboard, switch view to plain text mode 
    ?

    Oh, and try adding show() to your labels, maybe thats why they don't show up.
    And are you sure mainPage::showContent() is being called?
    Last edited by high_flyer; 7th January 2008 at 21:56.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStackedLayout doesn't show pages

    Quote Originally Posted by high_flyer View Post
    Why don't you use QStackedWidget?
    because i already inherit vom QWidget, so an additional widget would be useless.

    Quote Originally Posted by high_flyer View Post
    and why not just :
    Qt Code:
    1. area0->setPixmap( QPixmap( ":images/splash0.jpg" ) );
    To copy to clipboard, switch view to plain text mode 
    ?
    good question, i saw it in some examples, so i just copied it. thx

    Quote Originally Posted by high_flyer View Post
    Oh, and try adding show() to your labels, maybe thats why they don't show up.
    Nope, i added a show() to every label, but still everything blank...

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStackedLayout doesn't show pages

    because i already inherit vom QWidget, so an additional widget would be useless.
    ???
    What I mean is:
    Qt Code:
    1. mainPage::mainPage( QWidget *parent ): QWidget( parent ) {
    2. stack = new QStackedWidget( this ); //<<where are you deriving here?
    3. //setLayout( stack );
    4. ....
    To copy to clipboard, switch view to plain text mode 

    Maybe if you explain what it is you are trying to do we can help you use the ready made Qt widgets, most likely what you want is already done by Qt, no need to reinvent the wheel.
    And are you sure mainPage::showContent() is being called?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStackedLayout doesn't show pages

    What does QPixmap( ":images/splash0.jpg" ).isNull() return?

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStackedLayout doesn't show pages

    jacek - he said that with a QLable that is not in the layout it does show up, so I guess the image is not null... but a check will not harm
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Lightbulb Re: QStackedLayout doesn't show pages

    Quote Originally Posted by high_flyer View Post
    ???
    What I mean is:
    Qt Code:
    1. mainPage::mainPage( QWidget *parent ): QWidget( parent ) {
    2. stack = new QStackedWidget( this ); //<<where are you deriving here?
    3. //setLayout( stack );....
    To copy to clipboard, switch view to plain text mode 
    Ehm... Deriving from the mainPage-class? I'm a bit confused now... And i see that i forgot to post the class definition...
    Quote Originally Posted by high_flyer View Post
    Maybe if you explain what it is you are trying to do we can help you use the ready made Qt widgets, most likely what you want is already done by Qt, no need to reinvent the wheel.
    Sure, I have a QMainWindow, and the centralWidget is a QStackedWidget. Then, I have 5 classes that are responsible for displaying a specific content, one of them is the mainPage. They have the complete centralWidget-space for displaying their content. So, the mainPage shows 5 Splashes. The 5 buttons in the toolbar get connected to the slot mainPage::showContent() [yea, the forgotten class definition ;(] gets connected to the QActionGroup::hovered trigger. The right spash image should be displayed then.

    And: It works. I really don't know why. I made a "make distclean" and now it works. I can't remember that i changed any line in the code... It seems that i have to do distclean from time to time!
    Oh, and here's the class definition:
    Qt Code:
    1. class mainPage : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. mainPage( QWidget *parent = 0 );
    6. void loadSplashes();
    7. ~mainPage();
    8. private:
    9. QLabel *area0;
    10. QLabel *area1;
    11. QLabel *area2;
    12. QLabel *area3;
    13. QLabel *area4;
    14. public slots:
    15. void showContent( QAction* );
    16. };
    To copy to clipboard, switch view to plain text mode 
    The first version was with only one QLabel that got it's pixmap in the slot. You can imagine that this was quite slow. When moving the mouse over the toolbar, you could see how the pixmaps got painted.

    Back to the QStackedWidget: Why do you think i should use one here? I used one in the QMainWindow, because it has an manageing Widget so i don't have to create one on my own. But now, I already have a QWidget, so i would have to create a Layout, do setLayout() and create the QStackedWidget _in_ it. If i understand everything right, that's more complicated than it should be for such an easy thing...

    Thanks very much
    C167
    Attached Images Attached Images

  8. #8
    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: QStackedLayout doesn't show pages

    For the same reason people use QListWidget instead of QListView - because it's more convenient to use in simple situations. Take your case - it took you almost a day to solve something which would just work if you used the widget instead of the layout.

  9. #9
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStackedLayout doesn't show pages

    i don't think that using a QStackedWidget would have solved the problem, because i soved it by make distclean. Using a QStackedLayout would mean to create a Layout for the class, then create a QStackedWidget and add it to the layout.

  10. #10
    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: QStackedLayout doesn't show pages

    Quote Originally Posted by C167 View Post
    i don't think that using a QStackedWidget would have solved the problem, because i soved it by make distclean.
    But you've been chasing it for a long time suspecting the stacked layout to be the problem.

  11. #11
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStackedLayout doesn't show pages

    yes, because the labels did show up outside the layout. but if i had used the widget, i would have to search for the problem in some more positions

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.