QT sha1

需求 最近需要在 qt 中使用 sha1 加密。 解决 Qt 中已经提供了解决方法,就是 QCryptographicHash, 可以直接调用他的 hash 静态方法,只需要注明是 QCryptographicHash::Sha1 即可。 void MainWindow::on_pushButton_clicked() { QString id = ui->lineEdit->text(); QByteArray sha1 = QCryptographicHash::hash(id.toLocal8Bit(), QCryptographicHash::Sha1); string str = ptz::ByteArray2HexString((uint8_t*)sha1.data(), sha1.size()); QString pwd = QString::fromStdString(str); qDebug() << "byte array: " << sha1 << ", string: " << pwd << Qt::endl; ui->textBrowser->setText(pwd); } 参考 QT MD4 MD5 Sha1等几种加密方式 QCryptographicHash Class Secure Hash Algorithm SHA-1

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

QT 版本号

需求 最近需要在 qt 中获取版本号,根据版本号的不同,执行不同的语句。 解决 可以使用 QT_VERSION, QT_VERSION_STR, qVersion() 之类的。 #if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) ... #endif 参考 Qt程序中获取Qt的版本号信息 Qt之判断版本号宏「QT_VERSION」和「QT_VERSION_CHECK」

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

QT mvc mvp mvvm

需求 最新想了解下 qt 中使用 mvc。 解决 这个开源的6千行UI框架,能打败QT,MFC吗? - 小林通的回答 - 知乎 https://www.zhihu.com/question/66934513/answer/2809289366 四十六、QT应用开发之MVC架构(附案例) 【QT学习】实现MVC框架的简易封装(一文读懂) Model/View Programming Qt-MVC-MVVM Qt-MVC-MVVM / Qt_MVVM_Game 四十六、QT应用开发之MVC架构(附案例) 正确认识 MVC/MVP/MVVM MVVM / MVP and QML qt-mvvm代码分析 【QT学习】实现MVC框架的简易封装(一文读懂) 【QT学习】如何高效管理QT中的工程文件?(基于MVC框架的工程为例) Qt模型/视图框架(一) 参考

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

QT 单元测试

需求 最近学习了 android 上面的单元测试之后,也考虑在 Qt 上进行单元测试。 解决 主要是 Qtest, 先汇总一些资料,等有时间搞几个测试 demo,再慢慢引入. https://zhuanlan.zhihu.com/p/39376945 https://blog.csdn.net/yizhou2010/article/details/78272505 https://zhuanlan.zhihu.com/p/412497880 https://www.cnblogs.com/im18620660608/p/17157968.html https://blog.csdn.net/u011942101/article/details/124074075 https://blog.csdn.net/ipfpm/article/details/109852908 https://www.cnblogs.com/lvdongjie/p/10599650.html https://blog.csdn.net/yang1fei2/article/details/125121777 https://zhuanlan.zhihu.com/p/40901748 http://www.cleartechfei.com/2022/06/qt%e9%a1%b9%e7%9b%ae%e6%90%ad%e5%bb%ba%e5%ae%8c%e6%95%b4%e7%9a%84%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e6%a1%86%e6%9e%b6/ 参考

<span title='2023-06-15 14:36:00 +0800 CST'>2023-06-15</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;22 words&nbsp;·&nbsp;RamLife

QT dialog 关闭时释放资源

