PDA

View Full Version : QStackedLayout doesn't show pages



C167
7th January 2008, 22:09
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:
mainPage::mainPage( QWidget *parent ): QWidget( parent ) {
stack = new QStackedLayout( this );
setLayout( stack );
area0 = new QLabel( this );
area1 = new QLabel( this );
area2 = new QLabel( this );
area3 = new QLabel( this );
area4 = new QLabel( this );

stack->addWidget( area0 );
stack->addWidget( area1 );
stack->addWidget( area2 );
stack->addWidget( area3 );
stack->addWidget( area4 );

stack->setCurrentIndex( 0 );
// QLabel *test = new QLabel(this);
// test->setPixmap(QPixmap::fromImage(QImage(":/images/splash0.jpg")));
}

void mainPage::loadSplashes() {
area0->setPixmap( QPixmap::fromImage( QImage( ":images/splash0.jpg" ) ) );
area1->setPixmap( QPixmap::fromImage( QImage( ":images/splash1.jpg" ) ) );
area2->setPixmap( QPixmap::fromImage( QImage( ":images/splash2.jpg" ) ) );
area3->setPixmap( QPixmap::fromImage( QImage( ":images/splash3.jpg" ) ) );
area4->setPixmap( QPixmap::fromImage( QImage( ":images/splash4.jpg" ) ) );
}

void mainPage::showContent( QAction* action ) {
qDebug() << "showContent called: " << action->objectName();
if ( action->objectName() == "action_Show_Main" ) {
stack->setCurrentIndex( 0 );
} else if ( action->objectName() == "action_Show_Photos" ) {
stack->setCurrentIndex( 1 );
} else if ( action->objectName() == "action_Show_Movies" ) {
stack->setCurrentIndex( 2 );
} else if ( action->objectName() == "action_Show_Games" ) {
stack->setCurrentIndex( 3 );
} else if ( action->objectName() == "action_Show_Tools" ) {
stack->setCurrentIndex( 4 );
} else {
stack->setCurrentIndex( 0 );
}
}Why that? it's set up exactly like the central Widget.

high_flyer
7th January 2008, 22:51
Why don't you use QStackedWidget?
and why not just :

area0->setPixmap( QPixmap( ":images/splash0.jpg" ) );
?

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?

C167
7th January 2008, 22:59
Why don't you use QStackedWidget?because i already inherit vom QWidget, so an additional widget would be useless.


and why not just :

area0->setPixmap( QPixmap( ":images/splash0.jpg" ) );
?good question, i saw it in some examples, so i just copied it. thx :)


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

high_flyer
8th January 2008, 00:48
because i already inherit vom QWidget, so an additional widget would be useless.

???
What I mean is:


mainPage::mainPage( QWidget *parent ): QWidget( parent ) {
stack = new QStackedWidget( this ); //<<where are you deriving here?
//setLayout( stack );
....

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?

jacek
8th January 2008, 01:04
What does QPixmap( ":images/splash0.jpg" ).isNull() return?

high_flyer
8th January 2008, 13:03
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 :)

C167
8th January 2008, 13:46
???
What I mean is:
mainPage::mainPage( QWidget *parent ): QWidget( parent ) {
stack = new QStackedWidget( this ); //<<where are you deriving here?
//setLayout( stack );....Ehm... Deriving from the mainPage-class? I'm a bit confused now... And i see that i forgot to post the class definition...

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:
class mainPage : public QWidget
{
Q_OBJECT
public:
mainPage( QWidget *parent = 0 );
void loadSplashes();
~mainPage();
private:
QStackedLayout *stack;
QLabel *area0;
QLabel *area1;
QLabel *area2;
QLabel *area3;
QLabel *area4;
public slots:
void showContent( QAction* );
};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

wysota
8th January 2008, 15:49
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.

C167
22nd January 2008, 15:45
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.

wysota
22nd January 2008, 16:01
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.

C167
25th January 2008, 14:34
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