【问题标题】:Qt Creator no member named stackedWidgetQt Creator没有名为stackedWidget的成员
【发布时间】: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


【解决方案1】:

我有一个非常相似的问题。 UI 操作之一被定义为其他操作并使用。 我只有一个 ui has no member named xyz 编译错误,没有可能的解释。

我可以通过取消选中/选中项目编译选项中的Shadow build 选项来解决它!

希望它能节省我刚刚失去的 30 分钟 :)

【讨论】:

    【解决方案2】:

    今天遇到了同样的问题。重新创建项目确实有效,尽管我找到了一个不会让一个人重写所有内容的解决方案:) 在调试中运行它。完成后,问题就解决了。

    【讨论】:

    • 您提供的内容属于“建议”类别,应作为问题下方的评论!本节用于提供基于真实代码或基于概念描述的答案!我们知道这是您的第一次,但以后请务必遵守!
    【解决方案3】:

    创建一个新项目修复它...我不知道为什么,我仍在寻找解释。

    我开始了一个新项目,创建了stackedWidget,并测试了页面切换的代码......它只是工作......我仍然不知道为什么另一个无法构建......

    一遍又一遍地检查名字和一切。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 2014-11-09
      相关资源
      最近更新 更多