Guest User

Qt menuBar and Ubuntu Unity

a guest
Mar 26th, 2013
113
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # The following code is a severely reduced version of my application. Hopefully  
  2. # it demonstrates the following problem.                                        
  3.  
  4. # Problem:                                                                      
  5. # An extra resize event is being created after the subwindow should be fully    
  6. # realized.                                                                    
  7.  
  8. # Select New->SubWindow                                                        
  9. # The extra resize event is printed to stdout.                                  
  10. # AFAIK, this only happens on Ubuntu(12.04)/Unity 2d or 3d                      
  11.  
  12. import sys
  13. from PyQt4.QtCore import *
  14. from PyQt4.QtGui import *
  15. from PyQt4.QtCore import Qt as qt
  16.  
  17. class GView(QGraphicsView):
  18.     def __init__(self, parent=None):
  19.         super(GView, self).__init__(parent)
  20.         self.setScene(QGraphicsScene(QRectF(0, 0, 100, 100)))
  21.         self.scene().addRect(50, 50, 10, 10)
  22.         self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
  23.         self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
  24.     def fitAll(self):
  25.         r = QRectF()
  26.         for item in self.scene().items():
  27.             r = r.united(item.boundingRect())
  28.         self.fitInView(r, qt.KeepAspectRatio)
  29.     def resizeEvent(self, e):
  30.         print "resize", e.size()
  31.         super(GView, self).resizeEvent(e)
  32.  
  33.                        
  34. class AppWindow(QMainWindow):
  35.     def __init__(self, parent=None):
  36.         super(AppWindow, self).__init__(parent)
  37.         self.mdiArea = QMdiArea(self)
  38.         self.mdiArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
  39.         self.mdiArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
  40.         self.setCentralWidget(self.mdiArea)
  41.  
  42.         # Comment the following four lines and click in the main window to      
  43.         # create a new subwindow. The extra resize event is not created.        
  44.         # So is the very presence of the menuBar creating the event?            
  45.         newMenu = self.menuBar().addMenu("&New")
  46.         subWinAction = newMenu.addAction("&SubWindow")
  47.         self.connect(subWinAction, SIGNAL("triggered(bool)"),
  48.                      self.onSubWindow)
  49.  
  50.         self.statusBar()
  51.     def onSubWindow(self, b=False):
  52.         view = GView()
  53.         self.addSubWindow(view)
  54.         view.fitAll()
  55.         print "no resize should follow this comment"
  56.     def addSubWindow(self, view):
  57.         self.mdiArea.addSubWindow(view)
  58.         view.setAttribute(Qt.WA_DeleteOnClose)
  59.         subWin = self.mdiArea.currentSubWindow()
  60.         if subWin and not subWin.isMaximized():
  61.             view.show()
  62.         else:
  63.             view.showMaximized()
  64.     def mousePressEvent(self, e):
  65.         self.onSubWindow()
  66.  
  67.  
  68. if __name__ == '__main__':
  69.     app = QApplication(sys.argv)
  70.     mainWin = AppWindow()
  71.     mainWin.show()
  72.     app.exec_()
RAW Paste Data