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