1.void activateWindow();
/*!
\fn void QWidget::activateWindow()
Sets the top-level widget containing this widget to be the active
window.
An active window is a visible top-level window that has the
keyboard input focus.
This function performs the same operation as clicking the mouse on
the title bar of a top-level window. On X11, the result depends on
the Window Manager. If you want to ensure that the window is
stacked on top as well you should also call raise(). Note that the
window must be visible, otherwise activateWindow() has no effect.
On Windows, if you are calling this when the application is not
currently the active one then it will not make it the active
window. It will change the color of the taskbar entry to indicate
that the window has changed in some way. This is because Microsoft
does not allow an application to interrupt what the user is currently
doing in another application.
\sa isActiveWindow(), window(), show()
*/
void QWidget::activateWindow()
{
QWindow *const wnd = window()->windowHandle();
if (wnd)
wnd->requestActivate();
}
/*!
Requests the window to be activated, i.e. receive keyboard focus.
\sa isActive(), QGuiApplication::focusWindow()
*/
void QWindow::requestActivate()
{
Q_D(QWindow);
if (flags() & Qt::WindowDoesNotAcceptFocus) {
qWarning() << "requestActivate() called for " << this << " which has Qt::WindowDoesNotAcceptFocus set.";
return;
}
if (d->platformWindow)
d->platformWindow->requestActivateWindow();
}
void QWindowsWindow::requestActivateWindow()
{
qCDebug(lcQpaWindows) << __FUNCTION__ << this << window();
// 'Active' state handling is based in focus since it needs to work for
// child windows as well.
if (m_data.hwnd) {
const DWORD currentThread = GetCurrentThreadId();
bool attached = false;
DWORD foregroundThread = 0;
// QTBUG-14062, QTBUG-37435: Windows normally only flashes the taskbar entry
// when activating windows of inactive applications. Attach to the input of the
// currently active window while setting the foreground window to always activate
// the window when desired.
const auto activationBehavior = QWindowsIntegration::instance()->windowActivationBehavior();
if (QGuiApplication::applicationState() != Qt::ApplicationActive
&& activationBehavior == QWindowsApplication::AlwaysActivateWindow) {
if (const HWND foregroundWindow = GetForegroundWindow()) {
foregroundThread = GetWindowThreadProcessId(foregroundWindow, nullptr);
if (foregroundThread && foregroundThread != currentThread)
attached = AttachThreadInput(foregroundThread, currentThread, TRUE) == TRUE;
if (attached) {
if (!window()->flags().testFlag(Qt::WindowStaysOnBottomHint)
&& !window()->flags().testFlag(Qt::WindowStaysOnTopHint)
&& window()->type() != Qt::ToolTip) {
const UINT swpFlags = SWP_NOMOVE | SWP_NOSIZE | SWP_NOOWNERZORDER;
SetWindowPos(m_data.hwnd, HWND_TOPMOST, 0, 0, 0, 0, swpFlags);
SetWindowPos(m_data.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, swpFlags);
}
}
}
}
SetForegroundWindow(m_data.hwnd);
SetFocus(m_data.hwnd);
if (attached)
AttachThreadInput(foregroundThread, currentThread, FALSE);
}
}
可以,看到在Windows下的实现是通过调用SetForegroundWindow()函数使窗口激活。
2.void showMinimized();
/*!
Shows the widget minimized, as an icon.
Calling this function only affects \l{isWindow()}{windows}.
\sa showNormal(), showMaximized(), show(), hide(), isVisible(),
isMinimized()
*/
void QWidget::showMinimized()
{
bool isMin = isMinimized();
if (isMin && isVisible())
return;
ensurePolished();
if (!isMin)
setWindowState((windowState() & ~Qt::WindowActive) | Qt::WindowMinimized);
setVisible(true);
}
3.void showMaximized();
/*!
Shows the widget maximized.
Calling this function only affects \l{isWindow()}{windows}.
On X11, this function may not work properly with certain window
managers. See the \l{Window Geometry} documentation for an explanation.
\sa setWindowState(), showNormal(), showMinimized(), show(), hide(), isVisible()
*/
void QWidget::showMaximized()
{
ensurePolished();
setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowFullScreen))
| Qt::WindowMaximized);
setVisible(true);
}
4.void showFullScreen();
/*!
Shows the widget in full-screen mode.
Calling this function only affects \l{isWindow()}{windows}.
To return from full-screen mode, call showNormal().
Full-screen mode works fine under Windows, but has certain
problems under X. These problems are due to limitations of the
ICCCM protocol that specifies the communication between X11
clients and the window manager. ICCCM simply does not understand
the concept of non-decorated full-screen windows. Therefore, the
best we can do is to request a borderless window and place and
resize it to fill the entire screen. Depending on the window
manager, this may or may not work. The borderless window is
requested using MOTIF hints, which are at least partially
supported by virtually all modern window managers.
An alternative would be to bypass the window manager entirely and
create a window with the Qt::X11BypassWindowManagerHint flag. This
has other severe problems though, like totally broken keyboard focus
and very strange effects on desktop changes or when the user raises
other windows.
X11 window managers that follow modern post-ICCCM specifications
support full-screen mode properly.
\sa showNormal(), showMaximized(), show(), hide(), isVisible()
*/
void QWidget::showFullScreen()
{
ensurePolished();
setWindowState((windowState() & ~(Qt::WindowMinimized | Qt::WindowMaximized))
| Qt::WindowFullScreen);
setVisible(true);
#if !defined Q_OS_QNX // On QNX this window will be activated anyway from libscreen
// activating it here before libscreen activates it causes problems
activateWindow();
#endif
}
5.void showNormal();
/*!
Restores the widget after it has been maximized or minimized.
Calling this function only affects \l{isWindow()}{windows}.
\sa setWindowState(), showMinimized(), showMaximized(), show(), hide(), isVisible()
*/
void QWidget::showNormal()
{
ensurePolished();
setWindowState(windowState() & ~(Qt::WindowMinimized
| Qt::WindowMaximized
| Qt::WindowFullScreen));
setVisible(true);
}