tags:

views:

187

answers:

3

Hello

i have gui the i created in the designer , simple one .
QMainWIndow that contains stackedWidget , the application starts with
stackedWidget index 0 that contains qwebkit widget and after some user flow ,it changes to stackedWidget
index 1 that contains QTree widget , to center the first widget i use in the QMainWindow constractor this line of code
this->setCentralWidget(ui.webView); but when the application switching to index number 1 im getting exception that is
coming from the switching command .
why ?

A: 

It sounds like the QMainWindow central widget should be the stacked widget and not the webView.

shoosh
+3  A: 

A stacked widget is a container widget that contains other widget. In your settings, you seems to use two stacked widget : a QWebKit widget and a QTreeWidget.

To have them displayed in the QMainWindow, you have to set the central widget of the QMainWindow to be the stacked widget and uses QStackedWidget::changeCurrentIndex() slot to pass from the first widget to the other.

Here is a code sample using a QPushButton and a QLabel as elements of the stacked widget.

QMainWindow *mainWindow = new QMainWindow();
QStackedWidget *stackedWidget = new QStackedWidget();

// stacked item 0
QPushButton *pushButton = new QPushButton("Here is a button");
stackedWidget->addItem(pushButton);

// stacked item 1
QLabel *label = new QLabel("Here is a label");
stackedWidget->addItem(label);

// add the stacked widget to the main window
mainWindow->setCentralWidget(stackedWidget);

To change the current displayed item from the button to the label, you can use:

stackedWidget->setCurrentIndex(1); // go to the label widget
stackedWidget->setCurrentIndex(0); // go back to the button widget

Alternative response to the comments. Are you sure you want to use a stacked widget? Actually, stacking widget are specialized to create sort of tab like presentation of widget. If you want to switch from one widget to the other you can use directly the QMainWindow::setCentralWidget() method as seen in the following code.

QMainWindow *mainWindow = new QMainWindow();
QVector< QWidget* > widgets;

// stacked item 0
QPushButton *pushButton = new QPushButton("Here is a button");

// stacked item 1
QLabel *label = new QLabel("Here is a label");

widgets << pushButton << label;

// add the first widget to the main window
mainWindow->setCentralWidget(widgets[0]);

When you want to switch to the other widget, you can use:

mainWindow->setCentralWidget(widgets[1]);
Lohrun
this is what is have now . i need the effect im getting from the setCentralWidget that is widget that is attached to the adge of the main window ,this is the look im looking for and can't get it via the designer .
Well, I don't understand what is your issue now. The widget arranged in the `QStackedWidget` should take the whole space available. Can you detail your problem?
Lohrun
well no it dosn't the all space if i don't use setCentralWidgetbut if i use setCentralWidget it dos ,but when i switch to the second Stack Widget i got exception
Ok, so I proposed an alternative method in my response that doesn't use a `QStackedWidget`.
Lohrun
+1  A: 

If you are trying to center align the web view, setCentralWidget is not what you think it is. A central widget is the main widget in a main window. The term central is meant to indicate it's the primary widget where you put your contents in, while tool bars, dock widgets, and status bar are like "accessory widgets."

To align, or otherwise position, a widget, take a look at QLayout and add a proper layout to your main window or the central widget.

Stephen Chu