Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Why does resizeEvent react to changing subwidgets and what to do about it?
Forum Updated to NodeBB v4.3 + New Features

Why does resizeEvent react to changing subwidgets and what to do about it?

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 1 Posters 280 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wintreist
    wrote on last edited by
    #1

    Hello, I ran into a problem that resizeEvent is triggered when I change the pixmap of QLabel in it (resizeEvent), which is a sub-widget of QWidget in my program.
    Code, for understanding:

    class EjectedObject(QWidget, Ui_Form_Ejected_Object):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.setupUi(self)
        def resizeEvent(self, event: QResizeEvent = None) -> None:
            pixmap = QPixmap()
            pixmap.loadFromData(self.image_data)
            original_image_size = cv2.imdecode(
                np.frombuffer(self.image_data, np.uint8), -1).shape
            width_ratio = event.size().width() / original_image_size[0]
            height_ratio = event.size().height() / original_image_size[1]
            scale_ratio = min(width_ratio, height_ratio)
            pixmap = pixmap.scaled(
                int(original_image_size[0] * scale_ratio),
                int(original_image_size[1] * scale_ratio),
            )
            self.label_image.setPixmap(pixmap)
            self.label_image.setAlignment(Qt.AlignmentFlag.AlignCenter)
            event.accept()
    

    Ui_Form_Ejected_Object Structure:

    class Ui_Form_Ejected_Object(object):
        def setupUi(self, Form_Ejected_Object):
            Form_Ejected_Object.setObjectName("Form_Ejected_Object")
            Form_Ejected_Object.resize(400, 300)
            self.verticalLayout = QtWidgets.QVBoxLayout(Form_Ejected_Object)
            self.verticalLayout.setObjectName("verticalLayout")
            self.groupBox = QtWidgets.QGroupBox(parent=Form_Ejected_Object)
            self.groupBox.setTitle("")
            self.groupBox.setObjectName("groupBox")
            self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.groupBox)
            self.verticalLayout_2.setObjectName("verticalLayout_2")
            self.label_image = QtWidgets.QLabel(parent=self.groupBox)
            self.label_image.setText("")
            self.label_image.setObjectName("label_image")
            self.verticalLayout_2.addWidget(self.label_image)
            self.verticalLayout.addWidget(self.groupBox)
    
            self.retranslateUi(Form_Ejected_Object)
            QtCore.QMetaObject.connectSlotsByName(Form_Ejected_Object)
    
        def retranslateUi(self, Form_Ejected_Object):
            _translate = QtCore.QCoreApplication.translate
            Form_Ejected_Object.setWindowTitle(_translate("Form_Ejected_Object", "Form"))
    

    The fact is that I add the EjectedObject object to QGridLayout, which changes the sizes of widgets subject to it, and I want the image inside the widget to adjust to the size that is currently available to it, and instead, when the image size changes, resizeEvent is called again

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved