1

I've just designed my ui in the QT-Creator and, as the main application is based on two panels, I decided to use the StackedWidget for "change the layout" without opening a new window.

So, I added a QTStackedWidget named: stackedWidget (as default).

The problem is in mainwindow.cpp, I added a custom SLOT that contain:

ui->stackedWidget->setCurrentIndex(1);

when I build this the compiler says:

mainwindow.cpp:25: error: no member named 'stackedWidget' in 'Ui::MainWindow'
ui->stackedWidget->setCurrentIndex(1);
~~ ^

also in the qt-creator itself I was unable to attach a signal to the stackedWidget because it doesn't show to me the setCurrentIndex SLOT...

any advice?

Please note that I'm a noob with C++ I just used Qt a couple of years ago with PyQt4.

mainwindow.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;
}
Marek R
  • 32,568
  • 6
  • 55
  • 140
MiPnamic
  • 1,257
  • 10
  • 18

4 Answers4

9

I had a very similar issue. One of the UI actions was defined as the others and used. I had a ui has no member named xyz compilation error for this one only, without possible explanation.

I could solve it by unchecking/checking the Shadow build option in the project compilation options!

Hope it saves someone the 30 minutes I just lost :)

leplatrem
  • 1,005
  • 13
  • 25
1

Faced the very same issue today. Recreating the project does work, though I've found a solution that won't make one rewrite everything :) Run it in Debug. Once you've done it, problem's solved.

Philipp
  • 183
  • 1
  • 9
0

Creating a new project fixed it... I don't know why, I'm still looking for an explanation.

I started a new project, created the stackedWidget, and tested the code for the page-switching... it just simply works... and I still do not know WHY the other one fail to build...

Checked again and again names and everything.

MiPnamic
  • 1,257
  • 10
  • 18
0

I tried cleaning project and running qmake without any luck. I then drilled down into the project folder and deleted the ui_***.h file and everything started working. Might have had to do with the fact Visual Studio generated that file and I was working on another computer with Qt Creator for the same project.

riverofwind
  • 525
  • 4
  • 17