Qt4: hide minor widgets while maintaining the size of the center widget

I have a QMainWindow with the main "display" widgets and a few small widgets. I would like to enable or disable secondary widgets when the mouse enters or exits a window.

I can implement this basic functionality by implementing enterEvent and leaveEvent with show / hiding calls for optional widgets. However, the default behavior of Qt4 is to leave the QMainWindow geometry fixed and resize the important widget. I would rather save the geometry of this widget and resize / resize QMainWindow if necessary. Is it possible?

Here's a simplified example in PyQt4

 from PyQt4.QtGui import * from PyQt4.QtCore import * app = QApplication(['']) class MyWidget(QWidget): def __init__(self, parent=None): super(MyWidget, self).__init__(parent) layout = QVBoxLayout() self.setLayout(layout) self.main = QPushButton("major") self.main.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.minor = QPushButton("minor") layout.addWidget(self.main) layout.addWidget(self.minor) def enterEvent(self, event): self.minor.show() def leaveEvent(self, event): self.minor.hide() mw = QMainWindow() mw.setCentralWidget(MyWidget()) mw.show() app.exec_() 

Instead of the "main" button increasing / decreasing, I would like the borders of MyWidget to change to wrap around this button.

+4
source share
2 answers

I hacked your example on Mac OS X using PySide. After spending quite a while, I found the following solution:

 from PySide.QtGui import * from PySide.QtCore import * app = QApplication(['']) class MyWidget(QWidget): def __init__(self, parent=None): super(MyWidget, self).__init__(parent) layout = QVBoxLayout() self.setLayout(layout) self.main = QPushButton("major") self.main.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.minor = QPushButton("minor") layout.addWidget(self.main) layout.addWidget(self.minor) def resizeEvent(self, event): self.appendix = self.minor.frameGeometry().height() + 2 if self.minor.isVisible(): self.varheight = mw.height() - self.appendix else: self.varheight = mw.height() def enterEvent(self, event): self.minor.show() mw.resize(mw.width(), self.varheight + self.appendix) def leaveEvent(self, event): self.minor.hide() mw.resize(mw.width(), self.varheight) mw = QMainWindow() mw.setCentralWidget(MyWidget()) mw.show() app.exec_() 

This is not ideal since the initial state does not check the position of the mouse. Also, the magic "+ 2" should probably be widget geometry computable from some other metrics. But for example, this should be good.

To implement this task, it took several hours. It is imperative to make sure that all calculations are in a fixed state. Otherwise, the linking mechanism will produce difficult to understand results or even endless loops.

resizeEvent retrieves the correct values ​​from the size of the widget, but nothing else. Then enterEvent and leaveEvent use these values ​​and resize.

My variables are not initialized. You can do this, but the first resize happens earlier than other events.

+1
source

You can try this code. Instead of subclassing QWidget, you can directly subclass QMainWindow.

 from PyQt4.QtGui import * from PyQt4.QtCore import * class MyWindow( QMainWindow ): def __init__( self, parent = None ): QMainWindow.__init__( self, parent ) self.widget = QWidget( self ) lyt = QVBoxLayout() self.main = QPushButton( "major" ) self.minor = QPushButton( "minor" ) if not self.underMouse() : self.minor.hide() self.setMaximumHeight( self.main.sizeHint().height() ) else : self.setMaximumHeight( self.minor.sizeHint().height() + self.main.sizeHint().height() ) lyt.addWidget( self.main ) lyt.addWidget( self.minor ) self.widget.setLayout( lyt ) self.setCentralWidget( self.widget ) def enterEvent( self, event ) : self.minor.show() self.setMaximumHeight( self.minor.sizeHint().height() + self.main.sizeHint().height() ) self.adjustSize() event.accept() def leaveEvent( self, event ): self.minor.hide() self.setMaximumHeight( self.main.sizeHint().height() ) self.adjustSize() event.accept() if __name__ == '__main__' : app = QApplication( [] ) Gui = MyWindow() Gui.show() app.exec_() 
0
source

Source: https://habr.com/ru/post/1438073/


All Articles