Qt源码分析--QWidget(2)--Top-level windows

59 篇文章 1 订阅
37 篇文章 2 订阅

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);
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,以下是在QWidget中使用VLC-Qt进行截图的步骤: 1. 首先,确保已经按照引用中提供的步骤正确编译和配置了VLC-Qt。 2. 在你的QWidget类中,添加VlcInstance和VlcMediaPlayer对象作为成员变量,并在构造函数中进行初始化。 ```cpp #include <VLCQtCore/Instance.h> #include <VLCQtCore/MediaPlayer.h> class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr) : QWidget(parent) { // 初始化VLC实例 vlcInstance = new VlcInstance(VlcCommon::args(), this); // 创建VLC媒体播放器 vlcMediaPlayer = new VlcMediaPlayer(vlcInstance); } private: VlcInstance *vlcInstance; VlcMediaPlayer *vlcMediaPlayer; }; ``` 3. 在需要截图的地方,使用VlcMediaPlayer的grabVideo()函数进行截图,并将截图保存到文件。 ```cpp QString saveScreenshot(const QString &filePath) { // 使用VlcMediaPlayer的grabVideo()函数进行截图 QImage screenshot = vlcMediaPlayer->grabVideo(); // 将截图保存到文件 screenshot.save(filePath); return filePath; } ``` 注意:在调用grabVideo()函数之前,确保VLC媒体播放器已经加载了媒体文件并开始播放。 4. 调用saveScreenshot()函数进行截图,并指定保存截图的文件路径。 ```cpp QString screenshotFilePath = saveScreenshot("screenshot.png"); ``` 这将保存截图为名为"screenshot.png"的文件。 5. 最后,你可以在QWidget中显示截图,例如使用QLabel来显示截图。 ```cpp QLabel *screenshotLabel = new QLabel(this); screenshotLabel->setPixmap(QPixmap(screenshotFilePath)); screenshotLabel->show(); ``` 这将在QWidget中显示截图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天进步2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值