QT学 控件(四)步长调节框(QSpinBox + QDateTimeEdit)
QSpinBox
允许用户通过单击向上/向下按钮或按键盘上的上/下来选择一个值来增加/减少当前显示的值。用户还可以手动键入值
常用于处理 选择 : 【1-99】 【星期1一 - 星期天】【1月-12月】
样式:
继承自:
QAbstractSpinBox
设置数据范围:
spin->setMaximum(7); spin->setMinimum(1); //spin->setRange();
设置数据循环:即超过了最大值或者低于最小值则循环到另一边
spin->setWrapping(true);
设置步长:
//设置步长 spin->setSingleStep(5);
设置前缀与后缀
spin->setPrefix("周"); spin->setSuffix("天");
效果:
设置特殊文本:当最小值得时候
//最小值特殊文本 spin->setSpecialValueText("到底了");
设置数字数据得显示格式: 几进制??
//指定显示格式 spin->setDisplayIntegerBase(16); //十六进制
扩展: 用于浮点型小数的调节 : QDoubleSpinBox
如何自定义文本呢?
1:星期一。2:星期二 。。。。 7:星期天
答案: 重写虚函数:
[virtual protected] QString QSpinBox::textFromValue(int value) const
textFromValue:接受一个值,可以转换为你想要表示的相对应的QString,例如:星期的转换:
SpinBox重写QSpinBox的textFromValue的方法,注意下面创建对象的时候需要使用我们继承的这个新类:SpinBox,textFromValue接受值,然后在列表中寻找对应的值,返回转换后的QStirng
class SpinBox:public QSpinBox { public: SpinBox(QWidget* parent=nullptr) :QSpinBox(parent) { strs<<"一"<<"二"<<"三"<<"四"<<"五"<<"六"<<"日"; } QString textFromValue(int value)const override { //转换文本: 天数转换为大写 return strs[(value-1)%7]; } private: QStringList strs; //存储转换的文本 };
实例:
QDateTimeEdit
作用:
- 编辑日期和时间的单行文本框
- 既可以用箭头调节, 也可以用键盘编辑输入
- 可以单独调节某个部分
样例:
我们可以自己输入信息,当然也可以进行键盘的调节。
继承自:QAbstractSpinBox
构造函数:
可以只创建Date:日期,也可以之创建Time:时间,也可以DateTime都创建,像上图一样。
QDateTimeEdit(parent: QWidget = None) QDateTimeEdit(Union[QDateTime, datetime.datetime], parent: QWidget = None) QDateTimeEdit(Union[QDate, datetime.date], parent: QWidget = None) QDateTimeEdit(Union[QTime, datetime.time], parent: QWidget = None)
设置显示格式:
//设置显示格式 date->setDisplayFormat("yyyy年MM月dd日 hh::mm::ss");
Section控制
//Section控制 //选择控制 《天》 date->setCurrentSection(QDateTimeEdit::Section::DaySection); //打印出当前控制的块的数量,当前的块的信息,文本等 qInfo()<<date->sectionCount()<<date->currentSection()<<date->sectionText(QDateTimeEdit::Section::YearSection); //API sectionCount() -> int 获取section个数 setCurrentSectionIndex(int) 设置当前的section索引 currentSectionIndex() -> int 获取section索引 setCurrentSection(QDateTimeEdit.Section) 设置当前操作的日期时间section currentSection() -> QDateTimeEdit.Section 获取当前的section部分 sectionAt(index_int) -> QDateTimeEdit.Section 获取指定索引位置的section sectionText(QDateTimeEdit.Section) -> str 获取指定section的文本内容
设置最小和最大的日期时间等
setMaximumDateTime(QDateTime) 默认 9999年12月31日 23:59:59 999毫秒 maximumDateTime() -> QDateTime clearMaximumDateTime() setMinimumDateTime(QDateTime) minimumDateTime() -> QDateTime clearMinimumDateTime()
显示日历:
//显示日历 date->setCalendarPopup(true);
信号:
时间日期等改变时触发:
dateTimeChanged(QDateTime datetime) dateChanged(QDate date) timeChanged(QTime time)
相关子类:
QDateEdit
QTimeEdit
QDateTime
QDate
QTime
本文来自博客园,作者:hugeYlh,转载请注明原文链接:https://www.cnblogs.com/helloylh/p/17209631.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?