User interface QMainWindow setCentralWidget和stackedWidget出现问题

User interface QMainWindow setCentralWidget和stackedWidget出现问题,user-interface,qt,User Interface,Qt,我有我在designer中创建的gui,很简单。 QMainWIndow包含stackedWidget,应用程序以 stackedWidget索引0包含qwebkit小部件,在一些用户流之后,它将更改为stackedWidget 包含QTree小部件的索引1,用于将我在QMainWindow中使用的第一个小部件置于这行代码的中心 此->setCentralWidget(ui.webView);但是当应用程序切换到索引号1时,会出现异常 来自切换命令。 为什么? 听起来QMainWindow中心小

我有我在designer中创建的gui,很简单。
QMainWIndow包含stackedWidget,应用程序以
stackedWidget索引0包含qwebkit小部件,在一些用户流之后,它将更改为stackedWidget
包含QTree小部件的索引1,用于将我在QMainWindow中使用的第一个小部件置于这行代码的中心
此->setCentralWidget(ui.webView);但是当应用程序切换到索引号1时,会出现异常
来自切换命令。

为什么?

听起来QMainWindow中心小部件应该是堆叠小部件而不是webView。

听起来QMainWindow中心小部件应该是堆叠小部件而不是webView。

堆叠小部件是包含其他小部件的容器小部件。在您的设置中,您似乎使用了两个堆叠的小部件:QWebKit小部件和QTreeWidget

要使它们显示在
QMainWindow
中,您必须将
QMainWindow
的中心小部件设置为堆叠小部件,并使用
QStackedWidget::changeCurrentIndex()
槽从第一个小部件传递到另一个小部件

下面是一个使用
QPushButton
QLabel
作为堆叠小部件元素的代码示例

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);
要将当前显示的项目从按钮更改为标签,可以使用:

stackedWidget->setCurrentIndex(1); // go to the label widget
stackedWidget->setCurrentIndex(0); // go back to the button widget
mainWindow->setCentralWidget(widgets[1]);

对评论的其他回应。您确定要使用堆叠小部件吗?实际上,堆叠小部件专门用于创建小部件的类似于选项卡的表示。如果要从一个小部件切换到另一个小部件,可以直接使用QMainWindow::setCentralWidget()方法,如以下代码所示

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]);

堆叠小部件是包含其他小部件的容器小部件。在您的设置中,您似乎使用了两个堆叠的小部件:QWebKit小部件和QTreeWidget

要使它们显示在
QMainWindow
中,您必须将
QMainWindow
的中心小部件设置为堆叠小部件,并使用
QStackedWidget::changeCurrentIndex()
槽从第一个小部件传递到另一个小部件

下面是一个使用
QPushButton
QLabel
作为堆叠小部件元素的代码示例

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);
要将当前显示的项目从按钮更改为标签,可以使用:

stackedWidget->setCurrentIndex(1); // go to the label widget
stackedWidget->setCurrentIndex(0); // go back to the button widget
mainWindow->setCentralWidget(widgets[1]);

对评论的其他回应。您确定要使用堆叠小部件吗?实际上,堆叠小部件专门用于创建小部件的类似于选项卡的表示。如果要从一个小部件切换到另一个小部件,可以直接使用QMainWindow::setCentralWidget()方法,如以下代码所示

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]);

如果您试图将web视图居中对齐,
setCentralWidget
不是您想象的那样。中心小部件是主窗口中的主小部件。术语central表示它是您将内容放入其中的主要小部件,而工具栏、停靠小部件和状态栏类似于“附件小部件”


要对齐或定位小部件,请查看主窗口或中心小部件并为其添加适当的布局。

如果您试图将web视图居中对齐,
setCentralWidget
不是您所认为的那样。中心小部件是主窗口中的主小部件。术语central表示它是您将内容放入其中的主要小部件,而工具栏、停靠小部件和状态栏类似于“附件小部件”


要对齐或定位小部件,请查看主窗口或中心小部件,并为其添加适当的布局。

这就是我们现在拥有的。我需要我从setCentralWidget获得的效果,它是连接到主窗口的adge上的小部件,这是我想要的外观,但无法通过设计器获得。嗯,我不明白你现在的问题是什么。排列在
QStackedWidget
中的小部件应占据整个可用空间。你能详细说明你的问题吗?不,如果我不使用setCentralWidget,它不会占用所有空间,但是如果我使用setCentralWidget,它会占用所有空间,但是当我切换到第二个堆栈小部件时,我得到了exceptionOk,所以我在回答中提出了一种替代方法,即不使用
QStackedWidget
。这就是现在所拥有的。我需要我从setCentralWidget获得的效果,它是连接到主窗口的adge上的小部件,这是我想要的外观,但无法通过设计器获得。嗯,我不明白你现在的问题是什么。排列在
QStackedWidget
中的小部件应占据整个可用空间。你能详细说明你的问题吗?不,如果我不使用setCentralWidget,它不会占用所有空间,但是如果我使用setCentralWidget,它会占用所有空间,但是当我切换到第二个堆栈小部件时,我得到了exceptionOk,所以我在回答中提出了一种替代方法,它不使用
QStackedWidget