Effectively applying QtCore.Qt.FramelessWindowHint
we eliminate the functions of the decorator of OS windows, which leads to the impossibility of performing the functions it provides as resize using the window borders, the functions of the maximized, minimized buttons and close or drag and drop the window in a certain position.
Another very simple way to resize the window in this case is to use a QSizeGrip
like the one that has the status bar of QMainWindow
.
If you can not use the QSizeGrip
of the status bar of QMainWindows
given the difficulty you mention of resizing this and that the frame occupies the space left you have several options.
Following your own example you can use a base% QWidget
although in this case you must manually create the instance of QSizeGrip
and add it to the widget in the appropriate position. Based on your own example but using a QGridLAyout
and two QFrame
:
from PyQt5 import QtCore, QtGui, QtWidgets
class Dialog(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.resize(600, 300)
self.init_ui()
def init_ui(self):
self.gridLayout = QtWidgets.QGridLayout(self)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setSpacing(0)
self.frame = QtWidgets.QFrame(self)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum,
QtWidgets.QSizePolicy.Preferred
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
self.frame.setSizePolicy(sizePolicy)
self.frame.setMaximumSize(QtCore.QSize(120, 16777215))
self.frame.setStyleSheet("background:blue;")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
self.frame_2 = QtWidgets.QFrame(self)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Preferred
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.frame_2.sizePolicy().hasHeightForWidth())
self.frame_2.setSizePolicy(sizePolicy)
self.frame_2.setStyleSheet("background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 211), stop:1 rgba(255, 255, 255, 221));")
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.gridLayout.addWidget(self.frame_2, 0, 1, 1, 1)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground,True)
self.setAttribute(QtCore.Qt.WA_NoSystemBackground, False)
self.setWindowOpacity(0.9)
sizeGrip= QtWidgets.QSizeGrip(self)
self.gridLayout.addWidget(sizeGrip, 0, 1, 1, 1, QtCore.Qt.AlignBottom | QtCore.Qt.AlignRight)
# Centrar widget en la ventana
qt_rect = self.frameGeometry()
center_point = QtWidgets.QDesktopWidget().availableGeometry().center()
qt_rect.moveCenter(center_point)
self.move(qt_rect.topLeft())
self.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
dialog = Dialog()
sys.exit(app.exec_())
Another very simple option is to use a QDialog
that also has the possibility to enable its QSizeGrip
in the lower right corner using the QDialog.setSizeGripEnabled
method:
from PyQt5 import QtCore, QtGui, QtWidgets
class Dialog(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.resize(600, 300)
self.init_ui()
def init_ui(self):
self.gridLayout = QtWidgets.QGridLayout(self)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setSpacing(0)
self.frame = QtWidgets.QFrame(self)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum,
QtWidgets.QSizePolicy.Preferred
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
self.frame.setSizePolicy(sizePolicy)
self.frame.setMaximumSize(QtCore.QSize(120, 16777215))
self.frame.setStyleSheet("background:blue;")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
self.frame_2 = QtWidgets.QFrame(self)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Preferred
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.frame_2.sizePolicy().hasHeightForWidth())
self.frame_2.setSizePolicy(sizePolicy)
self.frame_2.setStyleSheet("background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 211), stop:1 rgba(255, 255, 255, 221));")
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.gridLayout.addWidget(self.frame_2, 0, 1, 1, 1)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground,True)
self.setAttribute(QtCore.Qt.WA_NoSystemBackground, False)
self.setWindowOpacity(0.9)
self.setSizeGripEnabled(True)
self.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
dialog = Dialog()
sys.exit(app.exec_())
The result is similar in both cases: