QT5 升级到 Qt6, QRegExp 不能用

需求 Qt5 的工程升级到 Qt6 之后,找不到 QRegExp 等一系列类。 解决 QRegExp -> QRegularExpression QRegExpValidator -> QRegularExpressionValidator // Qt5 QRegExp version(QLatin1String("(.+)_v(\\d+)")); if (version.exactMatch(completeBaseName/*QString*/)) { // some code } // Qt6 QRegularExpression version(QLatin1String("(.+)_v(\\d+)")); QRegularExpressionMatch match = version.match(completeBaseName); if (match.hasMatch()) { // Find exact match or not } 参考 QT6找不到QRegExpValidator类问题解决办法 关于QRegExpValidator头文件不存在的问题 Qt6中的端口QRegExp::exactMatch()

2023-12-02 · 1 min · 47 words · RamLife

QT qml和c++交互方式介绍

需求 qt 中 qml 和 c++ 中的类如何进行交互 解决 c++ 类注册到元对象系统 通过使用上下文属性,可以将C++对象嵌入到QML环境中。上下文属性适用于简单的应用程序。它们将您的对象导出为全局对象。在QML引擎实例化之后,上下文被暴露给QML环境。 调用函数 QQmlApplicationEngine engine; QmlCpp qmlcpp; // 先初始化一个类的实例 qmlcpp.setValue(898); // 设初值 // 将这个 C++ 实例注册到 Qml 引擎上下文中标示为 “qmlpro” 的名字, 这样 Qml 中就可以通过 qmlpro 来访问这个 C++ 实例。 engine.rootContext()->setContextProperty("qmlpro",&qmlcpp); class QmlCpp : public QObject { Q_OBJECT public: explicit QmlCpp(QObject *parent = nullptr); // Q_INVOKABLE: // Apply this macro to declarations of member functions to allow them to be invoked via the meta-object system....

2023-10-09 · 4 min · 775 words · RamLife

QT property 介绍

