版权声明:欢迎大佬们指点 https://blog.csdn.net/bloke_come/article/details/82773828
QPalette类
1、专门用于管理对话框的外观显示,每个控件或窗口都包含一个QPalette对象
2、有两个基本概念:
1> ColorGroup
三种不同的状态:
- QPalette::Active 获得焦点的状态
- QPalette::Inactive 未获得焦点的状态
- QPalette::Disable 不可用状态
2>ColorRole
颜色主题
- QPalette::Window 背景色
- QPalette::WindowText 前景色
- QPalette::Button Button背景颜色。这个背景颜色能是不同于window作为一些风格,要求一个不同的背景颜色作为Button(Button色,尝试完没感觉)
- QPalette::ButtonText Button中字体的颜色
- QPalette::Base 最长使用来作为text背景颜色为整个widget,但是也能被用来为其他的绘画,像combobox的上下清单的背景和工具栏句柄。它通常是白的或者其他亮的颜色.
最重要的函数setColor
原型
void QPalette::setColor(ColorGroup group, ColorRole role, const QColor &color);
void QPalette::setColor(ColorRole role, const QColor &color);
QTime类
获得当前系统时间:currentTime()
将时间转换为字符串函数:toString()
将字符串转换为时间函数:fromString()
解释:使用参数format指定的格式根据参数string指定的时间返回一个时间对象。如果string指定的时间不合法,则返回一个无效的时间对象。
参数format用来指定时,分,秒,毫秒的输出格式.如(hh:mm:ss.zzz)
h:表示小时,范围是0--23
hh:用两位数表示小时,不足两位的前面用0补足,如(0点:00,3点:03,11点:11)
m:表示分钟,范围0--59
mm:用两位数表示分钟,不足两位的前面用0补足.
s:表示秒,范围0--59
ss:用两位数表示秒,不足两位的前面用0补足.
z:表示毫秒,范围0--999
zzz:用三位数表示毫秒,不足三位的前面用0补足.
AP:用AM/PM显示.
ap:用ap/pm显示.
Qt::DateFormat作为参数指定时间显示的格式
参数的可选值:
Qt::TextDate:格式为HH:MM:SS
Qt::ISODate:遵循ISO8601的时间表示格式,同样也为HH:MM:SS
Qt::LocalDate:字符串格式依赖系统本地设置
电子时钟实例:
DigiClock::DigiClock(QWidget *parent)
{
QPalette p=palette();
p.setColor(QPalette::Window,Qt::blue);
setPalette(p);setPalette(p);
setWindowFlags(Qt::FramelessWindowHint);
setWindowOpacity(0.5); // 透明度
/* 设置时钟背景 */
m_pLCD = new QLCDNumber(this);
// 设置能显示的位数
m_pLCD->setDigitCount(12);
// 设置显示的模式为十进制
m_pLCD->setMode(QLCDNumber::Dec);
// 设置显示外观
//m_pLCD->setSegmentStyle(QLCDNumber::Flat);
// 设置样式
//m_pLCD->setStyleSheet("border: 1px solid green; color: green; background: silver;");
QTimer *timer=new QTimer(this); // 新建一个定时器对象
connect(timer,SIGNAL(timeout()),this,SLOT(showTime())); // 信号与槽函数
timer->start(1);
showTime(); // 初始时间显示
resize(125,23); // 设置电子时钟显示的尺寸
showColon=true; // 初始化
}
void DigiClock::showTime()
{
QTime time=QTime::currentTime(); // 获取系统时间
QString text=time.toString("hh:mm:ss.zzz"); // 时间转换成时分秒毫秒
if(showColon)
{
text[2]=':';
text[5]=':';
text[8]='.';
showColon=false;
}
else
{
text[2]=' ';
text[5]=' ';
text[8]=' ';
showColon=true;
}
m_pLCD->display(text); // 显示转换好的字符串时间
}