Skip to content

Commit

Permalink
Introduce QWindow::setFlag and QWidget::setWindowFlag
Browse files Browse the repository at this point in the history
Analogous to QWidget::setAttribute(), introduce an API to easily
enable/disable a single window flag without having to resort to
w.setFlags(w.flags() | Qt::NewFlag).

Change-Id: Ib0f7254a34c8d884cdec181c41b99e5ef035d954
Reviewed-by: Friedemann Kleint <[email protected]>
Reviewed-by: Sérgio Martins <[email protected]>
  • Loading branch information
danvratil committed Nov 22, 2016
1 parent a01f211 commit babc7c5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/gui/kernel/qwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,8 @@ QSurfaceFormat QWindow::format() const
The actual window flags might differ from the flags set with setFlags()
if the requested flags could not be fulfilled.
\sa setFlag()
*/
void QWindow::setFlags(Qt::WindowFlags flags)
{
Expand All @@ -839,6 +841,23 @@ Qt::WindowFlags QWindow::flags() const
return d->windowFlags;
}

/*!
\since 5.9
Sets the window flag \a flag on this window if \a on is true;
otherwise clears the flag.
\sa setFlags(), flags(), type()
*/
void QWindow::setFlag(Qt::WindowType flag, bool on)
{
Q_D(QWindow);
if (on)
setFlags(d->windowFlags | flag);
else
setFlags(d->windowFlags & ~flag);
}

/*!
Returns the type of the window.
Expand Down
1 change: 1 addition & 0 deletions src/gui/kernel/qwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class Q_GUI_EXPORT QWindow : public QObject, public QSurface

void setFlags(Qt::WindowFlags flags);
Qt::WindowFlags flags() const;
void setFlag(Qt::WindowType, bool on = true);
Qt::WindowType type() const;

QString title() const;
Expand Down
19 changes: 18 additions & 1 deletion src/widgets/kernel/qwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10357,14 +10357,31 @@ void QWidget::updateGeometry()
a window, causing the widget to be hidden. You must call show() to make
the widget visible again..

\sa windowType(), {Window Flags Example}
\sa windowType(), setWindowFlag(), {Window Flags Example}
*/
void QWidget::setWindowFlags(Qt::WindowFlags flags)
{
Q_D(QWidget);
d->setWindowFlags(flags);
}

/*!
\since 5.9

Sets the window flag \a flag on this widget if \a on is true;
otherwise clears the flag.

\sa setWindowFlags(), windowFlags(), windowType()
*/
void QWidget::setWindowFlag(Qt::WindowType flag, bool on)
{
Q_D(QWidget);
if (on)
d->setWindowFlags(data->window_flags | flag);
else
d->setWindowFlags(data->window_flags & ~flag);
}

/*! \internal

Implemented in QWidgetPrivate so that QMdiSubWindowPrivate can reimplement it.
Expand Down
1 change: 1 addition & 0 deletions src/widgets/kernel/qwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ public Q_SLOTS:

void setWindowFlags(Qt::WindowFlags type);
inline Qt::WindowFlags windowFlags() const;
void setWindowFlag(Qt::WindowType, bool on = true);
void overrideWindowFlags(Qt::WindowFlags type);

inline Qt::WindowType windowType() const;
Expand Down
13 changes: 13 additions & 0 deletions tests/auto/gui/kernel/qwindow/tst_qwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ private slots:
void initTestCase();
void stateChange_data();
void stateChange();
void flags();
void cleanup();

private:
Expand Down Expand Up @@ -2194,6 +2195,18 @@ void tst_QWindow::requestUpdate()
QTRY_COMPARE(window.received(QEvent::UpdateRequest), 2);
}

void tst_QWindow::flags()
{
Window window;
const auto baseFlags = window.flags();
window.setFlags(window.flags() | Qt::FramelessWindowHint);
QCOMPARE(window.flags(), baseFlags | Qt::FramelessWindowHint);
window.setFlag(Qt::WindowStaysOnTopHint, true);
QCOMPARE(window.flags(), baseFlags | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
window.setFlag(Qt::FramelessWindowHint, false);
QCOMPARE(window.flags(), baseFlags | Qt::WindowStaysOnTopHint);
}

#include <tst_qwindow.moc>
QTEST_MAIN(tst_QWindow)

5 changes: 5 additions & 0 deletions tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8957,8 +8957,13 @@ void tst_QWidget::taskQTBUG_4055_sendSyntheticEnterLeave()
void tst_QWidget::windowFlags()
{
QWidget w;
const auto baseFlags = w.windowFlags();
w.setWindowFlags(w.windowFlags() | Qt::FramelessWindowHint);
QVERIFY(w.windowFlags() & Qt::FramelessWindowHint);
w.setWindowFlag(Qt::WindowStaysOnTopHint, true);
QCOMPARE(w.windowFlags(), baseFlags | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
w.setWindowFlag(Qt::FramelessWindowHint, false);
QCOMPARE(w.windowFlags(), baseFlags | Qt::WindowStaysOnTopHint);
}

void tst_QWidget::initialPosForDontShowOnScreenWidgets()
Expand Down

0 comments on commit babc7c5

Please sign in to comment.