1

I have a QMainWindow Application which also includes an QStackedWidget. The pages of the QstackedWidget are promoted to ui widgets for example heating_widget.ui If I use a button slot on my QMainWindow I can use this to get my application to fullscreen:

void SmartHome::on_fullscreen_on_clicked()
{
  SmartHome::setWindowState(Qt::WindowFullScreen);
}

But how can I do this from a button which is in the heating_widget.cpp file? Using:

void heating_widget::on_fullscreen_on_clicked()
{
   SmartHome::setWindowState(Qt::WindowFullScreen);
}

obviously doesn't work and throws this error at me:

cannot call member function 'void QWidget::setWindowState(Qt::WindowStates)' without object SmartHome::setWindowState(Qt::WindowFullScreen);

I know this has something to do with parent() but I can't get it to work.

Do you have any idea?

My smarthome.h file:

#ifndef SMARTHOME_H
#define SMARTHOME_H
#include <QTime>
#include <QMainWindow>

namespace Ui {
class SmartHome;
}

class SmartHome : public QMainWindow
{
    Q_OBJECT

public:
    explicit SmartHome(QWidget *parent = 0);
    ~SmartHome();

private slots:
    void on_Info_Button_clicked();

    void on_News_Button_clicked();

    void on_Heating_clicked();

    void timerslot();

    void on_Config_clicked();

    void on_About_clicked();

public slots:

    void setFullscreen();

private:
    Ui::SmartHome *ui;
    QTimer* myTimer;
};

#endif // SMARTHOME_H

My heating_widget.h :

#ifndef HEATING_WIDGET_H
#define HEATING_WIDGET_H
#include "smarthome.h"

#include <QWidget>

namespace Ui {
class heating_widget;
class SmartHome;

}

class heating_widget : public QWidget
{
    Q_OBJECT

public:
    explicit heating_widget(QWidget *parent = 0);
    ~heating_widget();

private slots:

    void on_fullscreen_on_clicked();

private:
    Ui::heating_widget *ui;
};

#endif // HEATING_WIDGET_H

and my heating.widget.cpp:

#include "heating_widget.h"
#include "ui_heating_widget.h"
#include "smarthome.h"
#include "iostream"

heating_widget::heating_widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::heating_widget)
{
    ui->setupUi(this);

    QObject::connect(ui->fullscreen_on, SIGNAL(clicked()), this , SLOT(SmartHome::setFullscreen()));
}

heating_widget::~heating_widget()
{
    delete ui;
}

void heating_widget::on_fullscreen_on_clicked()
{
   parentWidget()->setWindowState(Qt::WindowFullScreen);
    std::cout<<"clicked"<<std::endl;
}
CADman
  • 11
  • 3
  • Remove `SmartHome::` from `SmartHome::setWindowState` – drescherjm Feb 03 '18 at 13:51
  • That doesn't seem to be working, I dont want to set the widget in in fullscreen, I want to set the whole application to fullscreen – CADman Feb 03 '18 at 13:59
  • I would add a slot in your main window class and connect that to your buttons in your widget in the constructor of the widget. I assume the parent of the widget is the main window. – drescherjm Feb 03 '18 at 14:08
  • An alternate method would be `parentWidget()->setWindowState(Qt::WindowFullScreen);` – drescherjm Feb 03 '18 at 14:10
  • parentWidget()->setWindowState(Qt::WindowFullScreen); Does not work either :/ already tried it but thanks a lot for the input But I tried the second approch in my main QMainWindow (SmartHome.ui) I added: ´ void setFullscreen();´ to .h and .cpp and ´connect(ui->fullscreen_off, SIGNAL(clicked()), this, SLOT(setFullscreen()));´ to heating.cpp constructor but I get: ´undefined reference to `SmartHome::setFullscreen()´ – CADman Feb 03 '18 at 14:24
  • Ok, correct a missing namespace but no I get after starting the application: `QObject::connect: No such slot heating_widget::SmartHome::setFullscreen() QObject::connect: (sender name: 'fullscreen_on') QObject::connect: (receiver name: 'heating_widget')` Using this connect: `connect(ui->fullscreen_on, SIGNAL(clicked()), this, SLOT(SmartHome::setFullscreen()));` – CADman Feb 03 '18 at 14:44
  • The parent of the widget is QStackedWidget – CADman Feb 03 '18 at 14:50
  • Hi. Could you post the line with the `connect(...)` statement that generated said error message? Mabe the *.h file of your `SmartHome` class? You seem to have a misunderstanding there going on. The slot might now be available because it is written differently, isn't in a `public slots:` area, the `Q_OBJECT` is missing, etc. – Dariusz Scharsig Feb 03 '18 at 15:49
  • I've added it to the question, thank you for helping! – CADman Feb 03 '18 at 16:02
  • No idea? Unfortunatly I'm still stuck on this – CADman Feb 04 '18 at 12:39

1 Answers1

0

I would do it in the following way:

void heating_widget::on_fullscreen_on_clicked()
{
  foreach(QWidget *widget, QApplication::topLevelWidgets()) {
    if (auto mainWindow = qobject_cast<SmartHome *>(widget)) {
      mainWindow->setWindowState(Qt::WindowFullScreen);
    }
  }
}

The idea is finding your main window among application top level widgets and change its state. This code can be called from anywhere in your application regardless of the windows hierarchy.

vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Thanks, thats great and it works! Next time I'll do a tutorial on this so that I don't have to ask something like this anymore! – CADman Feb 05 '18 at 16:17