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

stm32 cube ide 编译提示 undefined reference to

需求 编译的时候,提示 undefined reference to 解决 这种问题经常会碰到,一般就是相关源文件没有在编译列表里面,结果经过仔细查找,都添加进去了。后来经过仔细查找,发现原来是 c++ 和 c 混合编译的问题,在 c++ 中文件里面调用的函数,其所在的头文件没有使用 extern "C" 包含,导致的。想要修改也很简单,不需要改动头文件,只需要在 c++ 文件中这样: #include "gx_api.h" extern "C" { #include "gx_display.h" } 而且其实在 map 文件中是有相关线索的,下面分别是没有用 extern 包含,和用了 extern 包含的 map 文件中的函数信息。可以明显看到有 extern 包含的,有相关地址。 .text._gx_display_driver_565rgb_setup 0x0000000000000000 0x260 ./guix/common/src/gx_display_driver_565rgb_setup.o .text._gx_display_driver_565rgb_setup 0x000000000801e6e8 0x260 ./guix/common/src/gx_display_driver_565rgb_setup.o 0x000000000801e6e8 _gx_display_driver_565rgb_setup 参考 gcc “undefined reference to” 问题解决方法

2023-11-17 · 1 min · 53 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

c/c++ 解决打印缓冲

需求 c/cpp 解决打印缓冲 解决 c++ 直接使用 endl 即可立即输出。 c 以下几种都可以。 增加 \n setbuf(stdout, NULL) fflush(stdout) 参考 QT QProcess 使用及实时输出回显

2023-08-09 · 1 min · 19 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

c++ 实现类似 java 的 interface

需求 cpp 实现类似 java 的 interface 解决 interface 关键字 因为 interface 所有声明默认都是 public=,所以选择 =struct. #define interface struct; #define implements public 使用 #include "Interface.h" Interface IBar { public: virtual ~IBar(){} virtual int getBarData() const = 0; virtual void setBarData(int nData) = 0; }; class Bar :public BasicBar,implements IBar { public: Bar(int x=0) : BasicBar(x) { } ~Bar(){} virtual int getBarData() const { std::cout <<"Get Bar!"; return 0; } virtual void setBarData(int nData) { } }; class DataAccess { // Construction & Destruction public: DataAccess() { } ~DataAccess() { } static IBar* createBar() { //返回对象给IBar接口 return(new Bar()); } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //IBar *bar = new Bar(); IBar *bar = DataAccess::createBar(); bar->getBarData(); delete bar; return a....

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

c++ 工厂设计模式

需求 cpp 工厂模式 解决 https://zhuanlan.zhihu.com/p/83535678 https://zhuanlan.zhihu.com/p/83537599 参考

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

c++ impl 使用接口类

需求 cpp impl ? 解决 impl 使用接口类有两种方式: 接口类只是作为一个壳,需要什么都是直接调用原来的类来实现 接口类是使用虚函数,实现类通过继承来实现 代理类 代理类非常简单,下面这个 WeightProxy 就是壳,具体的实现都在 Weight 里面。 #ifndef WEIGHTPROXY_H #define WEIGHTPROXY_H #include <string> #include <memory> class Weight; class WeightProxy { public: WeightProxy(); ~WeightProxy(); std::string GetInfo(); std::unique_ptr<Weight> m_proxy; }; #endif // WEIGHTPROXY_H #include "Weightproxy.h" #include "Weight.h" WeightProxy::WeightProxy() : m_proxy(new Weight()) { } WeightProxy::~WeightProxy() = default; std::string WeightProxy::GetInfo() { return m_proxy->GetInfo(); } #include <iostream> // std::streambuf, std::cout #include "Weightproxy.h" int main () { WeightProxy w; std::cout << w....

2023-07-27 · 1 min · 207 words · RamLife