需求 qt 中 property 和 Q_PROPERTY 如何使用? 解决 qml 在 qml 中使用 property 来定义一个对象的属性。具体语法如下: [default] [required] [readonly] property <propertyType> <propertyName> 特性 类似于成员变量,不同的是可以初始化,并且没有public、private、 protected等限制。 可以使用 onXXXChnaged 作为这个属性的信号处理函数. property string someText onSomeTextChanged: console.log("The someText will be: " + someText) properName以一个小写字母开头,只能包括字母、数字和下划线。 propertyType可以是QML基本类型,enumeration以int来代替,也可以是QML对象类型,神奇的var类型是泛型的,支持任何类型的属性值. Item { property int theNumber property string theString property url theUrl property Item someItem property Rectangle someRectangle property var someNumber: 1.5 property var someString: "abc" property var someBool: true property var someList: [1, 2, "three", "four"] property var someObject: Rectangle { width: 100; height: 100; color: "red" } } 属性值可以被初始化,也可以使用JavaScript表达式来赋值,通过这两种方式赋值时,可以是一个静态值,也可以是一个与其它属性绑定的值。 Rectangle { id: rootRect property color theColor: "green" property color previousColor: rootRect....

2023-10-07 · 3 min · 497 words · RamLife

QT qprocess 调用进程,输出到文件,实时输出

需求 使用 QProcess 调用 ping, 并输出结果到文件,或者实时输出显示。 解决 调用进程 std::shared_ptr<QProcess> process_ = nullptr; process_ = std::make_shared<QProcess>(this); process_->start("ping www.baidu.com"); if (process_ != nullptr) { //process_->close(); process_->kill(); process_->waitForFinished(); process_ = nullptr; } close() 可以直接关闭子进程 kill() 用于向子进程发送 SIGKILL, 然后通过 waitForFinished() 来等待子进程退出。 输出到文件 在 start() 之前: process_->setStandardOutputFile("out.txt"); 实时输出显示 在 start() 之前: process_->setReadChannel(QProcess::StandardOutput); connect(process_.get(), &QProcess::readyRead, [=](){ qDebug() << process_->readAllStandardOutput(); }); 记得使用 closeReadChannel() 来关闭读取通道。 参考 QT QProcess 重定向问题 QProcess开启外部程序,实时获取该程序的标准输出 QProcess快速实现外部程序调用 Qt开发之路34—QProcess重定向子进程的日志输出 QT: 使用QProcess启动进程并实时获取标准输出 Qt 之启动外部程序

2023-08-09 · 1 min · 68 words · RamLife

QT 信号槽和回调函数的区别

需求 信号槽和回调函数区别 解决 方便 信号和槽相对更加方便。 回调函数和接口在每次使用的时候,都需要额外判断函数指针是否为空。 当需要回调多个函数的时候,还需要管理多个函数指针。 当调用和被调用中间隔了几个类,回调就会很麻烦,需要把指针一路传递过去。 速度 回调函数和接口这样直接使用的明显会更加快速,信号和槽会慢一些。曾经出现一个问题,使用信号和槽在断开连接时只打印一次,使用回调函数会打印两次,经过调试发现,使用信号和槽在调试模式也会打印两次。初步判断是信号虽然触发了两次,但是因为时间过短,所以导致只执行了一次槽函数。 参考

2023-07-28 · 1 min · 11 words · RamLife

QT QFutureWatcher 介绍

需求 QFutureWatcher ? 解决 QFutureWatcher 一般是搭配 QFuture, QtConcurrent 来使用,用于检测多线程异步执行计算的进度,方便在主线程上进行展示或者执行其他动作。 配置 watcher watcher_ = new QFutureWatcher<int>; connect(watcher_, &QFutureWatcher<int>::finished, this, &MainWindow::busy_job_finished); 调用子线程来执行 QtConcurrent::run 提供了最简单的子线程执行函数的方法。 auto future = QtConcurrent::run(this, &MainWindow::do_busy_job); watcher_->setFuture(future); int MainWindow::do_busy_job() { return 1; } 执行结果 void MainWindow::busy_job_finished() { // 若有需要, 关闭通知对话框 qDebug() << "busy job finished!"; qDebug() << "the returned value is: " << watcher_->result(); } 参考 在 QT UI 编程中使用 QtConcurrent 和 QFutureWatcher 执行并发任务 使用QFuture类监控异步计算的结果 Qt多线程编程之高级函数 QFutureWatcher:异步运行监视者 Qt多线程:QtConcurrent + QFuture + QFutureWatcher...

2023-07-23 · 1 min · 77 words · RamLife

QT QHash 介绍

需求 QHash ? 解决 QHash<Key, T> 是类似于 QMap 的键值对,但是与 QMap 的区别是: QHash 比 QMap 查找更快,但是所需空间更大。 QMap 默认键值升序排序, QHash 任意排序 QMap 键类型必须提供 operator<(), QHash 键类型必须提供 operator==() 和 qHash() 全局哈希函数。 参考 QT之QHash简介 Qt:QHash和QMap区别

2023-07-23 · 1 min · 31 words · RamLife

QT 警告 QObject::connect: Cannot queue arguments of type

需求 qt 程序在运行过程中,报了警告 QObject::connect: Cannot queue arguments of type 'qintptr' 解决 跨线程的信号和槽中,如果出现自定义的类型,那么就会报这个问题。解决也很简单。 在使用这个自定义类的信号和槽的类源文件中,增加头文件 #include <QMetaType>. 在这个类的构造函数中添加 qRegisterMetaType #include <QMetaType> TcpSocket::TcpSocket(qintptr socketDescriptor, QObject *parent) : QTcpSocket(parent), socketID(socketDescriptor) { qRegisterMetaType<qintptr>("qintptr"); this->setSocketDescriptor(socketDescriptor); ... } 参考 QObject::connect: Cannot queue arguments of type “xxx"的解决方案 Qt 线程间信号槽传递自定义数据类型(qRegisterMetaType的使用) QObject::connect: Cannot queue arguments of type…【已解决】

2023-07-23 · 1 min · 47 words · RamLife

QT QMetaObject 介绍

需求 QMetaObject ? 解决 QMetaObject 是 Qt 的元对象系统,里面保持了类的相关信息,主要的作用是为了在有指针的情况下,获取这个指针具体的类型信息,方便调试;另外也是 Qt 实现信号和槽的基础。毕竟 C++ 只有 dynamic_cast 用来试探子类型, typeid 只能获取类型名称,其他类型相关信息都不能获取。 由于C++是静态类型语言,有关类的信息只在编译期被使用,编译后就不再保留,因此程序运行时无法获取类的信息。这时就需要使用「运行期类型信息」,即 RTTI(Run-Time Type Information)。 参考 Qt中的元对象系统(Meta-Object System) Qt元对象系统:QMetaObject

2023-07-22 · 1 min · 24 words · RamLife

QT qintptr 介绍

需求 qintptr ? 解决 这个是 qt 为了跨平台用的,特别是有些情况,硬件平台可能是 32bit 或者 64bit。 qintptr 表示指针类型,占用空间和 int 一样,依赖于硬件实现, 32bit 平台那就是 32bit, 64bit 平台那就是 64bit。这样在用户代码层面便于统一。 qintptr 和 quintptr 都是指针,只不过一个是 有符号数,一个是无符号数。 qintptr 这个有符号的类型对于 hasing 等情况比较方便。 参考 <Qt help> -> <QtGlobal>::qintptr

2023-07-22 · 1 min · 34 words · RamLife