当前位置: 首页 > news >正文

Qt 常用函数

  1. 设置编码
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#if _MSC_VERQTextCodec *codec = QTextCodec::codecForName("gbk");
#elseQTextCodec *codec = QTextCodec::codecForName("utf-8");
#endifQTextCodec::setCodecForLocale(codec);QTextCodec::setCodecForCStrings(codec);QTextCodec::setCodecForTr(codec);
#elseQTextCodec *codec = QTextCodec::codecForName("utf-8");QTextCodec::setCodecForLocale(codec);
#endif
  1. IP地址匹配,正则表达式
QRegExp rxp("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
if(!rxp.exactMatch(ip))
{QMessageBox::warning(nullptr, QString::fromLocal8Bit("错误"),QString::fromLocal8Bit("ip地址错误!"));return;
}
  1. 计算用时
QDateTime dtStart = QDateTime::currentDateTime();
QDateTime dtEnd = QDateTime::currentDateTime();
double msec = dtStart.msecsTo(dtEnd);
  1. 数据类型转换
QPushButton *btn = reinterpret_cast<QPushButton *>(sender());dynamic_cast:   通常在基类和派生类之间转换时使用,run-time   cast
const_cast:   主要针对constvolatile的转换. 
static_cast:   一般的转换,no   run-time   check.通常,如果你不知道该用哪个,就用这个。   
reinterpret_cast:   用于进行没有任何关联之间的转换,比如一个字符指针转换为一个整形数。
  • 查找控件
//查找指定类名objectName的控件
QList<QWidget *> widgets = fatherWidget.findChildren<QWidget *>("widgetname");
//查找所有QPushButton
QList<QPushButton *> allPButtons = fatherWidget.findChildren<QPushButton *>();
//查找一级子控件,不然会一直遍历所有子控件
QList<QPushButton *> childButtons = fatherWidget.findChildren<QPushButton *>(QString(), Qt::FindDirectChildrenOnly);
  • 添加 FontAwesome 字体
    //判断图形字体是否存在,不存在则加入QFontDatabase fontDb;if (!fontDb.families().contains("FontAwesome")) {int fontId = fontDb.addApplicationFont(":/image/fontawesome-webfont.ttf");QStringList fontName = fontDb.applicationFontFamilies(fontId);if (fontName.count() == 0) {qDebug() << "load fontawesome-webfont.ttf error";}}if (fontDb.families().contains("FontAwesome")) {iconFont = QFont("FontAwesome");
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))iconFont.setHintingPreference(QFont::PreferNoHinting);
#endif}
btnPrevMonth->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); //设置策略QtConcurrent::run(this,&DataIoPanel::sysDataInput,fileName);//启动线程
http://www.lryc.cn/news/161056.html

相关文章:

  • UMA 2 - Unity Multipurpose Avatar☀️六.Advanced Occlusion高级遮挡功能解决皮肤服饰穿模
  • 深度解析自然语言处理之篇章分析
  • Python3.11教程3:模块和包(pip/conda)、文件系统(os/ shutil/json/pickle/openpyxl/xlrd)
  • shell 脚本工具(三剑客)
  • 基于微信小程序的智能垃圾分类回收系统,附源码、教程
  • 【C++进阶】:AVL树(平衡因子)
  • Python教程33:关于在使用zipfile模块,出现中文乱码的解决办法
  • 【疑难杂症】使用xshell连接云服务器连接不上
  • Qt MinGW / MSVC
  • 【数学建模】数据预处理
  • VMware 安装 黑群晖7.1.1-42962 DS918+
  • OpenCV(二十九):图像腐蚀
  • 【网络知识点】三次握手和四次挥手
  • CSS整理
  • OpenCV 06(图像的基本变换)
  • Java 中的日期时间总结
  • 创建10个线程并发执行(STL/Windows/Linux)
  • 三、创建各个展示模块组件
  • 推荐一款程序员截图神器!
  • 无涯教程-JavaScript - IMCSC函数
  • Ubuntu22.04 LTS 显卡相关命令
  • 《TCP/IP网络编程》阅读笔记--基于 TCP 的半关闭
  • Rust的模块化
  • vmware设置桥接模式后ip设置
  • 算法通关村第十七关:白银挑战-贪心高频问题
  • 目标检测评估指标mAP:从Precision,Recall,到AP50-95
  • 七大排序算法
  • GitHub two-factor authentication
  • un-app-手机号授权登录-授权框弹不出情况
  • 手写Spring:第14章-自动扫描Bean对象注册