我刚刚在 QT-Creator 中设计了我的 ui,由于主应用程序基于两个面板,我决定使用 StackedWidget 来“更改布局”而不打开新窗口。
所以,我添加了一个名为:stackedWidget(默认)的 QTStackedWidget。
问题出在 mainwindow.cpp 中,我添加了一个自定义 SLOT,其中包含:
ui->stackedWidget->setCurrentIndex(1);
当我构建它时,编译器说:
mainwindow.cpp:25:错误:'Ui::MainWindow'
ui->stackedWidget->setCurrentIndex(1) 中没有名为 'stackedWidget' 的成员;
~~ ^
同样在 qt-creator 本身中,我无法将信号附加到stackedWidget,因为它没有向我显示setCurrentIndex SLOT ...
有什么建议吗?
请注意,我是 C++ 的菜鸟,几年前我刚刚使用 Qt 和 PyQt4。
主窗口.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void showOtherPage();
void showMainPage();
};
#endif // MAINWINDOW_H
主窗口.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
qDebug() << "MainWindow - Debug Mode ON";
connect(ui->btnOther, SIGNAL(clicked()), SLOT(showOtherPage()));
}
void MainWindow::showOtherPage()
{
qDebug() << "Showing Other Page";
ui->stackedWidget->setCurrentIndex(1);
}
void MainWindow::showMainPage()
{
qDebug() << "Showing Main Page";
ui->stackedWidget->setCurrentIndex(0);
}
MainWindow::~MainWindow()
{
delete ui;
}