【发布时间】:2018-07-13 20:41:08
【问题描述】:
我有一个 QMainWindow 应用程序,其中还包括一个 QStackedWidget。
QstackedWidget 的页面被提升为 ui 小部件,例如 heating_widget.ui
如果我在 QMainWindow 上使用按钮槽,我可以使用它来让我的应用程序全屏显示:
void SmartHome::on_fullscreen_on_clicked()
{
SmartHome::setWindowState(Qt::WindowFullScreen);
}
但是我怎样才能通过 heat_widget.cpp 文件中的按钮来做到这一点呢? 使用:
void heating_widget::on_fullscreen_on_clicked()
{
SmartHome::setWindowState(Qt::WindowFullScreen);
}
显然不起作用并向我抛出此错误:
不能调用成员函数'void QWidget::setWindowState(Qt::WindowStates)' 没有对象 SmartHome::setWindowState(Qt::WindowFullScreen);
我知道这与 parent() 有关,但我无法让它工作。
你有什么想法吗?
我的 smarthome.h 文件:
#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
我的加热小部件.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
还有我的 heat.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;
}
【问题讨论】:
-
从
SmartHome::setWindowState中删除SmartHome:: -
这似乎不起作用,我不想将小部件设置为全屏,我想将整个应用程序设置为全屏
-
我会在您的主窗口类中添加一个插槽,并将其连接到小部件构造函数中小部件中的按钮。我假设小部件的父级是主窗口。
-
另一种方法是
parentWidget()->setWindowState(Qt::WindowFullScreen); -
parentWidget()->setWindowState(Qt::WindowFullScreen);也不起作用:/已经尝试过,但非常感谢您的输入但是我在我的主 QMainWindow (SmartHome.ui) 中尝试了第二个方法,我添加了:' void setFullscreen();' 到 .h 和 .cpp 和 'connect (ui->fullscreen_off, SIGNAL(clicked()), this, SLOT(setFullscreen()));' 到 heat.cpp 构造函数,但我得到:'undefined reference to `SmartHome::setFullscreen()'
标签: c++ qt qt5 qstackedwidget