[qt5学习笔记]ApplicationExample示例程序源码解析
- 创业
- 2025-09-04 20:51:01
![[qt5学习笔记]ApplicationExample示例程序源码解析](/0pic/pp_62.jpg)
开发环境问题 vs2022下直接打开ui、ts文件失败
解决办法如下图, 设置designer独立运行。估计是嵌入运行存在些许bug。 同理,ts编辑工具linguist也存在这个问题。 qrc rc的编辑嵌入编辑都正常,但分离式更稳定可靠。
qt creator编译失败原因是qt creator自己设置的默认编译环境路径错误,修改c++编译器所在路径。
界面元素的两种创建方式 在qt designer中编辑ui文件我觉得,在qt designer中编辑ui文件,唯一优点就是比较直观。 缺点较多,设置起来比较麻烦。效率较低,代码方式可以拷贝粘贴。界面对象指针都存储在Ui::QtWidgetsApplication1Class ui_;中。
在MainWindow的构造函数中new ui元素这是qt自带Application Example示例代码的方式,此示例工程就没有ui文件。 之所以还保留qrc文件,我觉得是为了将所有png单文件合并为一个资源文件,显示时直接访问程序内部的资源图像内存块,避免了图像加载过程,使得界面显示更流畅。
示例代码中没有管理new出来的ui对象内存 例如:QMenu *fileMenu = menuBar()->addMenu(tr(“&File”)); 原因是这些界面对象指针都会被挂在qApp这个内存树上,app关闭时会自动释放。 如果其它函数中途需要用到这些ui对象,可以放在class MainWindow中集中存储。
qml…
connect 信号和槽connect 一般有4个参数,分为两组: 前面两个分别是信号发送者和信号类型, 后面两个是信号接收者和负责响应的槽函数。 例如:
connect(ui_.plainTextEdit->document(), &QTextDocument::contentsChanged, this, &MainWindow::documentWasModified); #ifndef QT_NO_SESSIONMANAGER //系统关闭时,会触发qApp的commitDataRequest信号 QGuiApplication::setFallbackSessionManagementEnabled(false); connect(qApp, &QGuiApplication::commitDataRequest, this, &MainWindow::commitData); #endifconnect原型如下,
//Connect a signal to a pointer to qobject member function template <typename Func1, typename Func2> static inline QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer<Func2>::Object *receiver, Func2 slot, Qt::ConnectionType type = Qt::AutoConnection) enum ConnectionType { AutoConnection, DirectConnection, QueuedConnection, BlockingQueuedConnection, UniqueConnection = 0x80 };AutoConnection:默认选择,适合大多数场景。
DirectConnection:直接调用,适合单线程。
QueuedConnection:跨线程调用,适合多线程。
BlockingQueuedConnection:跨线程同步调用,适合需要同步的场景。
UniqueConnection:确保连接唯一,避免重复连接。
QSettings保存运行信息在下一次启动时可以查询复用上一次的运行信息。
QCoreApplication::setOrganizationName("QtProject"); void MainWindow::writeSettings() { QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName()); settings.setValue("geometry", saveGeometry()); } void MainWindow::readSettings() { QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName()); const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray(); if (geometry.isEmpty()) { const QRect availableGeometry = screen()->availableGeometry(); resize(availableGeometry.width() / 3, availableGeometry.height() / 2); move((availableGeometry.width() - width()) / 2, (availableGeometry.height() - height()) / 2); } else { restoreGeometry(geometry); } }如果涉及多个显示屏(下一次启动也可能没有之前的显示器),情况会复杂一些。
QCommandLineParser 支持命令行ui程序支持命令行的话,可以像linux命令一样将一组ui程序串成脚本运行(此时代码需要控制跳过界面显示),需要时再启动ui显示。
QCommandLineParser parser; parser.setApplicationDescription(QCoreApplication::applicationName()); parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument("file", "The file to open."); parser.process(app); if (!parser.positionalArguments().isEmpty())//支持命令行打开文件 mainWin.loadFile(parser.positionalArguments().first());[qt5学习笔记]ApplicationExample示例程序源码解析由讯客互联创业栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“[qt5学习笔记]ApplicationExample示例程序源码解析”
上一篇
Dify平台搭建面试机器人
下一篇
插槽slot