0
点赞
收藏
分享

微信扫一扫

《QDebug 2022年4月》

飞空之羽 2022-04-30 阅读 73
Qt

一、Qt Widgets 问题交流

二、Qt Quick 问题交流

三、其他

1.环境变量设置 QML2_IMPORT_PATH 后,与之不同版本的 QML 程序启动失败

如环境变量设置 Qt5.15:

但是打包的程序是 Qt5.12 等其他版本,会导致 QML 组件不能正常加载:

即使设置了 qt.conf 文件的导入路径,还是不能正常加载。

可以在代码中设置 QML2_IMPORT_PATH 环境变量:

qputenv("QML2_IMPORT_PATH","");
QApplication app(argc, argv);

或者设置 QQmlEngine 的 setImportPathList:

    QQmlApplicationEngine engine;
    QStringList qml_path;
    //Qt6 location接口改名为了path
    qml_path.push_front(QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath););
    engine.setImportPathList(qml_path);

这样就能覆盖掉环境变量中不同版本的 QML 组件路径。 

2.Qt Remote Object source 端多线程 emit 信号,client 只能触发第一次

示例工程:https://github.com/gongjianbo/MyTestCode2021/tree/master/Qt/QtRemoteObjects

将 source 服务端每次都 new 一个新的线程来处理任务,结束之后在该线程中 emit 信号,但 client 客户端关联该信号却只能触发第一次。

    //信号测试
    connect(ui->btnSignal,&QPushButton::clicked,[this]{
        std::thread th([this](){
            emit source.dataChanged("server dataChanged");
        });
        th.detach();
    });

可将信号触发放到一个固定的线程中:

    //信号测试
    connect(ui->btnSignal,&QPushButton::clicked,[this]{
        std::thread th([this](){
            QMetaObject::invokeMethod(&source,[=]{
                emit source.dataChanged("server dataChanged");
            },Qt::QueuedConnection);
        });
        th.detach();
    });
举报

相关推荐

0 条评论