PDA

View Full Version : Best way to swap images within main U?



cmp1104
14th October 2013, 15:34
I am about to develop an application that will let the user navigate through multiple screens. While each screen has its own set of options for the user, they all share a few common elements (a "Back" button, a "Cancel" button, etc.). I think I should be able to make one "base" UI with these common elements scattered around the edges and then just swap out the central part of the UI to give the user the new options. This way all of the common elements stay in the same position no matter where the user in in their navigation.

My question is this: What is the best approach for doing this? I have done some investigating and looked at things like QGraphicsView and how to use multiple UI files for one application but it is not clear to me what the best approach is. It seems like this should be fairly straightforward but I would like to avoid going down blind alleys.

I am using the Qt 5 plug-in with MSVC on a Windows machine.

Thanks.

Santosh Reddy
14th October 2013, 16:03
- Design a main ui in the Qt Designer, add the navigation buttons and QStackedWidget in the center.
- Connect the signal of push buttons to change the active widget (page) in the QStackedWidget.
- You can create widget using Qt Designer and add them to the QStackedWidget individually.

cmp1104
15th October 2013, 15:27
Thank Santosh, this looks like exactly what I need.

I tried to do a basic instance of this and got it to partially work. I can get a widget that exists within my main UI to be displayed in the stacked widget but I can not get a another UI I created as a widget to be displayed. Here is a sample of my test code:

void MainWindow::ChangeWidgets(void)
{
QTime myTime; //Timer used to change images


this->ui->testText->setText("Hello World"); //Setting some text just to show something happening

cMyChild myChild(this->ui->myStackedWidget); //Instance of the class that represents the widget UI.

this->ui->myStackedWidget->addWidget(&myChild); //Adding ui to the stacked widget
this->ui->myStackedWidget->addWidget(this->ui->aWidget); //Adding a widget that already exists on the main UI
this->ui->myStackedWidget->setCurrentIndex(0); //Setting the index of the stacked widget to the UI widget
this->ui->myStackedWidget->show(); //Make sure that the stacked widget is being displayed

myTime.start();

while(myTime.elapsed() < 3000) //Wait 3 seconds
{
;;
}
this->ui->myStackedWidget->setCurrentIndex(1); //Change the displayed widget to the one that exists on the main UI

}

When the index of the stacked widget is changed to 1 the widget that exists on the main UI gets displayed in the stacked widget. I am not sure why the myChild widget is not displayed. The constructor for myChild is this:

cMyChild::cMyChild(QWidget *parent) : QWidget(parent), ui(new UI::cMyChild)
{
this->ui->setupUi(parent);
this->show;
}

I am sure I am missing something obvious but I am not sure what.

Thanks.

Added after 30 minutes:

Okay, I think I figured out what I was doing wrong. Two things:
1. The index for the stacked widget is not zero basec.
2. I changed how I was adding my UI widget to this->ui->myStackedWidget->addWidget(new cMyChild());

It now appears to work.