《QDebug 2024年3月》

一、Qt Widgets 问题交流

1.

二、Qt Quick 问题交流

1.Qt5 ApplicationWindow 不能使用父组件 Window 的 transientParent 属性

ApplicationWindow 使用 transientParent 报错:

"ApplicationWindow.transientParent" is not available due to component versioning.

这个属性是 C++ 类 QWindow 定义的,而且比较特殊,用的 Q_PRIVATE_PROPERTY,版本也较高是 13

Q_PRIVATE_PROPERTY(QWindow::d_func(), QWindow* transientParent MEMBER transientParent WRITE setTransientParent NOTIFY transientParentChanged REVISION 13)

Qt Controls 引入的版本较低(尖括号里的版本号):

qmlRegisterRevision<QQuickWindow, 2>(uri, 2, 0);
qmlRegisterRevision<QWindow, 3>(uri, 2, 0);
qmlRegisterRevision<QQuickWindowQmlImpl, 3>(uri, 2, 2);

本来想在 main 函数重新注册成高版本,不过会报错,那还是改源码重新编译吧:

qmlRegisterRevision<QWindow, 15>("QtQuick.Controls", 2, 0);

plugin cannot be loaded for module "QtQuick.Controls": Namespace 'QtQuick.Controls' has already been used for type registration 

2.QML 中动态创建的对象没变量引用可能会被 gc 释放

比如 Qt.createComponent() +  createObject() 创建的对象;或者 C++ 创建但是 ObjectOwnership 是 JavaScriptOwnership 的对象,都需要注意。

3.ColorAnimation 不能调用 start 开始变色,RotationAnimation 却可以执行

ColorAnimation 的示例只有 ColorAnimation on color {} 和 Transition 两种,从组件名字上看 ColorAnimation 就是对 attach 的对象变色,其实只有 RotationAnimation 有默认属性,ColorAnimation 是没有的:

QQuickColorAnimation::QQuickColorAnimation(QObject *parent)
: QQuickPropertyAnimation(parent)
{
    Q_D(QQuickPropertyAnimation);
    d->interpolatorType = QMetaType::QColor;
    d->defaultToInterpolatorType = true;
    d->interpolator = QVariantAnimationPrivate::getInterpolator(d->interpolatorType);
}

QQuickRotationAnimation::QQuickRotationAnimation(QObject *parent)
: QQuickPropertyAnimation(*(new QQuickRotationAnimationPrivate), parent)
{
    Q_D(QQuickRotationAnimation);
    d->interpolatorType = QMetaType::QReal;
    d->interpolator = QVariantAnimationPrivate::getInterpolator(d->interpolatorType);
    d->defaultProperties = QLatin1String("rotation,angle");
}

所以你调用 start 他也不知道干啥,需要设置 property 为 color:

    Rectangle {
        id: rect
        width: 200
        height: 200
        color: "red"
        ColorAnimation {
            id: ani
            target: rect
            property: "color"
            from: "white"
            to: "black"
            duration: 200
        }
        MouseArea {
            anchors.fill: parent
            onClicked: ani.start()
        }
    }
4.图片缩放 

没找到合适的图来体现不同设置的区别,暂略

        // 默认缩放,有像素锯齿,相当于 QImage 的 FastTransformation
        Image {
            width: 400
            height: 250
            source: "qrc:/img.png"
            fillMode: Image.PreserveAspectFit
        }
        // smooth 和多重采样的效果差不多,比较模糊
        Image {
            width: 400
            height: 250
            source: "qrc:/img.png"
            smooth: true
            fillMode: Image.PreserveAspectFit
        }
        // 和 QImage 的 SmoothTransformation 差不多,略有不同
        Image {
            width: 400
            height: 250
            source: "qrc:/img.png"
            sourceSize: Qt.size(width, height)
            fillMode: Image.PreserveAspectFit
        }

三、其他

1.搜索代码中的中文字符串

程序做翻译的时候需要找出里面的中文字符进行处理,比如 Qt 框架加上 tr 等。

先百度中文的正则范围:[\u4e00-\u9fa5],再在两边加上双引号或者单引号组成最终的正则。

在 VSCode 中需要勾选正则表达式搜索:

".*[\u4e00-\u9fa5]+.*"

Qt Creator 也支持正则搜索:

但是会提示不支持 \U 等:

我们可以替换 Unicode 编码为对应的中文进行搜索:

".*[一-龥]+.*"

如果只搜索不带 tr 的字符串,就搜双引号前不是 r 字母的字符串:

[^r]\(".*[\u4e00-\u9fa5]+.*" 

注意 QML 可以单引号字符串,所以最好统一风格,搜索起来简单点。还有就是编码的时候规范一点,别到处写一堆空格,比如 tr(空格或者换行+"字符串内容"+空格或者换行) 。

2.QMake 中 $$ 拼接宏的时候,如果是没定义的,不会报错

如 pro 中这样写:

DESTDIR = $$PWD/bin/$$ABCD

其中 ABCD 未定义,但是不会报错,而是在 bin 目录下生成了 exe

相关推荐

  1. GESP C++二级认证真题 20243

    2024-04-01 10:16:05       23 阅读
  2. 20243个人工作生活总结

    2024-04-01 10:16:05       16 阅读
  3. 20243调研学习文档资料汇总

    2024-04-01 10:16:05       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-01 10:16:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-01 10:16:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-01 10:16:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-01 10:16:05       18 阅读

热门阅读

  1. 基于多模态脑机接口的智能小车自动驾驶系统

    2024-04-01 10:16:05       14 阅读
  2. Spring 中 @Component 和 @Bean 区别

    2024-04-01 10:16:05       17 阅读
  3. uniapp和vue的区别?

    2024-04-01 10:16:05       26 阅读
  4. C#网站系统如何监控登录过期

    2024-04-01 10:16:05       15 阅读
  5. MySQL 看库大表

    2024-04-01 10:16:05       14 阅读
  6. ARP原理

    ARP原理

    2024-04-01 10:16:05      11 阅读