需求 需要在 QDialog 关闭的时候,自动释放掉资源。 解决 默认情况下, QDialog 占用的内存会在 MainWindow 关闭后释放,如果想要在 QDialog 本身关闭的时候就释放,需要通过 setAttribute 方法配置 Qt::WA_DeleteOnClose 选项. CurrentGetConfigForm * current_get_config_form = nullptr; void MainWindow::on_pushButton_current_get_config_clicked() { if (!GetConfig()) return; if (current_get_config_form == nullptr) current_get_config_form = new CurrentGetConfigForm(nullptr, port_, version_, address_); current_get_config_form->setAttribute(Qt::WA_ShowModal, true); current_get_config_form->setAttribute(Qt::WA_DeleteOnClose, true); current_get_config_form->show(); //QEventLoop loop; //loop.exec(); qDebug() << "MainWindow::on_pushButton_current_get_config_clicked() end" << Qt::endl; current_get_config_form = nullptr; } 参考 Qt Dialog 内存管理问题:Dialog关闭时会自己释放自己吗?

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

QT widget 以阻塞的模态形式打开

需求 需要把 widget 以模态形式打开,组织用户操作前一个页面。 解决 QWidget 没有 exec 这个方法,只能转化为 QDialog 之后,再使用 exec 方法。 CurrentGetConfigForm * current_get_config_form = nullptr; void MainWindow::on_pushButton_current_get_config_clicked() { if (!GetConfig()) return; if (current_get_config_form == nullptr) current_get_config_form = new CurrentGetConfigForm(nullptr, port_, version_, address_); current_get_config_form->setAttribute(Qt::WA_ShowModal, true); current_get_config_form->setAttribute(Qt::WA_DeleteOnClose, true); current_get_config_form->show(); //QEventLoop loop; //loop.exec(); qDebug() << "MainWindow::on_pushButton_current_get_config_clicked() end" << Qt::endl; current_get_config_form = nullptr; } 参考 QWidget如何exec QT 创建新窗口并且实现页面跳转 qt 增加新窗口,并显示在最前

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

QT Text 相关 widget 追加文本不换行

需求 正常在 QTextEdit QTextBrowser 中使用 append 添加新内容,都是自动换行的,如果不希望自动换行怎么办? 解决 其实很简单,使用 insertPlainText 这个方法即可。 textBrowser->insertPlainText("hello "); textBrowser->insertPlainText("world"); insertPlainText接口是在当前光标插入文本(光标一般默认在末尾),不自动换行,所以会打印: hello world 参考 QTextEdit QTextBrowser追加文本不换行

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

QT 警告: QObject: Cannot create children for a parent that is in a different thread

需求 在运行 qt 程序时,出了相应的警告: QObject: Cannot create children for a parent that is in a different thread. (Parent is QThread(0xb95feffd70), parent's thread is QThread(0x1d3729aef20), current thread is QThread(0xb95feffd70) 解决 这个其实就是在子线程中,使用了主线程的对象,并创建子对象,所以出的警告。解决的方法也有几种: 子线程创建子对象 简单说,就是在子线程中先获取主线程的相应参数,然后创建出需要的对象,这样的话,在需要创建子对象的时候,也是在同一个线程。这种方法最简单,就是代码上可能啰嗦一点。 不指定父对象 对象创建时,不指定父对象,也就是不使用 this 来指定,留空即可。如果碰到一些调用的库函数内部创建对象,这种方法就不好使了。 使用 moveToThread 绑定相应的线程 调用 QObject 的成员函数 moveToThread, 绑定到对应的线程上去。下面是几个例子: ThreadTest2 thread2; thread2.moveToThread(&thread2); thread2.start(); 上面这个例子,thread2 把自己从主线程绑定到子线程,这样在 ThreadTest2 这个类内部创建的对象也就转移到子线程上去了。 class Controller : public QObject { Q_OBJECT ... private: QThread thread; }; Controller::Controller(QObject* parent) : QObject(parent) { Worker *worker = new Worker(); worker->moveToThread(&thread); ....

<span title='2023-03-06 16:00:00 +0800 CST'>2023-03-06</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;105 words&nbsp;·&nbsp;RamLife

[转] C++ 或 QT 判断当前模式是Debug还是Release模式

需求 在构建版本的时候,需要在 debug 和 release 版本中有不一样的地方,这时候就需要使用宏来自动识别并展开对应的语句。 解决 C++ #ifdef DEBUG cout << "Debug!" << endl; #else cout << "Release!" << endl; #endif Qt #ifdef QT_DEBUG cout << "Debug!" << endl; #else cout << "Release!" << endl; #endif 参考 C++ 或 QT 判断当前模式是Debug还是Release模式

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

QMutex 和 QWaitCondition 配合用于多线程

需求 多线程同步时,需要线程能够被外部唤醒,从而按照一定的顺序来执行。 解决 同步可以使用 QWaitCondition 先让线程睡眠,然后在必要的时候从外面唤醒 线程即可。但是 QWaitCondition 所在的线程必须先使用 QMutex 上锁才行, QWaitCondition 会先阻塞线程,然后把锁释放,再等待唤醒,唤醒可以对 QWaitCondition 的对象使用 wakeAll() 或者 wakeOne() , 唤醒后,锁会自动回来,所以别忘了最后的解锁. QMutex 除了直接的 lock, 还可以使用 QMutexLocker 自动上锁,并在生命周期结束后自动解锁。 m_mutex.lock(); m_cond.wait(&m_mutex); ... m_mutex.unlock(); const QMutexLocker locker(&m_mutex); ... m_cond.wakeOne(); 参考 Qt互斥锁(QMutex)、条件变量(QWaitCondition)理解+QMutex实现多线程循环输出ABC(含源码+注释) QT线程QMutex和 QWaitCondition 结合使用的例子

<span title='2023-02-27 19:13:00 +0800 CST'>2023-02-27</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;40 words&nbsp;·&nbsp;RamLife