Qt - QStackedWidget 使用
Posted
技术标签:
【中文标题】Qt - QStackedWidget 使用【英文标题】:Qt - QStackedWidget usage 【发布时间】:2014-08-09 17:27:15 【问题描述】:在我的 mainwidow.cpp 构造函数中,我这样做:
TLDAlgoSettings = new TLDSettings(ui->algoSettingsStackedWidget);
ui->algoSettingsStackedWidget->addWidget(TLDAlgoSettings);
ui->algoSettingsStackedWidget->setCurrentWidget(TLDAlgoSettings);
TLDSettings 是一个 QWidget:
#ifndef TLDSETTINGS_H
#define TLDSETTINGS_H
#include <QWidget>
namespace Ui
class TLDSettings;
class TLDSettings : public QWidget
Q_OBJECT
public:
explicit TLDSettings(QWidget *parent = 0);
~TLDSettings();
private:
Ui::TLDSettings *ui;
;
#endif // TLDSETTINGS_H
很简单;但是,看起来我缺少一些东西,因为它不起作用:当我这样做时,我的 TLDSettings QWidget 没有出现在它的位置ui->algoSettingsStackedWidget->setCurrentWidget(TLDAlgoSettings);
有什么建议吗?
[编辑]
好的,我可以确认这是可行的(仍在 mainwindow.cpp 构造函数中):
TLDAlgoSettings = new TLDSettings();
StackedWidget = new QStackedWidget;
StackedWidget->addWidget(TLDAlgoSettings);
ui->visionSettingsVerticalLayout->addWidget(StackedWidget);
不是一个糟糕的解决方法,但我仍然想知道为什么其他选项不起作用。
【问题讨论】:
TLDSettings 类是用 ui 形式创建的吗?或者它只是添加到Ui namespace
的类?
使用 ui 表单创建。
经过测试并且对我来说工作正常(Qt4.8.6),可能您必须显示更多代码...TLDAlgoSettings
是指向 TLDSettings 类的指针,对吗?跨度>
【参考方案1】:
我的完整测试代码,对我来说工作正常。你可以和你自己的比较。 使用Qt4.8.6
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "TLDSettings.h"
namespace Ui
class MainWindow;
class MainWindow : public QMainWindow
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
TLDSettings *TLDAlgoSettings;
;
#endif // MAINWINDOW_H
MainWindow.cpp:
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui->setupUi(this);
TLDAlgoSettings = new TLDSettings(ui->algoSettingsStackedWidget);
ui->algoSettingsStackedWidget->addWidget(TLDAlgoSettings);
ui->algoSettingsStackedWidget->setCurrentWidget(TLDAlgoSettings);
MainWindow::~MainWindow()
delete ui;
void MainWindow::on_pushButton_clicked()
ui->algoSettingsStackedWidget->setCurrentIndex(0);
void MainWindow::on_pushButton_2_clicked()
ui->algoSettingsStackedWidget->setCurrentIndex(1);
void MainWindow::on_pushButton_3_clicked()
ui->algoSettingsStackedWidget->setCurrentWidget(TLDAlgoSettings);
MainWindow.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<layout class="QHBoxLayout" name="buttonLayout">
<property name="spacing">
<number>12</number>
</property>
<property name="leftMargin">
<number>5</number>
</property>
<property name="topMargin">
<number>5</number>
</property>
<property name="rightMargin">
<number>5</number>
</property>
<property name="bottomMargin">
<number>5</number>
</property>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Page 1</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>Page 2</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_3">
<property name="text">
<string>Page 3</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QStackedWidget" name="algoSettingsStackedWidget">
<widget class="QWidget" name="page_1">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>I'm 1st page</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2">
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>I'm 2nd page</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</item>
<item row="1" column="0">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
TLDSettings.h:
#ifndef TLDSETTINGS_H
#define TLDSETTINGS_H
#include <QWidget>
namespace Ui
class TLDSettings;
class TLDSettings : public QWidget
Q_OBJECT
public:
explicit TLDSettings(QWidget *parent = 0);
~TLDSettings();
private:
Ui::TLDSettings *ui;
;
#endif // TLDSETTINGS_H
TLDSettings.cpp:
#include "TLDSettings.h"
#include "ui_TLDSettings.h"
TLDSettings::TLDSettings(QWidget *parent) :
QWidget(parent),
ui(new Ui::TLDSettings)
ui->setupUi(this);
TLDSettings::~TLDSettings()
delete ui;
TLDSettings.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TLDSettings</class>
<widget class="QWidget" name="TLDSettings">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>I'm TLD Setting (3rd page)</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
【讨论】:
我试试,非常感谢你的代码!不过,我看不出我的代码有什么不同......无论如何,我会让你知道的。再次感谢。以上是关于Qt - QStackedWidget 使用的主要内容,如果未能解决你的问题,请参考以下文章
使用 Qt Creator 中设计的自定义小部件将页面动态添加到 QStackedWidget