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关闭时会自己释放自己吗?

2023-05-25 · 1 min · 60 words · 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 增加新窗口,并显示在最前

2023-05-25 · 1 min · 59 words · RamLife

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

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

2023-03-10 · 1 min · 20 words · 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); ....

2023-03-06 · 1 min · 105 words · 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模式

2023-03-06 · 1 min · 42 words · 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 结合使用的例子

2023-02-27 · 1 min · 40 words · RamLife

QT 在非多线程的情况下处理耗时事务

需求 在 qt 中既需要处理耗时任务,又不想使用多线程。 解决 可以在处理长时间耗时任务时,在耗时任务中周期性的调用 qApp.processEvents();, 这样可以让 qt 间断性的处理界面事务。比较推荐的是配合使用 QProgressDialog, 这样可以有进度条提示用户。代码如下: bool MyApp::writeFile(const QString &filename) { QFile file(filename); ... QApplication::setOverrideCursor(Qt::WaitCursor); QProgressDialog progress; progress.setWindowTitle(tableData->sNameCH); progress.setLabelText(QStringLiteral("数据保存中,请稍候...")); //progress.setCancelButton(0);//不显示“取消”按钮 progress.setCancelButtonText("取消"); progress.setRange(0,rowCount ); progress.setModal(true); //此处没有调用show()来显示,是因为QProgressDialog会自动决定是否显示 //如果时间过短,就不会显示。 for(int r = 0; r != rowCount; ++r) { progress.setValue(row); //如果用户单击了“取消”,就取消保存文件,并删除该文件。 if(progress.wasCanceled) { file.remov(); return false; } for(int c = 0; c != colCount; ++c) { out << table(r,c); qApp.processEvents(); } } QApplication::restoreOverrideCursor(); } 参考 Qt 如何处理密集型耗时的事情 QApplication::processEvents的作用

2023-02-20 · 1 min · 70 words · RamLife

QT C++ 识别当前操作系统平台,并编译相应代码

需求 需要在 Qt 中检测当前的操作系统平台具体是 win 还是 linux,然后根据不同的平台执行不同的代码。比如在 linux 平台可以使用 syslog 解决 #ifdef Q_OS_LINUX #include <syslog.h> #endif #ifdef Q_OS_WIN #endif #ifdef Q_OS_OSX #endif #ifdef Q_OS_LINUX openlog(NULL, LOG_CONS | LOG_NDELAY | LOG_NOWAIT | LOG_PID, LOG_LOCAL0); setlogmask(LOG_UPTO(LOG_MASK_BUILD)); #endif syslog(LOG_DEBUG, "%s: %d: %s --> class construct", __FILE__, __LINE__, __FUNCTION__); 参考 Qt判断当前系统 QT C++识别当前操作系统

2023-02-14 · 1 min · 49 words · RamLife