QStackedWidgets and other Layouts does not display properly
following is my problem statement:
create a horizantal layout in display
add stackedwidget to this Hlayout
the stackedwidget contains imagesWidget and loadWidget
the imagesWidget is further divided into gridlayout which contains some icons
so on the stackedwidget i have a plain widget and a widget withe gridlayout
now , when i click on any icon of the gridlayout it should open loadwidget and on to that
my application should be visible(here i am just playing with colors instead of applications)
for this purpose i again divide grid layout into Horizantal layout and adding the apps widget.
i face two problems here:
1) my gridlayout icons on imagesWidget are coming properly but when i select them i can see
under those icons same icons are present( i dont understand how i got duplicate)
2) when i close my application i am following the steps mentioned in exitapp code:
but my background of the loadWidget is still retianed but with icons from imageWidget
unfortunately i cannot paste the connect calls because the loading is not handled by connect
but some external events from some other module.
below is list of steps i am following.
------------------------------------------
Code:
display->setLayout(controlsLayout);
display->setLayout(controlsLayout);
imagesWidget->setLayout(imagesLayout);// setting a grid layout for imagesWidget
imgstack
= new QStackedWidget;
// new stacked widget for imagesWidget and loadwidget imgstack->addWidget(imagesWidget);
imgstack->addWidget(loadWidget);
qDebug("imgstack cnt = %d",imgstack->count());
controlsLayout->addWidget(imgstack); // stacked widget to horizantal layout of of display
const char *colors[]={"background-color: white",
"background-color: black",
"background-color: blue",
"background-color: green",
"background-color: yellow",
"background-color: red",
"background-color: pink",
"background-color: cyan",
"background-color: white"
}; // unique colors are given for each widget to identify
for(int apno = 0;apno < 9;apno ++){
appwidgs[apno]->setStyleSheet(colors[apno]);
appstack->addWidget(appwidgs[apno]);
}
--------------------------
upon click on any of the image the following will be performed.
Code:
imgstack->setCurrentIndex(1);
//loadWidget->setStyleSheet("background-color: blue");
qDebug("currindx=%d\n",imgstack->currentIndex());
loadapp();
--------------------------
loadapp code:
Code:
loadWidget->setLayout(hl);
hl->addWidget(appstack,0,0);
qDebug("curr_apps =%d",appstack->currentIndex());
appstack->setCurrentIndex(currpos);
---------------------------------------
exitapp code:
Code:
appstack->removeWidget(appwidgs[currpos]);
imgstack->removeWidget(loadWidget);
imgstack->setCurrentWidget(imagesWidget);
Re: QStackedWidgets and other Layouts does not display properly
If you have a navigation panel of 9 buttons, each of which should switch to a different 'application' then surely you want the single stacked widget to have 10 panels: the navigation panel and each of the 9 applications.
Here's an example:
Code:
#include <QtGui>
{
Q_OBJECT
public:
{
resize(512, 512);
setCentralWidget(stack);
// Plumbing
connect(mapper, SIGNAL(mapped(int)), stack, SLOT(setCurrentIndex(int)));
// Nav panel
stack->addWidget(panel); // index 0
panel->setLayout(grid);
for (int r = 0; r < 3; ++r) {
for (int c = 0; c < 3; ++c) {
// Create the navigation button
const int index = r * 3 + c + 1;
grid->addWidget(navButton, r, c, Qt::AlignCenter );
// and the matching 'application'
stack->addWidget(button);
connect(button, SIGNAL(clicked()), SLOT(goHome())); // needs some way to go back
// make the nav button switch
mapper->setMapping(navButton, index);
connect(navButton, SIGNAL(clicked()), mapper, SLOT(map()));
}
}
}
public slots:
void goHome()
{
stack->setCurrentIndex(0);
}
};
int main(int argc, char **argv)
{
MainWindow w;
w.show();
return app.exec();
}
#include "main.moc"
Re: QStackedWidgets and other Layouts does not display properly
Thanks ChrisW,
i have done almost the same way , but my problem is , on my 3X3 layout window i am getting duplicate buttons by overlapping on existing.
and one more problem is when i use removeWidget :
suppose say i have displayed an application, now i want to go back to my image window where i have list of icons for naviagtion.
i am following the statements below.
appstack->removeWidget(appwidgs[currpos]); // to remove the crrent application upon exit
imgstack->removeWidget(loadWidget); // to remove widget used for application display
imgstack->setCurrentWidget(imagesWidget);// set images widget as current display
i am getting my images widget but , other two widgets are not geting removed.
i understand remove widget means , just hiding the widget to make path for other widgets.
Re: QStackedWidgets and other Layouts does not display properly
Read my example again: it's your entire program in a nutshell. My point is that you do not need to do anything other than to switch pages in the single QStackedWidget. No adding or removing widgets after the initial construction, no multiple stacked widgets, nothing special to do during destruction.
Re: QStackedWidgets and other Layouts does not display properly
I now follow the same way as you suggested, but still i am getting the same problem.
i really need to solve this.
please have a look.
Code:
display->setLayout(controlsLayout);
imgstack->addWidget(imagesWidget);
no_of_icons =9;
RowCount=no_of_icons/COL +
(temp =(no_of_icons%COL?1:0));
temp=0;
for( i=0;i<RowCount;i++)
{
for(j=0;j<3;j++)
{
QPixmap pixmap
(":/images/img.jpeg");
QIcon ButtonIcon
(pixmap
);
Buttons[temp]->setIcon(ButtonIcon);
Buttons[temp]->setIconSize(pixmap.rect().size());
Buttons[temp]->setMinimumWidth(80);
Buttons[temp]->setMinimumHeight(80);
Buttons[temp]->setMaximumWidth(80);
Buttons[temp]->setMaximumHeight(80);
label->setAlignment(Qt::AlignVCenter|Qt::AlignVCenter);
label->show();
imagesLayout->addWidget(Buttons[temp],i,j);
temp++;
if(imagesLayout->count() ==no_of_icons)
break;
}
}
currpos=0;
if(no_of_icons>0)
{
QPixmap pixmap
(":/images/image.jpeg");
QIcon ButtonIcon
(pixmap
);
Buttons[currpos]->setIcon(ButtonIcon);
Buttons[currpos]->setIconSize(pixmap.rect().size());
Buttons[currpos]->setMinimumWidth(68+10);
Buttons[currpos]->setMinimumHeight(60+10);
Buttons[currpos]->setMaximumWidth(68+10);
Buttons[currpos]->setMaximumHeight(68+10);
}
imagesWidget->setLayout(imagesLayout);
qDebug("imgstack cnt = %d",imgstack->count());
const char *colors[]={"background-color: cyan",
"background-color: grey",
"background-color: blue",
"background-color: green",
"background-color: yellow",
"background-color: red",
"background-color: pink",
"background-color: white",
"background-color: orange"
};
for(int apno = 0;apno < 9;apno ++){
appwidgs[apno]->setStyleSheet(colors[apno]);
imgstack->addWidget(appwidgs[apno]);
}
controlsLayout->addWidget(imgstack);
qDebug("imgstack_count = %d",imgstack->count());
-----------------------
currpos = 0 ; // will be incremented based on no of left and right arrows
in loadapp()
Code:
imgstack->setCurrentIndex(currpos+1); //loads the widget lets say i select index 3(widget:blue)
printf("load:curridx =%d",imgstack->currentIndex());
-------------------------
in exitapp() // will be invoked when i press exit button
Code:
imgstack->setCurrentIndex(0); // widget at index 0 is loaded,but still the blue color can be seen.
printf("exit:curridx =%d",imgstack->currentIndex());
-------------
out put: (when i load the 0th app that is placed at index 1.)
imgstack cnt = 1
imgstack_count = 10
load :currindx=1
exit:curridx =0
Re: QStackedWidgets and other Layouts does not display properly
Your code fragment, while full of cruft, is not fundamentally broken. However, I can only guess what the rest of you program is doing or even where/when you are calling this code from. I could guess that you call this code several times and lay one layout over another... but it's only a WAG. Produce a simple, complete program that demonstrates the problem and post it.