【问题标题】:setWindowState from QMainWindow from QWidget来自 QWidget 的 QMainWindow 的 setWindowState
【发布时间】: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()-&gt;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


【解决方案1】:

我会按照以下方式进行:

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

这个想法是在应用程序顶级小部件中找到您的主窗口并更改其状态。无论 Windows 层次结构如何,都可以从应用程序中的任何位置调用此代码。

【讨论】:

  • 谢谢,太好了,而且有效!下次我会做一个关于这个的教程,这样我就不必再问这样的问题了!
猜你喜欢
  • 2014-05-18
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多