1.想给按钮设置图标,但setIcon设置的图标太小,用setStyleSheet设置background-image同样如此;
查询后发现border-image刚好满足要求,即
button->setStyleSheet("border-image:url(:/res/button.png);background:transparent;");
2.但后来发现,这样设置会使得按钮的toolTips背景同样被设为相应图片,这不是我想要的。无意在按钮函数自动补全时发现了setIconSize();
所以,正确的做法应该是:
button->setIcon(QIcon(":/res/button.png")); button->setStyleSheet("background:transparent;"); button->setIconSize(QSize(50,50)); button->setToolTip("Test");
3.另外,一些情况下可使用 QToolTip::showText()
bool HeightIndicator::event(QEvent *event) { if(event->type()==QEvent::ToolTip) { QHelpEvent *helpEvent=static_cast<QHelpEvent *>(event); if((helpEvent->x()>(centerPosX-100))&&(helpEvent->x()<(centerPosX+100))&& (helpEvent->y()>(centerPosY-100))&&(helpEvent->y()<(centerPosY+100))) { QToolTip::showText(helpEvent->globalPos(),QString("高度:%1 英尺").arg(currentHeight)); } } return QWidget::event(event); }
文章评论