同时显示图片和文字,重绘QPushButton,让button大小适应图片大小
2016-04-12 12:16
1006 查看
MPushButton::MPushButton(const QString& iconPath, const QString& text, QWidget* parent)
: QPushButton(parent)
{
m_text = text;
m_icon = QPixmap(iconPath);
setFixedHeight(m_icon.size().height() + 15);
setMinimumWidth(m_icon.size().width());
m_drawedIconRect.setX((rect().x() - m_icon.rect().x()) / 2);
m_drawedIconRect.setY(rect().y());
m_drawedIconRect.setWidth(m_icon.rect().width());
m_drawedIconRect.setHeight(m_icon.rect().height());
}
void MPushButton::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
painter.drawPixmap(m_drawedIconRect, m_icon);
painter.drawText(e->rect(), Qt::AlignHCenter | Qt::AlignBottom, m_text);
}
: QPushButton(parent)
{
m_text = text;
m_icon = QPixmap(iconPath);
setFixedHeight(m_icon.size().height() + 15);
setMinimumWidth(m_icon.size().width());
m_drawedIconRect.setX((rect().x() - m_icon.rect().x()) / 2);
m_drawedIconRect.setY(rect().y());
m_drawedIconRect.setWidth(m_icon.rect().width());
m_drawedIconRect.setHeight(m_icon.rect().height());
}
void MPushButton::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
painter.drawPixmap(m_drawedIconRect, m_icon);
painter.drawText(e->rect(), Qt::AlignHCenter | Qt::AlignBottom, m_text);
}
相关文章推荐
- 工具相关——用Eclipse的SVN插件管理一般目录
- LeetCode(49)-Valid Parentheses
- LeetCode(49)-Valid Parentheses
- LeetCode 215. Kth Largest Element in an Array
- fast-IO
- Qt实现自定义按钮的三态效果
- jsp九大内置对象
- 桂林山水甲天下
- Android代码优化小技巧总结
- 100层楼摔鸡蛋问题
- Qt4.8.2 QPushButton按钮贴图以及实现按钮的几种常用状态
- 使用U盘安装14.04 服务器版本时 提示无法挂载CD-ROM
- canvas 动画制作——时钟
- Leetcode no. 141
- Android蓝牙开发(二)
- Silverlight OOB 程序自动更新
- 谁会是 Zabbix 和 Nagios 的继任者?
- 设备未就绪异常
- 关于一道题目解法
- C++ 和Matlab混合编程一些问题:如何让matlab不打开新的command windows窗口