样式设置的三种方式

样式表设置

ui.label->setStyleSheet("QToolTip{border:1px solid rgb(118, 118, 118); background-color: #ffffff; color:#484848; font-size:12px;}"); //设置边框, 边框色, 背景色, 字体色, 字号
ui.label->setToolTip("Hello, world!");

 调色板设置

//设置QToolTip颜色
QPalette palette = QToolTip::palette();
palette.setColor(QPalette::Inactive,QPalette::ToolTipBase,Qt::white);   //设置ToolTip背景色
palette.setColor(QPalette::Inactive,QPalette::ToolTipText,QColor(102, 102, 102, 255));    //设置ToolTip字体色
QToolTip::setPalette(palette);
QFont font("Segoe UI", -1, 50);
font.setPixelSize(12);
QToolTip::setFont(font);  //设置ToolTip字体

全局:qApp->setStyleSheet("QToolTip{border: 0px solid black;background:red;}");
控件:QLabel m_label; m_label->setStyleSheet("QToolTip{border: 0px solid black;background:red;}");

HTML方法

有时候使用第三方控件时,上述方法都无效,采取HTML方法:
例如,使用Qcustomplot时,Qcustomplot控件的样式表设置达不到设定的效果。(后来发现设置Qcustomplot控件的父控件样式可以实现效果)

QString st = "<b style=\"background:white;color:black;\">%1</b>";
QToolTip::showText(event->pos(),st.arg("5566"),this);

 
注:因为是自定义的QWidget派生类,需要对paintEvent添加一些代码,才能使得在Qt Designer里通过StyleSheet更改背景颜色(background-color)生效。

 

需要添加的代码如下:

QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

 

 

 

QToolTip换行显示

QString ToolTipWrap(QString strSrc,QFont font,int width) 
{ 
	int iCurrent = 0; 
	QString strToolTip = ""; 
	QString strTemp = "";  
	QFontMetrics fontMetrics(font);
	while(iCurrent < strSrc.size()) 
	{ 
		strTemp += strSrc.at(iCurrent); 
		if(fontMetrics.width(strTemp)>=width) 
		{ 
 			strToolTip +=strTemp + "\n";//或HTML标签<br/> 
 			strTemp.clear();
 		}   
		 ++iCurrent;
 	} 
 	return strToolTip;
}

 



 

当我们把鼠标放到QLabel,QPushButton等控件上面时,会出现提示语,这个提示语就是QToolTip;
想要出现提示语需要调用对应控件的setToolTip函数;
QToolTip的样式设置和QLabel保持一致;但QToolTip无法做到背景透明,也无法改变形状,如果设置背景为transparent透明时,默认会添加黑色背景;设置圆角半径时,改变的只是内部的圆角半径;
 
QToolTip{
//设置字体样式
font-family: "Microsoft YaHei";//字体类型
font-size: 25px;//字体大小,像素
font-style: italic;//字体斜体样式,mormal不斜体
font-weight:bold;//字体加粗样式,mormal不加粗
color: #bdc8e2//字体颜色
 
font: bold italic 18px "Microsoft YaHei";//顺序要求:style weight size family 或者 weight style  size family
//文字位置
padding-left: 10px;距离左边边界的距离
padding-top: 10px;距离顶边边界的距离
padding-right: 10px;距离右边边界的距离
padding-bottom: 10px;距离底边边界的距离
 
//边框样式
border-style: solid;//边框样式,实线:solid ;虚线:dashed; 点线:dotted;
不显示(默认):none;
border-width: 2px;
border-color: red;
 
border:2px,solid red;//同时设置
 
//某一条边框(其他三个边框: right,bottom,left)
border-top-style:solid;
border-top-width:2px;
border-top-color:red;
 
//圆角
border-top-left-radius:20px;//左上角弧度
border-top-right-radius:20px;//右上角弧度
border-bottom-left-radius:20px;//左下角弧度
border-bottom-right-radius:20px;//右下角弧度
 
bordet-radius:20px;//同时设置4个角的弧度
 
//背景样式
background-color:rgba(r,g,b,a);//值transparent为透明
background-image:url(".png");//背景图片
background-repeat:no-repeat;//在x轴重复:repeat-x; 在y轴重复:repeat-y
background-position:left center;//图片显示位置:left,right,center,top,bottom;
 
background: url(".png") no-repeat left center #2e3648;//顺序任意
}

 

 

 

posted on 2020-09-23 09:32  刘达人186  阅读(1745)  评论(0编辑  收藏  举报