Qt編程擷取滑鼠移動事件

來源:互聯網
上載者:User

標籤:qt   移動   滑鼠   

想做下面一個效果:想當滑鼠移動到按鈕上的時候,按鈕就變大,表徵圖換個大的,滑鼠不在按鈕上的時候,按鈕就變小,表徵圖也換成小的,感覺比較Cool


實現原理:為每個按鈕設定監聽屬性

void MainHomeForm::init(){    //為按鈕註冊事件    ui->SystemSetButton->installEventFilter(this);    ui->ZoneSetButton->installEventFilter(this);}

//系統監聽器監聽按鈕對象bool MainHomeForm::eventFilter(QObject *target, QEvent *e){    if(target == ui->SystemSetButton)    {        if(e->type() == QEvent::Enter)        {            ui->SystemSetButton->resize(163,91);            QIcon *MouseOnIcon = new QIcon(":/new/prefix1/back/系統設定2.png");            ui->SystemSetButton->setIcon(*MouseOnIcon);        }        else if(e->type() == QEvent::Leave)        {            ui->SystemSetButton->resize(115,60);            QIcon *MouseOffIcon = new QIcon(":/new/prefix1/back/系統設定.png");            ui->SystemSetButton->setIcon(*MouseOffIcon);        }    }    else if(target == ui->ZoneSetButton)    {        if(e->type() == QEvent::Enter)        {            ui->ZoneSetButton->resize(163,91);            QIcon *MouseOnIcon = new QIcon(":/new/prefix1/back/地區控制2.png");            ui->ZoneSetButton->setIcon(*MouseOnIcon);        }        else if(e->type() == QEvent::Leave)        {            ui->ZoneSetButton->resize(115,60);            QIcon *MouseOffIcon = new QIcon(":/new/prefix1/back/地區控制.png");            ui->ZoneSetButton->setIcon(*MouseOffIcon);        }    }}

搞定,首先綁定按鈕的installEventFilter為當前表單,在表單的EventFilter事件中,看移到了哪個按鈕上面,就可以了。


還有一種就是,當滑鼠按下去的時候,移動到按鈕上面才會有變化:

//跟蹤滑鼠移動事件,當滑鼠移動到中間的按鈕上時,改變表徵圖大小和內容void MainHomeForm::mouseMoveEvent(QMouseEvent *e){    e->accept();    if(enterBtn(e->pos(),ui->SystemSetButton))    {        ui->SystemSetButton->setSizeIncrement(163,91);        QIcon *MouseOnIcon = new QIcon(":/new/prefix1/back/系統設定2.png");        ui->SystemSetButton->setIcon(*MouseOnIcon);    }}//自己寫的函數,判斷滑鼠是否在一個按鈕地區內bool MainHomeForm::enterBtn(QPoint pp, QToolButton *btn){    int height = btn->height();    int width = btn->width();    QPoint btnMinPos = btn->pos();    QPoint btnMaxPos = btn->pos();    btnMaxPos.setX(btn->pos().x()+width);    btnMaxPos.setY(btn->pos().y()+height);    if(pp.x() >= btnMinPos.x() && pp.y() >= btnMinPos.y()        && pp.x() <= btnMaxPos.x() && pp.y() <= btnMaxPos.y())        return true;    else        return false;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.