PDA

View Full Version : widgets behind hidden widgets not working



bpetty
5th September 2007, 15:44
Have you all had this problem? I have two layouts that overlap each other and can be toggled visible via radio buttons. The problem I am seeing is that when I setVisible(false) to my widgets on top and setVisible(true) to widgets on bottom, my bottom widgets are rendered useless. For example, I can't check a check box that was made visible but has a hidden widget on top of it.

Has anyone ran into this? Is there something I am missing?

I'll play around with it a little more. If I can get it to work I'll post my solution. Maybe hiding all top widgets first, then making the bottom widgets visible instead of the other way around. Hmmm...

jpn
5th September 2007, 15:51
Try using QStackedWidget.

bpetty
6th September 2007, 16:30
That looks promising except that my widgets are in layouts and I need to stack layouts. I looked at QStackedLayout, which sounds great, but I don't think it accepts layouts either, just widgets.

There has to be some widget/layout method to call to fix this issue without using another class.

marcel
6th September 2007, 16:48
It accepts widgets WITH layouts - meaning that you add all your widgets to container widget that has a layout.

bpetty
6th September 2007, 17:12
Augh, I think I see. I am using Qt Designer to create these layouts and what not. So essentially all I do is instantiate a QStackedWidget object and add my widgets? I take it that I call insertWidget(index) and add all of my widgets from layout X to index 1 for example, and all widgets from layout Y to index 2?

I assume that I can hide my widgets too, by hiding the QStackedWidget object? Or maybe adding a "blank" widget at a certain index and setting the current index it to that index?

This is cool, thanks for the pointers!

marcel
6th September 2007, 17:17
Augh, I think I see. I am using Qt Designer to create these layouts and what not. So essentially all I do is instantiate a QStackedWidget object and add my widgets? I take it that I call insertWidget(index) and add all of my widgets from layout X to index 1 for example, and all widgets from layout Y to index 2?


No, you can insert only one widget at a certain index.
What I meant was that you first create a widget, take layout X and set it to this widget with setLayout. Next add this widget at index 0 and do the same for all your layouts.

Regards

bpetty
6th September 2007, 17:43
wow, I am really lost then. If I have 5 widgets in one layout, and 10 widgets in another layout... and I create 15 indexes... how am I suppose to view my 5 widgets at once, then switch over to the other 10 and vice versa? setCurrentIndex() only takes 1 index at a time. I don't want to show one widget at a time, I want to show groups of widgets at a time that happen be in the same location. I guess I don't see how to do this with QStackedWidget.

vonCZ
6th September 2007, 17:47
like so:


void Window::mkVuBuLayout() // creates a stack of ViewButton layouts
{

vuButtStack = new QStackedWidget(this);
vuButtStack->setFixedHeight(34);

for(int compNUM=0; compNUM<num_of_stacked_layouts; compNUM++)
{

vuButtLayout[compNUM] = new QHBoxLayout;
vuButtLayout[compNUM]->setMargin(0);
vuButtLayout[compNUM]->setAlignment(Qt::AlignLeft);
vuButtLayout[compNUM]->addSpacing(75);

for(int i=0; i<num_buttons_in_this_layout; i++)
{
vuButtons[i] = new QPushButton(tr("%1").arg(dafo.get_vuBuNames(i)));
vuButtons[i]->setFixedSize(60,20);
vuButtLayout[compNUM]->addWidget(vuButtons[i]);
vuButtLayout[compNUM]->addSpacing(10);
}

vuButtLayoutW[compNUM] = new QWidget(vuButtStack);
vuButtLayoutW[compNUM]->setLayout(vuButtLayout[compNUM]);

vuButtStack->addWidget(vuButtLayoutW[compNUM]);

}
}

vonCZ
6th September 2007, 17:51
oops: i posted that before your last post...

if you have 5 buttons in one layout, and 10 in another: you'll only have 2 indices. Basically, you:

1. create Q*Layout: lay1

2. add buttons to lay1

3. create QWidget: lay1W

4. do: lay1W->setLayout(lay1);

5. then do the same with lay2, lay3, etc...

6. then add these lay*W QWidgets to your QStackedWidget

bpetty
6th September 2007, 20:08
Thanks guys.

I have ran into an issue though. I am creating my layouts and what not with Qt Designer. My .ui creates the following layout code:



// taken from an auto generated ui_*.h file.
QWidget *Layout1;
QHBoxLayout *hboxLayout1;
QLabel *lblDocType_grp1;
QComboBox *cmbIndexes_grp1;
...
Layout1 = new QWidget(ConfigWidget);
Layout1->setObjectName(QString::fromUtf8("Layout1"));
Layout1->setGeometry(QRect(30, 150, 303, 31));
hboxLayout1 = new QHBoxLayout(Layout1);
hboxLayout1->setSpacing(6);
hboxLayout1->setMargin(0);
hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1"));
lblDocType_grp1 = new QLabel(Layout1);
lblDocType_grp1->setObjectName(QString::fromUtf8("lblDocType_grp1"));

hboxLayout1->addWidget(lblDocType_grp1);

cmbIndexes_grp1 = new QComboBox(Layout1);
cmbIndexes_grp1->setObjectName(QString::fromUtf8("cmbIndexes_grp1"));
QSizePolicy sizePolicy(static_cast<QSizePolicy::Policy>(7), static_cast<QSizePolicy::Policy>(0));
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(cmbIndexes_grp1->sizePolicy().hasHeightForWidth());
cmbIndexes_grp1->setSizePolicy(sizePolicy);
cmbIndexes_grp1->setEditable(true);

hboxLayout1->addWidget(cmbIndexes_grp1);


Sooo.. Layout1 is my QWidget that I want to insert right?

When I create a stacked object like so:


void CConfigWidget::InitStack()
{
// Add Layout Widgets to QStackerWidget
pStacker = new QStackedWidget;
pStacker->insertWidget(0, ui.Layout1);
pStacker->insertWidget(1, ui.Layout2);
pStacker->insertWidget(2, ui.Layout3);
}


and connect it to my radio buttons like so:


void CConfigWidget::InitConnections()
{
connect(ui.rbRadio1, SIGNAL(toggled(bool)), this, SLOT(Radio1Click(bool)));
connect(ui.rbRadio2, SIGNAL(toggled(bool)), this, SLOT(Radio2Click(bool)));
connect(ui.rbRadio3, SIGNAL(toggled(bool)), this, SLOT(Radio3Click(bool)));
}

void CConfigWidget::Radio1Click(bool bVisible)
{
if (bVisible) pStacker->setCurrentIndex(0);
}


... nothing shows up. setCurrentIndex(int) is getting called... but nothing is displayed.

Am I still missing something?

vonCZ
7th September 2007, 07:34
i don't see something like:

Layout1->setLayout(hboxLayout1);


...in the ui_*.h file. Maybe that's it?


Edit: just saw this in setLayout doc:


An alternative to calling this function is to pass this widget to the layout's constructor.


...so maybe not. (since you did this) But might try it, anyway. I'm not sure what the difference is between:



layout = new QHBoxLayout(widget);
//and
layout = new QHBoxLayout;
widget->setLayout(layout);

bpetty
7th September 2007, 19:57
What is also odd is that when I setVisible(false) to my top widgets and setVisible(true) to my bottom widgets and then call ->raise() to bring them to the front... that doesn't work either. It is odd. Am I the only one that has had this problem... or just the only one that couldn't get QStackedWidget to work. :(

bpetty
7th September 2007, 20:16
I got this working. I used QStackedLayout instead. That works great. Not sure why raise() didn't do the trick, but this is a cleaner in my case. Thanks guys. Sorry I was a little slow.

jpn
7th September 2007, 20:23
Hmm, you shouldn't need to hide() or raise() anything. It's as simple as this:


class Window : public QWidget
{
public:
Window(QWidget* parent = 0) : QWidget(parent)
{
QComboBox* comboBox = new QComboBox(this);
for (int i = 1; i <= 2; ++i)
comboBox->addItem(QString::number(i));

QStackedWidget* stackedWidget = new QStackedWidget(this);
stackedWidget->addWidget(createPage1(stackedWidget));
stackedWidget->addWidget(createPage2(stackedWidget));

connect(comboBox, SIGNAL(currentIndexChanged(int)), stackedWidget, SLOT(setCurrentIndex(int)));

QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(comboBox);
layout->addWidget(stackedWidget);
}

private:
QWidget* createPage1(QWidget* parent = 0)
{
...
}

...
};