《QDebug 2024年1月》

一、Qt Widgets 问题交流

1.

二、Qt Quick 问题交流

1.Repeator 的 delegate 在 remove 移除时的注意事项

Qt Bug Tracker:https://bugreports.qt.io/browse/QTBUG-47500

Repeator 在调用 remove 函数之后,对应的 Item 会立即释放,后续就无法访问上下文的属性了:

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("Hello World")

    ListModel {
        id: list_model
        ListElement { value: 1 }
        ListElement { value: 2 }
        ListElement { value: 3 }
    }

    Flow {
        anchors.fill: parent
        Repeater {
            id: repeator
            model: list_model
            delegate: Rectangle {
                id: item
                width: 100
                height: 50
                border.color: "black"
                Text {
                    anchors.centerIn: parent
                    text: model.index
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        console.log("before", item.width, repeator.count, model.value)
                        list_model.remove(model.index, 1)
                        console.log("after", item.width, repeator.count, model.value)
                    }
                }
                Component.onDestruction: {
                    console.log("free")
                }
            }
        }
    }
}

测试代码在调用 remove 后,Component.onDestruction 立即触发了,之后只能访问 item 属性,外部属性以及 model role 无法继续访问了。

但如果把 Repeator 换成 ListView,Component.onDestruction 在 onClicked 执行完后才会触发。

2.非动态创建的对象调用 destroy() 不触发 Component.onDestruction

如下代码调用 destory 后没有触发 Component.onDestruction:

javascript 复制代码
    Rectangle {
        id: item
        width: 300
        height: 300
        anchors.centerIn: parent
        color: "red"
        MouseArea {
            anchors.fill: parent
            onClicked: {
                item.destroy()
            }
        }
        Component.onDestruction: {
            console.log("item free")
        }
    }

如果替换成自定义的 C++ 类,在习惯加上打印,会发现析构是执行了的:

cpp 复制代码
#include <QQuickItem>
#include <QDebug>

class CppItem : public QQuickItem
{
    Q_OBJECT
public:
    explicit CppItem(QQuickItem *parent = nullptr)
        : QQuickItem(parent) {
        qDebug() << Q_FUNC_INFO;
    }
    ~CppItem(){
        qDebug() << Q_FUNC_INFO;
    }
};
javascript 复制代码
    CppItem {
        id: cpp_item
        Component.onCompleted: {
            console.log("init")
        }
        Component.onDestruction: {
            console.log("free")
        }
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            cpp_item.destroy()
        }
    }
3.View 嵌套的问题

QML 自带的 ListView、GridView、TableView 等,在可视区域和 cacheBuffer 范围外的元素一般是没有在初始化时加载的,只有显示对应的元素时才动态加载。当组合 View 实现更复杂的效果时会有个问题,子级 View 一般需要加载全部内容才能得到这一级的宽高,或者二级嵌套的 Flow + Repeator 等默认就全部加载的组件。当子级 View 内容很多,或者加载了大量图片数据,内存占用就会很高。

这里还有个恶心的地方,无论是 QML 还是 Widgets,View 的相关源码都用到一些未导出的类,这就导致很难直接子类化 C++ View 类然后重写部分接口,要么把相关类都 Copy 一下,要么得改源码重新编译下 Qt 对应模块。

三、其他

1.
相关推荐
TechNomad24 分钟前
QML开发:QML的第一个程序
qt
浮生卍流年1 小时前
C++模板知识点3『std::initializer_list初始化时逗号表达式的执行顺序』
开发语言·c++·qt
huxiao_060117 小时前
如何手动打包 Linux(麒麟系统)的 Qt 程序
linux·qt
咕噜咕噜啦啦17 小时前
Qt按键响应
开发语言·qt
若水晴空初如梦21 小时前
QT聊天项目DAY17
开发语言·qt
小灰灰搞电子1 天前
Qt 使用QtXlsx库处理Excel文件
qt·excel
刃神太酷啦1 天前
C++ 容器适配器与核心数据结构精解:栈、队列、deque 底层实现与实战应用----《Hello C++ Wrold!》(17)--(C/C++)
java·c语言·数据结构·c++·qt·算法·leetcode
残醉1 天前
QT的UDP
qt·udp
feiyangqingyun1 天前
Qt结合ffmpeg实现图片参数调节/明亮度对比度饱和度设置/滤镜的使用
qt·ffmpeg·明亮度饱和度对比度
程序员编程指南1 天前
Qt 嵌入式 Linux 系统定制全指南
linux·c语言·开发语言·c++·qt