【发布时间】:2014-07-03 09:16:05
【问题描述】:
我刚刚在 QT-Creator 中设计了我的 ui,由于主应用程序基于两个面板,我决定使用 StackedWidget 来“更改布局”而不打开新窗口。
所以,我添加了一个名为:stackedWidget(默认)的 QTStackedWidget。
问题出在 mainwindow.cpp 中,我添加了一个自定义 SLOT,其中包含:
ui->stackedWidget->setCurrentIndex(1);
当我构建它时,编译器会说:
mainwindow.cpp:25:错误:“Ui::MainWindow”中没有名为“stackedWidget”的成员
ui->stackedWidget->setCurrentIndex(1);
~~ ^
同样在 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
mainiwindow.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;
}
【问题讨论】:
-
你能发布更多关于类是如何定义的代码吗?
-
你是对的。请记住,我只使用 Qt-Creator,这是 70% 的自动代码。
-
检查你的 ui 文件(或使用设计器)如果你有 stackWidget。可能你改名了,没注意。
-
第 65 行:
第 66 行: -
尝试:清理,运行 qmake,构建。
标签: c++ qt qt-creator qt5.2