user interface - problem with QMainWindow setCentralWidget and stackedWidget -


I have a simple GUI in the designer.
QMainWindow which is StackedWindow, the application
stackedWidget index 0 in which there is a qwebkit widget and after some user flow, it varies in stacked widget - Index 1 which contains the QTree widget, which is the center of the first widget I Use this line in the QMainWindow notation code / this-> setCentralWidget (ui.webView); But when the application is going to index number 1, exception exception is coming from switching command. Why?

A stacked widget is a container widget that contains other widgets. In your settings, you are using two stacked widgets: a QWackKit widget and a curie widget.

To display them in QMainWindow , you have to set the central widget QMainWindow to get a stacked widget and to move from the first code to the other uses the QStackedWidget :: changeCurrentIndex () slot.

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

  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 stacked widget to main window - main- window-> setCentralWidget (stackedwd);  

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

  stackedWidget- & gt; Set endindex (1); // Go to the Label widget stackedWidget-> SetCurrentIndex (0); // Back to the button widget  

Optional answers to comments. Are you sure you want to use a stacked widget? Basically, stacking widgets are special for creating special types of tabs, such as presentation of widgets. If you want to switch from one widget to another, you can use the QMainWindow :: setCentralWidget () method seen directly in the following code.

  QMainWindow * mainWindow = new QMainWindow (); QVector & LT; QWidget * & gt; Widgets; // Stacked item 0 QPushButton * pushButton = new QPushButton ("Here is a button"); // Stacked Item 1 QLabel * label = new QLabel ("Here is a label"); Widgets & lt; & Lt; Pushbutton & lt; & Lt; the label; // Add the first widget to the main window Main- Window-> SetCentralWidget (widgets [0]);  

When you want to switch to another widget, you can use it:

  mainWindow-> setCentralWidget (widgets [1]);  

Comments