Add resize event to a qwidget pyqt5

1

I have the following problem:

I am designing a user interface for a program, however I did not use a main window since I did not find a way to reduce the size of the qstatusbar

So I'm doing it on a qwidget, the problem is that by removing the window frames self.setWindowFlags(QtCore.Qt.FramelessWindowHint) I can not change the size of the widget, as it would in a normal frame sale.

any solution?

Code .py

from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5 import uic 
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore, Qt, QtGui

class Widget(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        uic.loadUi("23.ui",self)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground,True)
        self.setAttribute(QtCore.Qt.WA_NoSystemBackground,False)





app = QApplication([])
p = Widget()
p.show()
app.exec_()

.ui code

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>principal</class>
 <widget class="QWidget" name="principal">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>595</width>
    <height>418</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <property name="styleSheet">
   <string notr="true">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));</string>
  </property>
  <widget class="QFrame" name="frame">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>120</width>
     <height>421</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">background:blue;</string>
   </property>
   <property name="frameShape">
    <enum>QFrame::StyledPanel</enum>
   </property>
   <property name="frameShadow">
    <enum>QFrame::Raised</enum>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
    
asked by Revsky01 30.06.2018 в 18:20
source

1 answer

0

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:

    
answered by 30.06.2018 / 21:43
source