最近在用qss做界面美化的工作,发现一个问题就是qss不能对顶级窗口设置边角圆弧,于是得另辟蹊径。据网上搜索可得到的方法我实现了三种:
setWindowFlags(Qt::FramelessWindowHint); QBitmap bmp(this->size()); bmp.fill(); QPainter p(&bmp); // p.setPen(Qt::NoPen); // p.setBrush(Qt::black); p.setRenderHint(QPainter::Antialiasing); //p.drawRoundedRect(bmp.rect(), 20, 20); //四个角都是圆弧 //只要上边角圆弧 int arcR = 20; QRect rect = this->rect(); QPainterPath path; //逆时针 path.moveTo(arcR, 0); path.arcTo(0, 0, arcR * 2, arcR * 2, 90.0f, 90.0f); path.lineTo(0, rect.height()); path.lineTo(rect.width(), rect.height()); path.lineTo(rect.width(), arcR); path.arcTo(rect.width() - arcR * 2, 0, arcR * 2, arcR * 2, 0.0f, 90.0f); path.lineTo(arcR, 0); p.drawPath(path); p.fillPath(path, QBrush(Qt::Red)); //arm和windows平台没有这行代码将显示一个透明的空空的框 setMask(bmp);
2.
构造函数中
setAttribute(Qt::WA_TranslucentBackground, true); setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
重载paintEvent
QPainter p(this); p.drawRoundedRect(0, 0, width() - 1, height() - 1, 20, 20);
3.背景图片
QPixmap pixmap(":/images/*.png"); // setMask(pixmap.mask()); QPalette palette; palette.setBrush(QPalette::Background, QBrush( pixmap ) ); setPalette(palette); resize( pixmap.size() ); setMask(pixmap.mask());