SPetkoski
SPetkoski

Reputation: 1

how to call resizeEvent function for QDockWidget in QMainWindow

I am trying to implement a QMainWindow with centralWidget and dockWidget. When the user resize the dock widget I want resizeEvent to be called for the dock widget and some values to be returned. I have implemented the resizeEvent for the complete QMainWindow and it is working fine. How can i call resizeEvent for the dockWidget which is a Qwidget without making another class which will inherit from Qwidget and implement the resizeEvent there and afterwards to create an object in QMainwindow. The first given example is working just fine.

class ui(QMainWindow):
    def __init__(self):
        super().__init__()

        self.bottom_dock_widget = DockWidget('Results')
        self.addDockWidget(Qt.BottomDockWidgetArea, self.bottom_dock_widget)

        self.resize(500, 500)

    def resizeEvent(self, event):
        print('mainWindow')
        self.update()

class DockWidget(QDockWidget):
    def __init__(self, name, image_view):
        super().__init__()
        self.setWindowTitle(name)
    def resizeEvent(self, event):
        print('in Dock')
        self.update()

Is there a way to be able to implement the example like this:

class ui(QMainWindow):
    def __init__(self):
        super().__init__()

        self.bottom_dock_widget = QDockWidget('Results')
        self.addDockWidget(Qt.BottomDockWidgetArea, self.bottom_dock_widget)
        self.resize(500, 500)

but to be able to call resizeEvent only for the dock widget

    def resizeEvent(self, event):
        print('dock')
        self.update()

like on c++ with the scope

def bottom_dock_widget :: resizeEvent(self):

Upvotes: 0

Views: 658

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

If you want to hear the resize event of a widget it is not necessary to override the resizeEvent() method since it is enough to install an event filter analyzed the QEvent::Resize event

import sys

from PyQt5.QtCore import QEvent, Qt
from PyQt5.QtWidgets import QApplication, QDockWidget, QMainWindow


class UI(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.bottom_dock_widget = QDockWidget("Results")
        self.bottom_dock_widget.installEventFilter(self)
        self.addDockWidget(Qt.BottomDockWidgetArea, self.bottom_dock_widget)
        self.resize(500, 500)

    def eventFilter(self, obj, event):
        if obj is self.bottom_dock_widget and event.type() == QEvent.Resize:
            print("dock")
        return super().eventFilter(obj, event)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = UI()
    w.show()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions