QT QToolTip


樣式設置的三種方式

樣式表設置

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;//順序任意
}

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM