В моем конструкторе 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);
Неплохой обходной путь, но все же мне интересно, почему другой вариант не сработает.
Мой полный тестовый код работает нормально для меня. Вы можете сравнить с вашим собственным.
Использование 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>