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);//参数:移动之后窗口左上角的坐标
}            

猜你喜欢

转载自www.cnblogs.com/wangbin-heng/p/9484094.html