QT 鼠标移到按钮上,就弹出对话框,移开,对话框就消失

    我们在做项目的时候,有一个这样的需求,就是一个按钮,鼠标放上去之后,就会弹出一张图片,鼠标移开,图片就消失。

比如这样的按钮,

鼠标放上去之后,就会弹出对话框



该怎么做呢?

其中,

1.那个带图标的按钮, 其实是个QToolButton, 可以设置前面图标,后面文字。

2.使用QLabel 显示图片

 
    m_pStandardLabel = new QLabel(this);
    QPixmap pixmap(qApp->applicationDirPath() + "/scoreStandard.png");
    m_pStandardLabel->setPixmap(pixmap);

    m_pStandardLabel->resize(619, 471);
    m_pStandardLabel->hide();

3.安装事件过滤器(需要给QToolButton 和显示图片的QLabel都要安装)

    ui->scoreStandardBtn->installEventFilter(this);
    m_pStandardLabel->installEventFilter(this);

4.重写事件过滤器

bool eventFilter(QObject *watched, QEvent *event);
bool QcTaskDetailForm::eventFilter(QObject *watched, QEvent *event)
{

    if(ui->scoreStandardBtn == watched || m_pStandardLabel == watched) {
        if(QEvent::Enter == event->type()) {            //鼠标进入
            if (m_pStandardLabel->isHidden()) { //已经隐藏就显示出来
                m_pStandardLabel->show();
                QPoint point = ui->scoreStandardBtn->pos();
                point.rx() = point.x() -  m_pStandardLabel->width() + 120;
                point.ry() = point.y() + ui->scoreStandardBtn->height() + 40;
                m_pStandardLabel->move(point);

                m_pStandardLabel->raise();//显示最顶层
                return true;
            }
        }
        else if (QEvent::Leave == event->type()) { //鼠标离开
            if (!m_pStandardLabel->isHidden()) {
                if(!ui->scoreStandardBtn->geometry().contains(this->mapFromGlobal(QCursor::pos())) //判断鼠标是否在控件上
                   &&!m_pStandardLabel->geometry().contains(this->mapFromGlobal(QCursor::pos())) )
                {
                    m_pStandardLabel->hide();
                    return true;
                }
            }
        }
    }

     return QWidget::eventFilter(watched, event);
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值