PDA

View Full Version : windowState() doesn't retrun the enum Qt::WindowState?



shawnlau
7th July 2018, 18:41
From the documentation:

"Qt::WindowState QWindow::windowState() const
the screen-occupation state of the window
See also setWindowState() and windowStates()."

So isn't it supposed to return a Qt::WindowState?

Well this code fails.
.pro


QT+= widgets
HEADERS += \
mainwindow.h

SOURCES += \
main.cpp


mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
void getState();
signals:

public slots:
private:
Qt::WindowState m_state;
};

#endif // MAINWINDOW_H


main.cpp


#include <QApplication>

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
resize(400,400);
}

void MainWindow::getState()
{
m_state = windowState();
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();

return app.exec();
}


Compiler error:

Y:\QTProjects\Problems\myWindowState\main.cpp:12: error: C2440: '=': cannot convert from 'Qt::WindowStates' to 'Qt::WindowState'
Y:\QTProjects\Problems\myWindowState\main.cpp:12: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

What am I missing?
I need to be able to save m_state, and use it to setWindowState(m_state)

d_stranz
8th July 2018, 04:52
The compiler doesn't lie. QWidget::windowState() returns Qt::WindowStates, not Qt::WindowState. QMainWindow is a QWidget, not a QWindow.

shawnlau
8th July 2018, 16:21
I see, I was looking at the documentation for the window class.

d_stranz
8th July 2018, 21:11
Yes. So why do they call it QMainWindow instead of QMainWidget when it is actually a QWidget-based class? No wonder people have such a hard time with some parts of Qt.

shawnlau
9th July 2018, 03:48
Good point! :)