《QDebug 2025年12月》

一、Qt Widgets 问题交流

1.

二、Qt Quick 问题交流

1.Qt5.15非动态创建的子窗口onCompleted时调用show不显示

测试环境:Qt5.15.2 + MSVC2019/2022+Windows10/11

如测试代码所示:子窗口直接show无法显示,需要设置visible=true,或者动态创建子窗口

测试代码:

javascript 复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

Window {
    id: root
    width: 640
    height: 480
    visible: true
    title: qsTr("Root")

    Window {
        id: sub1
        width: 400
        height: 300
        title: qsTr("Sub1")
    }

    Component {
        id: comp2
        Window {
            id: sub2
            width: 400
            height: 300
            title: qsTr("Sub2")
        }
    }

    Component.onCompleted: {
        // Qt5初始化的时候子窗口就show,会自动关闭,设置visible后就不会
        sub1.show()
        // sub1.visible = true
        // 动态创建并show可以正常显示
        // let obj = comp2.createObject(root)
        // obj.show()
    }
}

三、其他

1.在QtConcurrent::run抛出std异常的捕获问题

如果QtConcurrent::run返回的QFuture用Qt6的链式接口onFailed捕获异常,当run中抛出std异常时,用QException或者std::exception是获取不到异常内容的,需要用QUnhandledException。

run的实现中对于非QException异常,包装成了QUnhandledException再抛出去,原始的异常信息作为QUnhandledException的成员数据,直接用QException/std::exception自然拿不到具体的异常信息了。

cpp 复制代码
    void run() override
    {
        if (promise.isCanceled()) {
            promise.reportFinished();
            return;
        }
#ifndef QT_NO_EXCEPTIONS
        try {
#endif
            runFunctor();
#ifndef QT_NO_EXCEPTIONS
        } catch (QException &e) {
            promise.reportException(e);
        } catch (...) {
            promise.reportException(QUnhandledException(std::current_exception()));
        }
#endif
        promise.reportFinished();
    }

测试代码:

cpp 复制代码
    QFuture<void> future = QtConcurrent::run([](){
        qDebug() << "doing" << QThread::currentThread();
        throw std::runtime_error("err");
        QThread::sleep(1);
    });
    future.cancel();
    future.then([] {
              qDebug() << "then" << QThread::currentThread();
              QThread::sleep(1);
          }).onFailed([](const std::runtime_error &e) {
            qWarning() << "failed runtime_error" << e.what() << QThread::currentThread();
        }).onFailed([](const QUnhandledException &e) {
            try {
                std::rethrow_exception(e.exception());
            }
            catch (const std::exception &ex) {
                qDebug() << "QUnhandledException" << ex.what() << QThread::currentThread();
            }
            // qWarning() << "failed QException" << e.what() << QThread::currentThread();
        }).onFailed([](const std::exception &e) {
            qWarning() << "failed exception" << e.what() << QThread::currentThread();
        }).onCanceled([] {
            qWarning() << "canceled" << QThread::currentThread();
        }).then([] {
            // 第一个future逻辑结束后会进入后续的then
            // Qt6.10 future提供了cancelChain取消整个链
            qDebug() << "then 2" << QThread::currentThread();
        });
    // future.cancel();
2.Windows上窗口置前问题

直接调用 SetForegroundWindow 只会在任务栏闪烁,并没有将非当前活跃的窗口置前;使用 SetWindowPos 切换 HWND_TOPMOST 状态也只是窗口置前,任务栏上的当前窗口还是没有切换过来的。

参考解决方法:https://www.cnblogs.com/xuhuajie/p/14782205.html

相关文档:https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow

想要 SetForegroundWindow 生效,必须得是前置的进程(活动的进程),可以用以下方法实现:

cpp 复制代码
// 跨进程SetForegroundWindow 会失效,需要如下代码搞一搞。
HWND hForeWnd = ::GetForegroundWindow();
DWORD dwCurID = ::GetCurrentThreadId();
DWORD dwForeID = ::GetWindowThreadProcessId(hForeWnd, NULL);
::AttachThreadInput(dwCurID, dwForeID, TRUE);
::ShowWindow(m_hWnd, SW_SHOWNORMAL);
::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
::SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
::SetForegroundWindow(m_hWnd);
::BringWindowToTop(m_hWnd);
::AttachThreadInput(dwCurID, dwForeID, FALSE);
相关推荐
程序leo源11 小时前
Qt窗口详解
开发语言·数据库·c++·qt·青少年编程·c#
我在人间贩卖青春12 小时前
重学Qt——事件处理
qt
小宋00112 小时前
QT中控件qss样式修改
开发语言·qt
yuechuji00116 小时前
三、MPR(三平面重建)和三视图
qt
Hua-Jay18 小时前
OpenCV联合C++/Qt 学习笔记(二十二)----相机模型与投影及单目相机标定
c++·笔记·qt·opencv·学习·计算机视觉
小短腿的代码世界20 小时前
QCefView架构深度解析:从Chromium嵌入到Qt信号槽集成的完整技术链路
qt·架构
byxdaz20 小时前
Qt修改操作系统的日期与时间
qt
小短腿的代码世界20 小时前
Qt属性系统揭秘:从Q_PROPERTY宏到动态元对象系统的完整架构解析
开发语言·qt·架构
丁劲犇21 小时前
QodeAssist:为msys2 ucrt64 Qt Creator 注入 AI 灵魂的开源插件
开发语言·人工智能·qt
listhi52021 小时前
基于QT的串口心电波形实时显示系统
开发语言·qt