c/c++ 解决打印缓冲

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

<span title='2023-08-09 11:27:00 +0800 CST'>2023-08-09</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;19 words&nbsp;·&nbsp;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 之启动外部程序

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

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

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

<span title='2023-07-28 18:35:00 +0800 CST'>2023-07-28</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;11 words&nbsp;·&nbsp;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....

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

c++ 工厂设计模式

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

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

<span title='2023-07-27 18:07:00 +0800 CST'>2023-07-27</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;207 words&nbsp;·&nbsp;RamLife

c++ impl 使用指针实现即 pimpl

需求 cpp impl 是什么? 解决 impl 解决的问题 依赖太多,包含头文件过多,一个头文件修改,一堆源文件都需要重新编译,导致的编译时间过长。 库文件,不喜欢在头文件中暴露太多的内部成员变量等信息给使用着。 简单 pimpl // 使用Pimpl // 在头文件person.hpp中 #include <memory> class Person { public: Person(); private: // Person类的实现细节放置在该前向声明的实现类中。 struct Impl; // 指向实现类Impl的私有指针 std::unique_ptr<Impl> pimpl_; }; // 在源文件person.cpp中 #include "person.hpp" #include "basic_info.hpp" #include <string> #include <memory> struct Person::Impl { std::string name; std::string id; BasicInfo basic_info; }; Person::Person() : pimpl_(std::make_unique<Impl>()) {} 具体的成员变量放到 cpp 文件中,编译为库后,有以下好处: 就算需要修改包含的成员变量,也不需要修改头文件,那么编译的时候,只会编译这个 cpp 文件,其他依赖这个头文件的都不需要重新编译。 使用者并不能从头文件中发现成员变量的细节。 pimpl 更多细节 pimpl 在使用的时候,还会碰到很多需要注意的地方: 析构函数的实现需要完整的类型 移动赋值需要完整类型 移动构造需要完整类型 拷贝构造和拷贝赋值都需要完整类型 所以上面这些都需要放在 cpp 中实现。...

<span title='2023-07-27 17:45:00 +0800 CST'>2023-07-27</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;158 words&nbsp;·&nbsp;RamLife

gitea release 时上传的附件大小限制

需求 gitea release 时,上传的二进制文件大小提示受限. gitea exceeds the maximum size of (4 MB). 解决 修改 gitea 配置文件,在 gitea/conf/app.ini 里面。在 [attachment] 这一项下面增加 MAX_SIZE 和 MAX_FILES. [attachment] PATH = /data/gitea/attachments MAX_SIZE = 128 MAX_FILES = 16 注意, [repository.upload] 下面也有 FILE_MAX_SIZE 和 MAX_FILES, 但是这两个设置之后,并不能解决问题。只有 [attachment] 下面的才是有效的。 参考 Configuration Cheat Sheet 1.19.4 Configuration Cheat Sheet 1.21-dev Increase repository release upload max files limit gitea上传大小限制

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

gitea release 介绍

需求 gitea 如何 release 解决 release 之前,首先需要打 tag. 有了 tag 之后,才能在 gitea 上面如下操作: tag 页面,找到具体的 tag,然后点击对应的 release 跳转到 release 页面,填写相应的标题和内容,把二进制文件等其他的的附件拖进去。 勾选 use the title and content of release as tag message, 这个表示在 release 页面,具体的某个 release 使用你填写的标题和内容。 如果二进制文件后期还需要调整等情况,可以勾选 mark as pre-release, 这个是预发布。 保存为草稿或者直接发布。 参考 怎样在github上发布pre-release和release?(我跟着这个操作成功了) git tag 以及发布 release git中tag与release的创建以及两者的区别

<span title='2023-07-24 18:29:00 +0800 CST'>2023-07-24</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;49 words&nbsp;·&nbsp;RamLife

git tag 介绍

需求 git 如何 打 tag 解决 查看最新提交 git log -1 --pretty=format:"%H" 轻量 tag lightweight tag 仅仅只是一个引用,不会存储额外信息。格式 git tag {标签名} {提交ID}, 例如: git tag v0.1.0 e2356d 打上类似这样的轻量标签即可。如果是给最新的提交打 tag, 那么只需要 git tag v0.1.0 这样即可。 注释 tag annotated tag 是一个完整对象,有标签名,标签信息,标签签名等。格式 git tag -a {标签名} -m "{标签信息}" {提交ID}, 例如: git tag -a v1.0.0 -m "Release version 1.0.0" HEAD. 推送 tag git push origin {标签名} 这样可以推送指定标签。 git push origin --tags 把所有 tag 推送到服务器。 删除 tag 删除本地 git tag -d {标签名} 删除远程 git push origin :refs/tags/{标签名} 参考 Git优雅使用:git tag操作...

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