QT using QSS to implement rounded corners dialog box
Round Corner dialog box There are many ways, usually in the way of redrawing the code, more complex, this high imitation 360 interface to the code, more complex, put into my qdialog window always does not take effect:
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this);setWindowFlags(Qt::FramelessWindowHint);//隐藏边框和标题栏//生成一张位图QBitmap objBitmap(size());//QPainter用于在位图上绘画QPainter painter(&objBitmap);//填充位图矩形框(用白色填充)painter.fillRect(rect(),Qt::white);painter.setBrush(QColor(0,0,0));//在位图上画圆角矩形(用黑色填充)painter.drawRoundedRect(this->rect(),5,5);//使用setmask过滤即可setMask(objBitmap);//设置背景QPalette pal;QPixmap pixmap(QDir::toNativeSeparators (":/image/frame.png"));pal.setBrush(QPalette::Window, QBrush(pixmap));setPalette(pal);}
Simple implementation of rounded corners window
The actual discovery, can be simple through QT styleSheet to achieve this function, quite simple and easy to use.
The idea is to put the qdialog background transparent, and then put a rounded corner of the Qdialog Qframe can achieve the fillet effect!
2.1 Initialization dialog box
OnePicker::OnePicker(QWidget *parent) :RoundCornerPicker(parent),ui(new Ui::OnePicker){// ui->setupUi(this); setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);}
2.2 Setting the appropriate style
Where the style of the dialog box is
Border:none; Background-color:transparent;
Frame style used for straight-forward backgrounds
Border-image:url (:/resource/images/picker_bk.png);
border-width:1px 1px 1px 1px;
Border-style:solid;
Border-color:rgb (76,76,76);
border-radius:6px;
2.3 Global Style Sheet
You can put the above definitions into the global QSS style sheet, so that all dialogs automatically become the fillet moment table
Qdialog { border: none; background-color: transparent; } Qdialog #DialogBk { border-image:URL (:/resource/images/picker_bk.png) ; border-width: 1px 1px 1px 1px ; border-style: solid ; border-color: rgb (+,+) ; Border-radius:6px ;}
The final effect is as follows
Reasons for not making rounded corners of Qdialog directly
Why the use of qframe is caused by forcing the qdialog into a rounded background, still showing a four-week rectangular border, which is extremely unattractive.
QT using QSS to implement rounded corners dialog box