PDA

View Full Version : Silly question on QStacked Widget



seerofsorrow
10th October 2017, 23:28
So I have the following code:



void Classy::on_checkBox_pressed()
{
qDebug() << "Box Pressed";
layout->addWidget(stackedWidget);
stackedWidget->setCurrentWidget(sickWidget);
stackedWidget->setLayout(layout);
stackedWidget->show();
}


Layout is a QVBoxLayout that I defined in public under classy.
stackedWidget is a stackedWidget that I have as as stacked Widget in my ui.
sickWidget is a "page" (QWidget) that has more objects underneath it.

i looked at all kinds of documentation for this including this thread: http://www.qtcentre.org/threads/68786-Switching-between-two-ui-forms-QMainWindow-screens?highlight=qstackedwidget

(I have w.show(); inside of main.cpp) However whenever I select the checkbox in my program it crashes and I don't understand what I'm doing wrong as I'm trying my best to follow the example in the Qt documentation under QStackedWidget. Can anyone help me?

Thank you again to everyone.

Santosh Reddy
11th October 2017, 06:14
void Classy::on_checkBox_pressed()
{
qDebug() << "Box Pressed";
layout->addWidget(stackedWidget);
stackedWidget->setCurrentWidget(sickWidget);
//stackedWidget->setLayout(layout); // This should not be done, layout of stackedWidget is managed internally by QStackedWidget.
//stackedWidget->show(); // Need not show this directly, just show the top level widget which contains the stackedWidget.
}

seerofsorrow
11th October 2017, 19:49
I attempted to delete those lines and I also intizalized the variables i mentioned before as well however now its stating that stackedWidget and nfsWidget are uninitialized.
I am attempting to find a way to initialize them so that they can be seen as Widgets that I have built inside of the ui. I've tried different enumerations of this and have gotten no where.
Obviously when attempt to run this it crashes due to lack of intilization. Thoughts?




void Classy::on_checkBox_pressed()
{
QVBoxLayout *layout;
QStackedWidget *stackedWidget;
QWidget *nfsWidget;
layout.addWidget(stackedWidget);
stackedWidget.setCurrentWidget(nfsWidget);
}

Santosh Reddy
12th October 2017, 06:00
... Thoughts?
You need to work on your C++ skills.

See if this helps you understand your problem
- Make layout, stackedWidget, sickWidget and nfsWidget as member variable of Classy.
- Initialise layout, stackedWidget, sickWidget and nfsWidget in Classy constructor
- layout.addWidget(stackedWidget); // Move this to Classy constructor
- then try this code



void Classy::on_checkBox1_pressed()
{
stackedWidget->setCurrentWidget(sickWidget);
}

void Classy::on_checkBox2_pressed()
{
stackedWidget->setCurrentWidget(nfsWidget);
}