Flameshot源码及分析3 —— gui函数解析

本文深入解析了Flameshot截图工具的GUI实现过程,重点介绍了如何在不同操作系统上处理截图请求,并展示了如何通过Qt的showFullScreen方法实现全屏截图。

接前一篇文章《Flameshot源码及分析2 —— 主函数调用流程》,链接为:

Flameshot源码及分析2 —— 主函数调用流程_蓝天居士的博客-CSDN博客

上一回我们讲到了gui函数,这个函数的实现在src/core/flameshot.cpp中,源码如下:

CaptureWidget* Flameshot::gui(const CaptureRequest& req)
{
    if (!resolveAnyConfigErrors()) {
        return nullptr;
    }

#if defined(Q_OS_MACOS)
    // This is required on MacOS because of Mission Control. If you'll switch to
    // another Desktop you cannot take a new screenshot from the tray, you have
    // to switch back to the Flameshot Desktop manually. It is not obvious and a
    // large number of users are confused and report a bug.
    if (m_captureWindow != nullptr) {
        m_captureWindow->close();
        delete m_captureWindow;
        m_captureWindow = nullptr;
    }
#endif

    if (nullptr == m_captureWindow) {
        // TODO is this unnecessary now?
        int timeout = 5000; // 5 seconds
        const int delay = 100;
        QWidget* modalWidget = nullptr;
        for (; timeout >= 0; timeout -= delay) {
            modalWidget = qApp->activeModalWidget();
            if (nullptr == modalWidget) {
                break;
            }
            modalWidget->close();
            modalWidget->deleteLater();
            QThread::msleep(delay);
        }
        if (0 == timeout) {
            QMessageBox::warning(
              nullptr, tr("Error"), tr("Unable to close active modal widgets"));
            return nullptr;
        }

        m_captureWindow = new CaptureWidget(req);

#ifdef Q_OS_WIN
        m_captureWindow->show();
#elif defined(Q_OS_MACOS)
        // In "Emulate fullscreen mode"
        m_captureWindow->showFullScreen();
        m_captureWindow->activateWindow();
        m_captureWindow->raise();
#else
        m_captureWindow->showFullScreen();
//        m_captureWindow->show(); // For CaptureWidget Debugging under Linux
#endif
        return m_captureWindow;
    } else {
        emit captureFailed();
        return nullptr;
    }
}

笔者的电脑安装的是Linux系统,因此经过跟踪,代码最终会走到以下分支:

#else
        m_captureWindow->showFullScreen();
//        m_captureWindow->show(); // For CaptureWidget Debugging under Linux

showFullScreen()的声明在/usr/include/x86_64-linux-gnu/qt5/QtGui/qwindow.h中:

void showFullScreen();

看函数名就能知道,这是Qt中显示全屏的函数。这里引用博客文章Qt窗口全屏方法_Qt君的博客-CSDN博客_qt全屏,给出使用示例:

Qt窗口全屏方法

方法一 使用showFullScreen方法显示全屏

示例:

QWidget w;
w.showFullScreen();

方法二 使用setWindowState方法设置

示例:

QWidget w;
w.setWindowState(w.windowState() ^ Qt::WindowFullScreen);
w.show();

注意事项:

  •     适合于父窗口;
  •     设置子窗口全屏需要遵循父窗口全屏。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蓝天居士

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

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

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

打赏作者

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

抵扣说明:

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

余额充值