How to round QWidget corners
Question:
I wonder if there is there a way to round Qt widget corners?
from PyQt4 import QtCore, QtGui
class Custom(QtGui.QWidget):
def __init__(self, *args, **kwargs):
QtGui.QWidget.__init__(self, *args, **kwargs)
self.setWindowOpacity(0.9)
self.setWindowFlags(QtCore.Qt.Popup|QtCore.Qt.FramelessWindowHint)
self.setWindowTitle('Custom')
self.resize(440,220)
self.move(QtGui.QCursor.pos())
def closeEvent(self, event):
event.accept()
sys.exit(app.exec_())
def mousePressEvent(self, event):
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = Custom()
w.show()
sys.exit(app.exec_())
Answers:
You can use QWidget.setMask(self, QRegion)
for it.
An example in C++:
QWidget *widget = new QWidget;
widget->resize(300, 200);
const int radius = 10;
QPainterPath path;
path.addRoundedRect(widget->rect(), radius, radius);
QRegion mask = QRegion(path.toFillPolygon().toPolygon());
widget->setMask(mask);
widget->show();
SOLUTION:
Here is working Python solution outlined by hank using C++:
import sys
from PySide import QtCore, QtGui
class Custom(QtGui.QWidget):
def __init__(self, *args, **kwargs):
QtGui.QWidget.__init__(self, *args, **kwargs)
self.setWindowOpacity(0.9)
self.setWindowFlags(QtCore.Qt.Popup|QtCore.Qt.FramelessWindowHint)
self.setWindowTitle('Custom')
radius = 40.0
path = QtGui.QPainterPath()
self.resize(440,220)
path.addRoundedRect(QtCore.QRectF(self.rect()), radius, radius)
mask = QtGui.QRegion(path.toFillPolygon().toPolygon())
self.setMask(mask)
self.move(QtGui.QCursor.pos())
def closeEvent(self, event):
event.accept()
sys.exit(app.exec_())
def mousePressEvent(self, event):
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = Custom()
w.show()
sys.exit(app.exec_())
It works if your widget is static, but if you want to resize it and still have rounded corners, there is the paintEvent
method you need to override. It should look like this:
void Widget::paintEvent(QPaintEvent *event)
{
const int radius = 7;
QPainterPath path;
path.addRoundedRect(rect(), radius, radius);
QRegion mask = QRegion(path.toFillPolygon().toPolygon());
setMask(mask);
QWidget::paintEvent(event);
}
I wonder if there is there a way to round Qt widget corners?
from PyQt4 import QtCore, QtGui
class Custom(QtGui.QWidget):
def __init__(self, *args, **kwargs):
QtGui.QWidget.__init__(self, *args, **kwargs)
self.setWindowOpacity(0.9)
self.setWindowFlags(QtCore.Qt.Popup|QtCore.Qt.FramelessWindowHint)
self.setWindowTitle('Custom')
self.resize(440,220)
self.move(QtGui.QCursor.pos())
def closeEvent(self, event):
event.accept()
sys.exit(app.exec_())
def mousePressEvent(self, event):
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = Custom()
w.show()
sys.exit(app.exec_())
You can use QWidget.setMask(self, QRegion)
for it.
An example in C++:
QWidget *widget = new QWidget;
widget->resize(300, 200);
const int radius = 10;
QPainterPath path;
path.addRoundedRect(widget->rect(), radius, radius);
QRegion mask = QRegion(path.toFillPolygon().toPolygon());
widget->setMask(mask);
widget->show();
SOLUTION:
Here is working Python solution outlined by hank using C++:
import sys
from PySide import QtCore, QtGui
class Custom(QtGui.QWidget):
def __init__(self, *args, **kwargs):
QtGui.QWidget.__init__(self, *args, **kwargs)
self.setWindowOpacity(0.9)
self.setWindowFlags(QtCore.Qt.Popup|QtCore.Qt.FramelessWindowHint)
self.setWindowTitle('Custom')
radius = 40.0
path = QtGui.QPainterPath()
self.resize(440,220)
path.addRoundedRect(QtCore.QRectF(self.rect()), radius, radius)
mask = QtGui.QRegion(path.toFillPolygon().toPolygon())
self.setMask(mask)
self.move(QtGui.QCursor.pos())
def closeEvent(self, event):
event.accept()
sys.exit(app.exec_())
def mousePressEvent(self, event):
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = Custom()
w.show()
sys.exit(app.exec_())
It works if your widget is static, but if you want to resize it and still have rounded corners, there is the paintEvent
method you need to override. It should look like this:
void Widget::paintEvent(QPaintEvent *event)
{
const int radius = 7;
QPainterPath path;
path.addRoundedRect(rect(), radius, radius);
QRegion mask = QRegion(path.toFillPolygon().toPolygon());
setMask(mask);
QWidget::paintEvent(event);
}