title: Qt Widget resizing after overwriting the mousePressEvent
body: <p>I'm trying to implement a context menu in a QtTreeView Widget. Using the advice in
  <a href="https://stackoverflow.com/questions/35992088/why-mousemoveevent-does-nothing-in-pyqt5">Why mouseMoveEvent does nothing in PyQt5</a> I've created a sub class of the QTreeView: </p>
  
  <pre><code>class TreeView(QTreeView):
  def mousePressEvent(self, event):
      if event.type() == QEvent.MouseButtonPress:
          if event.button() == Qt.LeftButton:
              print( "Left click")
          elif event.button() == Qt.RightButton:
              print( "Right click")
          elif event.button() == Qt.MiddleButton:
              print( "Middle click")
      super(TreeView, self).mousePressEvent(event)
  </code></pre>
  
  <p>which I'm overwriting the QtTreeView (which I designed in Qt Designer) as follows:</p>
  
  <pre><code>self.dockWidget.treeView = TreeView(self.dockWidget.treeView)
  </code></pre>
  
  <p>This works and allows for the mousePressEvents to be captured in the TreeView but it changes the size of the content in the TreeView:</p>
  
  <p><a href="https://i.stack.imgur.com/k5Nx9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k5Nx9.png" alt="Incorrect TreeView Size"></a></p>
  
  <p>When it should look like:</p>
  
  <p><img src="https://i.stack.imgur.com/yu8m6.png" alt="Correct TreeView Size[3]"></p>
  
  <p>I've tried setting the SizePolicy:</p>
  
  <pre><code>self.dockWidget.treeView.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
  </code></pre>
  
  <p>but its not making a difference. I'm looking for a way to either prevent it from changing size or return it to its original size once it has changed.</p>
  
  <p>Reprex - 3 Files:</p>
  
  <p>main.py</p>
  
  <pre><code>from PyQt5.QtWidgets import QApplication, QMainWindow, QDockWidget, 
  QListWidget,QTextEdit,QTreeView, QSizePolicy
  from PyQt5.QtGui import QIcon, QStandardItemModel
  from PyQt5.QtCore import Qt, QEvent
  import sys
  
  from utility_core_dockwidget import UtilityDockWidget
  
  
  
  class Window(QMainWindow):
      def __init__(self):
          super().__init__()
  
          title= "Dockable Application"
          top=400
          left=400
          width=600
          height= 500
  
          icon = "icon.png"
  
          self.setWindowTitle(title)
          self.setGeometry(top,left,width,height)
          self.setWindowIcon(QIcon("icon.png"))
  
          self.textEdit = QTextEdit()
          self.setCentralWidget(self.textEdit)
          self.dockAble()
          self.populateTreeView()
  
      def dockAble(self):
          self.dock = UtilityDockWidget()
          self.dock.treeView= TreeView(self.dock.treeView) #overwriting the treeview with the custom class causes the display issues
          self.addDockWidget(Qt.RightDockWidgetArea,self.dock)
          self.dock.treeView.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
  
      def populateTreeView(self):
          print("here")
          model = self.createTreeViewLayerModel(self.dock.treeView)
          self.dock.treeView.setModel(model)
  
          for i in range (1,10):
              layerName= f"LayerName {i}"
              visibility = "Visible"
              layerId = i
              self.addTreeViewItem(model, layerName, visibility, layerId)
  
      def createTreeViewLayerModel(self,parent):
          model = QStandardItemModel(0, 3, parent)
          model.setHeaderData(0, Qt.Horizontal, "Layer Name")
          model.setHeaderData(1, Qt.Horizontal, "Visibility")
          model.setHeaderData(2, Qt.Horizontal, "Layer ID")
  
          return model
  
      def addTreeViewItem(self,model, layerName, visibility,layerId):
          model.insertRow(0)
          model.setData(model.index(0, 0), layerName)
          model.setData(model.index(0, 1), visibility)
          model.setData(model.index(0, 2), layerId)
  
  
  class TreeView(QTreeView):
      def mousePressEvent(self, event):
          if event.type() == QEvent.MouseButtonPress:
              if event.button() == Qt.LeftButton:
                  print( "Left click")
              elif event.button() == Qt.RightButton:
                  print( "Right click")
              elif event.button() == Qt.MiddleButton:
                  print( "Middle click")
          super(TreeView, self).mousePressEvent(event)
  
  app = QApplication(sys.argv)
  window=Window()
  window.show()
  app.exec()
  </code></pre>
  
  <p>utility_core_dockwidget.py:</p>
  
  <pre><code>import os
  
  from PyQt5 import uic, QtWidgets, QtGui
  from PyQt5.QtCore import pyqtSignal, Qt
  
  
  FORM_CLASS, _ = uic.loadUiType(os.path.join(
      os.path.dirname(__file__), 'utility_core_dockwidget_base.ui'))
  
  
  class UtilityDockWidget(QtWidgets.QDockWidget, FORM_CLASS):
  
      closingPlugin = pyqtSignal()
  
      def __init__(self, parent=None):
          """Constructor."""
          super(UtilityDockWidget, self).__init__(parent)
          self.setupUi(self)
  
      def closeEvent(self, event):
          self.closingPlugin.emit()
          event.accept()
  </code></pre>
  
  <p>utility_core_dockwidget_base.ui:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ui version="4.0"&gt;
   &lt;class&gt;utilityDockWidgetBase&lt;/class&gt;
   &lt;widget class="QDockWidget" name="utilityDockWidgetBase"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;0&lt;/x&gt;
      &lt;y&gt;0&lt;/y&gt;
      &lt;width&gt;660&lt;/width&gt;
      &lt;height&gt;807&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="windowTitle"&gt;
     &lt;string&gt;Utility&lt;/string&gt;
    &lt;/property&gt;
    &lt;widget class="QWidget" name="dockWidgetContents"&gt;
     &lt;layout class="QHBoxLayout" name="horizontalLayout_7"&gt;
      &lt;item&gt;
       &lt;widget class="QTabWidget" name="tabWidget_2"&gt;
        &lt;property name="sizePolicy"&gt;
         &lt;sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding"&gt;
          &lt;horstretch&gt;0&lt;/horstretch&gt;
          &lt;verstretch&gt;0&lt;/verstretch&gt;
         &lt;/sizepolicy&gt;
        &lt;/property&gt;
        &lt;property name="minimumSize"&gt;
         &lt;size&gt;
          &lt;width&gt;200&lt;/width&gt;
          &lt;height&gt;500&lt;/height&gt;
         &lt;/size&gt;
        &lt;/property&gt;
        &lt;property name="currentIndex"&gt;
         &lt;number&gt;0&lt;/number&gt;
        &lt;/property&gt;
        &lt;widget class="QWidget" name="tab_5"&gt;
         &lt;attribute name="title"&gt;
          &lt;string&gt;Layer Source Changer&lt;/string&gt;
         &lt;/attribute&gt;
         &lt;layout class="QVBoxLayout" name="verticalLayout_7"&gt;
          &lt;item&gt;
           &lt;layout class="QGridLayout" name="gridLayout_2"&gt;
            &lt;property name="topMargin"&gt;
             &lt;number&gt;6&lt;/number&gt;
            &lt;/property&gt;
            &lt;item row="1" column="0"&gt;
             &lt;widget class="QLabel" name="label_8"&gt;
              &lt;property name="font"&gt;
               &lt;font&gt;
                &lt;weight&gt;75&lt;/weight&gt;
                &lt;bold&gt;true&lt;/bold&gt;
               &lt;/font&gt;
              &lt;/property&gt;
              &lt;property name="text"&gt;
               &lt;string&gt;Source List&lt;/string&gt;
              &lt;/property&gt;
             &lt;/widget&gt;
            &lt;/item&gt;
            &lt;item row="0" column="1" colspan="2"&gt;
             &lt;widget class="QLineEdit" name="searchBar"&gt;
              &lt;property name="sizePolicy"&gt;
               &lt;sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed"&gt;
                &lt;horstretch&gt;0&lt;/horstretch&gt;
                &lt;verstretch&gt;0&lt;/verstretch&gt;
               &lt;/sizepolicy&gt;
              &lt;/property&gt;
              &lt;property name="maximumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;10000&lt;/width&gt;
                &lt;height&gt;16777215&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
             &lt;/widget&gt;
            &lt;/item&gt;
            &lt;item row="1" column="1"&gt;
             &lt;widget class="QComboBox" name="layerSourceList"&gt;
              &lt;property name="sizePolicy"&gt;
               &lt;sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed"&gt;
                &lt;horstretch&gt;0&lt;/horstretch&gt;
                &lt;verstretch&gt;0&lt;/verstretch&gt;
               &lt;/sizepolicy&gt;
              &lt;/property&gt;
              &lt;property name="minimumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;250&lt;/width&gt;
                &lt;height&gt;0&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="maximumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;10000&lt;/width&gt;
                &lt;height&gt;16777215&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="font"&gt;
               &lt;font&gt;
                &lt;family&gt;MS Shell Dlg 2&lt;/family&gt;
               &lt;/font&gt;
              &lt;/property&gt;
              &lt;property name="editable"&gt;
               &lt;bool&gt;false&lt;/bool&gt;
              &lt;/property&gt;
             &lt;/widget&gt;
            &lt;/item&gt;
            &lt;item row="1" column="2"&gt;
             &lt;widget class="QPushButton" name="browseNewSource"&gt;
              &lt;property name="maximumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;35&lt;/width&gt;
                &lt;height&gt;16777215&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="text"&gt;
               &lt;string&gt;...&lt;/string&gt;
              &lt;/property&gt;
             &lt;/widget&gt;
            &lt;/item&gt;
            &lt;item row="0" column="0"&gt;
             &lt;widget class="QLabel" name="label_9"&gt;
              &lt;property name="sizePolicy"&gt;
               &lt;sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred"&gt;
                &lt;horstretch&gt;0&lt;/horstretch&gt;
                &lt;verstretch&gt;0&lt;/verstretch&gt;
               &lt;/sizepolicy&gt;
              &lt;/property&gt;
              &lt;property name="maximumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;50&lt;/width&gt;
                &lt;height&gt;16777215&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="font"&gt;
               &lt;font&gt;
                &lt;weight&gt;75&lt;/weight&gt;
                &lt;bold&gt;true&lt;/bold&gt;
               &lt;/font&gt;
              &lt;/property&gt;
              &lt;property name="layoutDirection"&gt;
               &lt;enum&gt;Qt::LeftToRight&lt;/enum&gt;
              &lt;/property&gt;
              &lt;property name="autoFillBackground"&gt;
               &lt;bool&gt;false&lt;/bool&gt;
              &lt;/property&gt;
              &lt;property name="text"&gt;
               &lt;string&gt;Search&lt;/string&gt;
              &lt;/property&gt;
             &lt;/widget&gt;
            &lt;/item&gt;
           &lt;/layout&gt;
          &lt;/item&gt;
          &lt;item&gt;
           &lt;layout class="QHBoxLayout" name="horizontalLayout_8"&gt;
            &lt;item&gt;
             &lt;widget class="QPushButton" name="refreshComboBox"&gt;
              &lt;property name="sizePolicy"&gt;
               &lt;sizepolicy hsizetype="Minimum" vsizetype="Fixed"&gt;
                &lt;horstretch&gt;0&lt;/horstretch&gt;
                &lt;verstretch&gt;0&lt;/verstretch&gt;
               &lt;/sizepolicy&gt;
              &lt;/property&gt;
              &lt;property name="minimumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;75&lt;/width&gt;
                &lt;height&gt;0&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="maximumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;120&lt;/width&gt;
                &lt;height&gt;16777215&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="text"&gt;
               &lt;string&gt;Refresh Sources&lt;/string&gt;
              &lt;/property&gt;
             &lt;/widget&gt;
            &lt;/item&gt;
            &lt;item&gt;
             &lt;widget class="QCheckBox" name="onlyVisibleCheckbox"&gt;
              &lt;property name="layoutDirection"&gt;
               &lt;enum&gt;Qt::LeftToRight&lt;/enum&gt;
              &lt;/property&gt;
              &lt;property name="text"&gt;
               &lt;string&gt;Only Visible Layers&lt;/string&gt;
              &lt;/property&gt;
             &lt;/widget&gt;
            &lt;/item&gt;
           &lt;/layout&gt;
          &lt;/item&gt;
          &lt;item&gt;
           &lt;layout class="QGridLayout" name="gridLayout_4"&gt;
            &lt;property name="topMargin"&gt;
             &lt;number&gt;10&lt;/number&gt;
            &lt;/property&gt;
            &lt;item row="1" column="0"&gt;
             &lt;widget class="QPushButton" name="updateSelectedButton"&gt;
              &lt;property name="sizePolicy"&gt;
               &lt;sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed"&gt;
                &lt;horstretch&gt;0&lt;/horstretch&gt;
                &lt;verstretch&gt;0&lt;/verstretch&gt;
               &lt;/sizepolicy&gt;
              &lt;/property&gt;
              &lt;property name="minimumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;110&lt;/width&gt;
                &lt;height&gt;0&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="maximumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;100&lt;/width&gt;
                &lt;height&gt;16777215&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="text"&gt;
               &lt;string&gt;Update Selected&lt;/string&gt;
              &lt;/property&gt;
             &lt;/widget&gt;
            &lt;/item&gt;
            &lt;item row="1" column="1"&gt;
             &lt;widget class="QPushButton" name="updateAllButton"&gt;
              &lt;property name="minimumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;110&lt;/width&gt;
                &lt;height&gt;0&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="maximumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;110&lt;/width&gt;
                &lt;height&gt;16777215&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="toolTip"&gt;
               &lt;string&gt;Update all the layers with sources found in Source List&lt;/string&gt;
              &lt;/property&gt;
              &lt;property name="text"&gt;
               &lt;string&gt;Update All&lt;/string&gt;
              &lt;/property&gt;
             &lt;/widget&gt;
            &lt;/item&gt;
            &lt;item row="1" column="2"&gt;
             &lt;spacer name="horizontalSpacer_5"&gt;
              &lt;property name="orientation"&gt;
               &lt;enum&gt;Qt::Horizontal&lt;/enum&gt;
              &lt;/property&gt;
              &lt;property name="sizeType"&gt;
               &lt;enum&gt;QSizePolicy::Expanding&lt;/enum&gt;
              &lt;/property&gt;
              &lt;property name="sizeHint" stdset="0"&gt;
               &lt;size&gt;
                &lt;width&gt;40&lt;/width&gt;
                &lt;height&gt;20&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
             &lt;/spacer&gt;
            &lt;/item&gt;
            &lt;item row="2" column="0"&gt;
             &lt;widget class="QLabel" name="sourceInformationLabel"&gt;
              &lt;property name="maximumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;120&lt;/width&gt;
                &lt;height&gt;16777215&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="font"&gt;
               &lt;font&gt;
                &lt;weight&gt;75&lt;/weight&gt;
                &lt;bold&gt;true&lt;/bold&gt;
               &lt;/font&gt;
              &lt;/property&gt;
              &lt;property name="text"&gt;
               &lt;string&gt;Source Information&lt;/string&gt;
              &lt;/property&gt;
             &lt;/widget&gt;
            &lt;/item&gt;
            &lt;item row="0" column="0"&gt;
             &lt;widget class="QLabel" name="label_10"&gt;
              &lt;property name="sizePolicy"&gt;
               &lt;sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred"&gt;
                &lt;horstretch&gt;0&lt;/horstretch&gt;
                &lt;verstretch&gt;0&lt;/verstretch&gt;
               &lt;/sizepolicy&gt;
              &lt;/property&gt;
              &lt;property name="maximumSize"&gt;
               &lt;size&gt;
                &lt;width&gt;110&lt;/width&gt;
                &lt;height&gt;16777215&lt;/height&gt;
               &lt;/size&gt;
              &lt;/property&gt;
              &lt;property name="font"&gt;
               &lt;font&gt;
                &lt;weight&gt;75&lt;/weight&gt;
                &lt;bold&gt;true&lt;/bold&gt;
               &lt;/font&gt;
              &lt;/property&gt;
              &lt;property name="toolTip"&gt;
               &lt;string&gt;Update to the latest version&lt;/string&gt;
              &lt;/property&gt;
              &lt;property name="text"&gt;
               &lt;string&gt;Automatic Options&lt;/string&gt;
              &lt;/property&gt;
             &lt;/widget&gt;
            &lt;/item&gt;
           &lt;/layout&gt;
          &lt;/item&gt;
          &lt;item&gt;
           &lt;widget class="QTreeView" name="treeView"&gt;
            &lt;property name="sizePolicy"&gt;
             &lt;sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding"&gt;
              &lt;horstretch&gt;0&lt;/horstretch&gt;
              &lt;verstretch&gt;0&lt;/verstretch&gt;
             &lt;/sizepolicy&gt;
            &lt;/property&gt;
            &lt;property name="minimumSize"&gt;
             &lt;size&gt;
              &lt;width&gt;0&lt;/width&gt;
              &lt;height&gt;0&lt;/height&gt;
             &lt;/size&gt;
            &lt;/property&gt;
            &lt;property name="maximumSize"&gt;
             &lt;size&gt;
              &lt;width&gt;16777215&lt;/width&gt;
              &lt;height&gt;16777215&lt;/height&gt;
             &lt;/size&gt;
            &lt;/property&gt;
            &lt;property name="styleSheet"&gt;
             &lt;string notr="true"&gt;selection-color: qradialgradient(spread:repeat, cx:0.5, cy:0.5, radius:0.077, fx:0.5, fy:0.5, stop:0 rgba(0, 169, 255, 147), stop:0.497326 rgba(0, 0, 0, 147), stop:1 rgba(0, 169, 255, 147));&lt;/string&gt;
            &lt;/property&gt;
            &lt;property name="rootIsDecorated"&gt;
             &lt;bool&gt;false&lt;/bool&gt;
            &lt;/property&gt;
            &lt;property name="sortingEnabled"&gt;
             &lt;bool&gt;true&lt;/bool&gt;
            &lt;/property&gt;
            &lt;property name="animated"&gt;
             &lt;bool&gt;true&lt;/bool&gt;
            &lt;/property&gt;
            &lt;attribute name="headerCascadingSectionResizes"&gt;
             &lt;bool&gt;true&lt;/bool&gt;
            &lt;/attribute&gt;
            &lt;attribute name="headerDefaultSectionSize"&gt;
             &lt;number&gt;200&lt;/number&gt;
            &lt;/attribute&gt;
            &lt;attribute name="headerShowSortIndicator" stdset="0"&gt;
             &lt;bool&gt;true&lt;/bool&gt;
            &lt;/attribute&gt;
           &lt;/widget&gt;
          &lt;/item&gt;
          &lt;item&gt;
           &lt;widget class="QLabel" name="label_11"&gt;
            &lt;property name="font"&gt;
             &lt;font&gt;
              &lt;weight&gt;75&lt;/weight&gt;
              &lt;bold&gt;true&lt;/bold&gt;
             &lt;/font&gt;
            &lt;/property&gt;
            &lt;property name="text"&gt;
             &lt;string&gt;Log&lt;/string&gt;
            &lt;/property&gt;
           &lt;/widget&gt;
          &lt;/item&gt;
          &lt;item&gt;
           &lt;widget class="QTextEdit" name="textEdit"/&gt;
          &lt;/item&gt;
          &lt;item&gt;
           &lt;spacer name="verticalSpacer_4"&gt;
            &lt;property name="orientation"&gt;
             &lt;enum&gt;Qt::Vertical&lt;/enum&gt;
            &lt;/property&gt;
            &lt;property name="sizeHint" stdset="0"&gt;
             &lt;size&gt;
              &lt;width&gt;20&lt;/width&gt;
              &lt;height&gt;40&lt;/height&gt;
             &lt;/size&gt;
            &lt;/property&gt;
           &lt;/spacer&gt;
          &lt;/item&gt;
         &lt;/layout&gt;
        &lt;/widget&gt;
       &lt;/widget&gt;
      &lt;/item&gt;
     &lt;/layout&gt;
    &lt;/widget&gt;
   &lt;/widget&gt;
   &lt;resources/&gt;
   &lt;connections/&gt;
  &lt;/ui&gt;
  </code></pre>