QT之事件過濾器的應用

最近自寫一個自用的軟件,隨便玩玩,現在將一個經驗分享給大家。

1 效果預覽

2 實現

1)鼠標滑入邊框高亮

原理:當鼠標移入時設置焦點並繪製邊框,當鼠標移出時移除焦點,並繪製透明邊框。

方法:利用事件過濾器,監聽鼠標移入與移出事件來設置焦點;利用paintevent繪製邊框

a) 註冊事件過濾器

BirthDayInfoFrm::BirthDayInfoFrm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BirthDayInfoFrm)
{
    ui->setupUi(this);

    this->setFocusPolicy(Qt::ClickFocus);
    installEventFilter(this);
}

b)實現事件過濾方法

protected:
    bool eventFilter(QObject *watched, QEvent *event);
bool BirthDayInfoFrm::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type() == QEvent::Enter)
    {
        //設置焦點
        setFocus();
    }
    else if(event->type() == QEvent::Leave)
    {
        //移除焦點
        focusNextPrevChild(true);
    }

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

c) 利用paintevent繪製邊框

protected:
    bool eventFilter(QObject *watched, QEvent *event);
    void paintEvent(QPaintEvent *);
    void drawBorder(QPainter *painter);
void BirthDayInfoFrm::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing);
    //繪製邊框
    drawBorder(&painter);
}

void BirthDayInfoFrm::drawBorder(QPainter *painter)
{
    painter->save();
    QPen pen;
    pen.setWidth(2);
    pen.setColor(hasFocus() ? QColor(0, 255, 200) : QColor(255, 255, 255, 0));
    painter->setPen(pen);
    painter->drawRect(rect());
    painter->restore();
}

注意:在界面中的佈局控制中 ,上、下、左、右的邊距不要設置爲0

2)實現圖形頭像

將圖片繪製成圖形到QPixmap,現將QPixmap設置到QLabel,設置QLabel的鼠標懸浮樣式及QLabel邊框樣式。

a)QLabel設置圓形圖片

void BirthDayInfoFrm::setPic()
{
    ui->picLabel->setPixmap(PixmapToRoundV2(qApp->applicationDirPath() + "/pic/yz.jpg", (ui->picLabel->width() / 2) -  2));
}

QPixmap BirthDayInfoFrm::PixmapToRound(QString src, int radius)
{
    QPixmap pixmapa(src);
    QPixmap pixmap(radius * 2, radius * 2);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    QPainterPath path;
    path.addEllipse(1, 1, radius * 2, radius * 2);
    painter.setClipPath(path);
    painter.drawPixmap(1, 1, radius * 2, radius * 2, pixmapa);
    return pixmap;
}

b)QLabel設置圓形邊框樣式及懸浮高亮

void BirthDayInfoFrm::setPic()
{
    ui->picLabel->setPixmap(PixmapToRoundV2(qApp->applicationDirPath() + "/pic/yz.jpg", (ui->picLabel->width() / 2) -  2));
    ui->picLabel->setStyleSheet(QString("QLabel:hover{ "
                                        "border-width: 2px; "
                                        "border-style: solid; "
                                        "border-radius:%0px; "
                                        "border-color:rgb(0, 177, 252); }").arg((ui->picLabel->width() / 2) -  2)
                                + QString("QLabel{"
                                          "border-width: 2px 2px 2px 2px;"
                                          "border-style: solid;"
                                          "border-color: rgb(255,255,255);"
                                          "border-radius:%0px;}").arg((ui->picLabel->width() / 2) -  2));
}

3)實現控件內的子控件的響應點擊事件方法

a)子控件註冊事件過濾器

ui->picLabel->installEventFilter(this);
ui->labName->installEventFilter(this);
ui->labClose->installEventFilter(this);

b)在事件過濾器中實現事件響應

bool BirthDayInfoFrm::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonDblClick)
    {
        if( watched == ui->picLabel)
        {
            qDebug() << "pic......";
        }
        else if( watched == ui->labName)
        {
            qDebug() << "name......";
        }

    }
    if (event->type() == QEvent::MouseButtonPress)
    {
        if( watched == ui->labClose)
        {
            qDebug() << "close......";
        }
    }

    if(event->type() == QEvent::Enter)
    {
        setFocus();
    }
    else if(event->type() == QEvent::Leave)
    {
        focusNextPrevChild(true);
    }

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章