QToolButton按下时下沉效果怎么实现?

jinling4388 2011-07-19 01:32:14
如题,我用的是qt-everywhere-opensource-src-4.7.3,平台是Windows。
设计界面用的是Qt Designer,按钮内容是美工画的,采用背景图来实现的。不知道改变那个属性能实现按钮按下时的下沉效果?
多谢各位了!
...全文
1890 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ws00801526 2013-09-22
  • 打赏
  • 举报
回复
"QToolButton{border-image: url(ui_image/b3.jpeg);border-style: flat;}" "QToolButton:pressed{border-image: url(ui_image/IMG_0010.JPG);border-style: flat;}"
至善者善之敌 2011-07-20
  • 打赏
  • 举报
回复
其实我说的第一种方法最简单,可是你又不采纳
xxcc309 2011-07-20
  • 打赏
  • 举报
回复
我以前写的类似的东西,直接给你段代码作参考吧:

MyToolButton::MyToolButton(QWidget *parent, int iconsize, int btnWidth, int btnHeight, int textsize)
: QToolButton(parent)
{
setMouseTracking(true);
setStyleSheet("border-style:flat;");
setCheckable(true);

_bHover = false;
_bPress = false;
setArrowType(Qt::NoArrow);

_mouseHoverPixmap = cached(QString(":/MyQTInterface/Resources/mouse_hover.png"));
_mousePressPixmap = cached(QString(":/MyQTInterface/Resources/mouse_press.png"));
_normalPixmap = cached(QString(":/MyQTInterface/Resources/mouse_disable.png"));

_textDirection = None;

//计算绘制icon的rect和绘制文字的rect
if (textsize == 0)
{
//不绘制文字,把icon绘制在中间
_iconrect = QRect((btnWidth-iconsize)/ 2, (btnHeight - iconsize) / 2, iconsize, iconsize);
}
else
{
//绘制文字,需要考虑文字的高度
_textrect = QRect(0, btnHeight - textsize, btnWidth, textsize);
_iconrect = QRect((btnWidth-iconsize)/ 2,(btnHeight - textsize - iconsize) / 2,iconsize, iconsize);
}
setIconSize(QSize(iconsize,iconsize));
setFixedSize(btnWidth,btnHeight);
}

MyToolButton::~MyToolButton()
{

}

void MyToolButton::leaveEvent(QEvent *event)
{
_bHover = false;
_bPress = false;
update();
}

void MyToolButton::enterEvent(QEvent *event)
{
_bHover = true;
update();
}
void MyToolButton::mousePressEvent(QMouseEvent *event)
{
_bHover = false;
_bPress = true;
update();
QToolButton::mousePressEvent(event);
}
void MyToolButton::mouseReleaseEvent(QMouseEvent *event)
{
_bHover = true;
_bPress = false;
update();
QToolButton::mouseReleaseEvent(event);
}
bool MyToolButton::isHover()
{
return _bHover;
}

void MyToolButton::paintEvent(QPaintEvent *event)
{
QPixmap drawPixmap = cached("");

if (_bHover)
drawPixmap = _mouseHoverPixmap;

else if(_bPress)
drawPixmap = _mousePressPixmap;

else
drawPixmap = _normalPixmap;

//绘制背景
QPainter p(this);
p.drawPixmap(_iconrect, drawPixmap);

//绘制文字
if (_textDirection == Down )
{
p.setPen(Qt::white);
p.drawText(_textrect,Qt::AlignCenter, text());
}
}

void MyToolButton::setTextDirection(TextDirection textDirection )
{
_textDirection = textDirection;
}

void MyToolButton::setMouseHoverPixmap(QPixmap mouseHoverPixmap)
{
_mouseHoverPixmap = mouseHoverPixmap;
}

void MyToolButton::setMousePressPixmap( QPixmap mousePressPixmap )
{
_mousePressPixmap = mousePressPixmap;
}

void MyToolButton::setNormalPixmap(QPixmap normalPixmap)
{
_normalPixmap = normalPixmap;
}
jinling4388 2011-07-20
  • 打赏
  • 举报
回复
多谢,能给点示例吗?非常感谢!
至善者善之敌 2011-07-20
  • 打赏
  • 举报
回复
还有另外一种方法,自己按下按钮后对图片进行灰度之类的处理了
jinling4388 2011-07-20
  • 打赏
  • 举报
回复
难点没有办法直接实现吗?必须要在按下时切换另外一幅图吗?
jinling4388 2011-07-20
  • 打赏
  • 举报
回复
不是不采纳,我用Qt Designer设计的界面,你说的第一种方法我不知道在样式表里怎么设置?

我希望最好直接用Designer来实现,我用它画的界面在.h文件里没办法直接修改。自动生成的如下:
add_user->setStyleSheet(QString::fromUtf8("border-image: url(:/DesignerWindow/Resources/add_user.png);"));

我希望用这种方式实现按钮按下下沉的效果。

多谢楼上2位了!
至善者善之敌 2011-07-19
  • 打赏
  • 举报
回复
帮顶。。。。画两幅图,来回切换

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