Qt不規則窗體和按鍵


1、重寫paintEvent()

 1 void Widget::paintEvent(QPaintEvent *)
 2 {
 3     QPainter p(this);
 4     p.drawPixmap(0, 0, QPixmap("../Image/sunny.png"));
 5     //不規則按鍵 
 6     QPixmap pix;
 7     pix.load("../Image/face.png");
 8     ui->pushButton->setFixedSize(pix.size());//在Designer中定義一噶按鍵
 9     ui->pushButton->move(10,10);
10     ui->pushButton->setMask(pix.mask());
11     ui->pushButton->setStyleSheet("background-image: url(../Image/face.png)");
12 }

 

2、在構造函數中

1 //去邊框
2 setWndowFlag(Qt::FramelessWindowHint|windowsFlags());
3 //把窗口背景設置為透明
4 setAttribute(Qt::WA_TranslucentBackground);

3、重寫鼠標事件移動窗口

void Widget::mousePressEvent(QMouseEvent *e)
{//注意:移動是當前 窗口左上角 相對於 屏幕坐上角 坐標而言的。
    if(e->button() == Qt::RightButton){
        close();
    }
    else if(e->button() == Qt::LeftButton){
        //坐標差值:當前點擊坐標-窗口左上角坐標
        p = e->globalPos() - this->frameGeometry().topLeft();
    }
}

void Widget::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons() & Qt::LeftButton)
        move(e->globalPos() - p);//參數:移動之后窗口左上角的坐標
}            

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM