• 4538阅读
  • 1回复

[提问]子类化的QToolButton自绘制背景图怎么会影响到QDialog呢? [复制链接]

上一主题 下一主题
离线searchcai
 

只看楼主 倒序阅读 楼主  发表于: 2013-05-13
子类化QToolButton,参考的是



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;
}


然后在QDialog上放一个QToolButton控件,然后提升它到MyToolButton,
但是显示时感觉将对话框背景色改了,QToolButton的背景图片就是这种蓝色:
      
离线searchcai

只看该作者 1楼 发表于: 2013-05-13
QToolButton 则看不到了
快速回复
限100 字节
 
上一个 下一个