QT 打印当前线程

需求 qt 打印当前线程进程 解决 当前线程指针: QThread::currentThread() 当前线程: QThread::currentThreadId() 当前进程: QCoreApplication::applicationPid() 参考 QT打印当前线程地址 【Qt线程-6】获取当前线程id,thread()和currentThreadId(),不是想当然那样,不使用信号槽可能看不出区别

<span title='2023-11-06 10:18:00 +0800 CST'>2023-11-06</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;13 words&nbsp;·&nbsp;RamLife

QT 减少控件间的间隙

需求 qt 默认情况下,控件之间的间隙比较大,不好看,需要减少间隙 解决 这个其实很简单,直接调用 api 即可。 layout_->setContentsMargins(0,0,0,0); layout_->setSpacing(0); 参考 【QT】Layout布局间消除间隙(修改layout内置参数) QT 布局管理器设置控件固定大小,控件间隔 QT布局管理器不同部分比例大小设置方法

<span title='2023-11-05 11:43:00 +0800 CST'>2023-11-05</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;14 words&nbsp;·&nbsp;RamLife

QT 自定义分页

需求 qt 自定义分页控件 解决 具体参考: QT 自定义分页控件 第十四课:采用 Qt 开发翻页/分页/多页窗体组件 参考 QT 自定义分页控件 第十四课:采用 Qt 开发翻页/分页/多页窗体组件

<span title='2023-11-05 10:43:00 +0800 CST'>2023-11-05</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;16 words&nbsp;·&nbsp;RamLife

QT QString 设置位数

需求 qt 如何设置 QString 中数值的小数位数 解决 位数,进制,补位 QString QString::arg(uint a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const int num = 3; QString str = QString("%1") .arg(num, 4, 10, QChar('0')); // str == "0003" qDebug() << str; 小数位数 QString QString::number(double n, char format = 'g', int precision = 6) QString str = QString::number(32, 'f', 2); // str == "32.00" qDebug() << str; 参考 Qt QString中arg的使用,以及保留小数位数 QString设置小数点精度位数 Qt string 保留小数点后固定位数

<span title='2023-11-05 10:18:00 +0800 CST'>2023-11-05</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;77 words&nbsp;·&nbsp;RamLife

QT log 输出到文件

需求 qt 如何把 log 输出到文件 解决 log 输出功能 log_qt.h #ifndef LOG_QT_H #define LOG_QT_H #include <QString> void LogOutputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg); #endif // LOG_QT_H log_qt.cpp #include "log/log_qt.h" #include <QString> #include <QMutex> #include <QDateTime> #include <QFile> void LogOutputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg) { // 加锁 static QMutex mutex; mutex.lock(); QString tag; switch(type) { case QtDebugMsg: tag = QString("Debug:"); break; case QtWarningMsg: tag = QString("Warning:"); break; case QtCriticalMsg: tag = QString("Critical:"); break; case QtFatalMsg: tag = QString("Fatal:"); break; default:break; } // 设置输出信息格式 // QString context_info = QString("File:(%1) Line:(%2)")....

<span title='2023-11-04 10:18:00 +0800 CST'>2023-11-04</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;248 words&nbsp;·&nbsp;RamLife

c++ find, find_if, find_if_not

需求 cpp 了解 find 相关函数 解决 find 的语法格式 InputIterator find (InputIterator first, InputIterator last, const T& val); //find() 函数作用于普通数组 char stl[] ="http://c.biancheng.net/stl/"; //调用 find() 查找第一个字符 'c' char * p = find(stl, stl + strlen(stl), 'c'); //判断是否查找成功 if (p != stl + strlen(stl)) { cout << p << endl; } //find() 函数作用于容器 std::vector<int> myvector{ 10,20,30,40,50 }; std::vector<int>::iterator it; it = find(myvector.begin(), myvector.end(), 30); if (it != myvector.end()) cout << "查找成功:" << *it; else cout << "查找失败"; find_if 可以指定查找规则。语法: InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred); //自定义一元谓词函数 bool mycomp(int i) { return ((i % 2) == 1); } //以函数对象的形式定义一个 find_if() 函数的查找规则 class mycomp2 { public: bool operator()(const int& i) { return ((i % 2) == 1); } }; int main() { vector<int> myvector{ 4,2,3,1,5 }; //调用 find_if() 函数,并以 IsOdd() 一元谓词函数作为查找规则 vector<int>::iterator it = find_if(myvector....

<span title='2023-10-31 15:30:00 +0800 CST'>2023-10-31</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;198 words&nbsp;·&nbsp;RamLife

c++ 重载运算符 <

需求 cpp 需要对 array 中的对象进行排列,使用 sort 的情况下,默认需要重载 <, 才能实现相关功能。 解决 选择的是,在结构体内部进行重载。 struct PatientDataHead { // increment id int32_t id; bool operator <(const PatientDataHead& head) const {return id < head.id;} }; std::array<PatientDataHead, kPatientNum> patient_data_heads_ {}; std::sort(patient_data_heads_.begin(), patient_data_heads_.end()); 参考 C++中自定义比较函数和重载运算符总结

<span title='2023-10-31 11:27:00 +0800 CST'>2023-10-31</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;38 words&nbsp;·&nbsp;RamLife

c++ 容器的 insert, emplace, erase, clear

需求 cpp 对容器的插入和删除操作 解决 insert & emplace insert 插入时,会先调用构造函数,再调用移动构造函数。 emplace 插入时,只调用构造函数。 emplace 直接在容器的指定位置调用构造函数,省略了移动构造函数,效率会更高一些,推荐使用 emplace. erase erase 一般是需要先获取迭代器,然后再删除。 vector<int>::iterator it; it = myvector.begin(); myvector.erase(it); // finding the position of the element in the vector int valueToBeDeleted = 3; auto it = find(vector.begin(), vector.end(), valueToBeDeleted); if (it != vector.end()) { vector.erase(it); } vector<int>::iterator it1, it2; it1 = myvector.begin(); it2 = myvector.end(); it2--; it2--; myvector.erase(it1, it2); clear 直接清空所有元素 参考 C++ STL vector插入元素(insert()和emplace())详解 vector erase() and clear() in C++

<span title='2023-10-29 11:27:00 +0800 CST'>2023-10-29</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;75 words&nbsp;·&nbsp;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....

<span title='2023-10-09 15:42:00 +0800 CST'>2023-10-09</span>&nbsp;·&nbsp;4 min&nbsp;·&nbsp;775 words&nbsp;·&nbsp;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....

<span title='2023-10-07 10:42:00 +0800 CST'>2023-10-07</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;497 words&nbsp;·&nbsp;RamLife