2016-08-18 309 views
1

我一直在試圖建立一個有動態邊欄的應用程序。我正在使用qtdesigner和pyqt5。我嘗試了兩種方法:如何構建在鼠標懸停在pyqt上顯示的側欄菜單?

1)使用樣式表連接盤旋。但側邊欄(框架)上有按鈕。碰巧,當我看到酒吧時,我只能看到有鼠標的按鈕。

stylesheetcode:

QFrame { 
        background-color: rgba(255, 151, 231, 0); 
      } 

QFrame:hover { 
       background-color:rgba(255, 151, 231, 0.45) 
      } 

QPushButton { 
       color: rgba(0, 0, 0, 0); 
       background-color: rgba(255, 151, 231, 0); 
      } 

QPushButton:hover { 
       background-color:rgba(255, 151, 231, 0.45); 
      } 

輸出:(鼠標移動到按鈕上)

enter image description here

我知道爲什麼會發生,但我不知道如何解決它。

2)使用事件

使用活動中,我能達到我想要的,但我不知道如果我做了正確的THIG。

class TelaPrincipal(QDialog, QMainWindow, Ui_Form): 
    def __init__(self, parent=None): 
     super(TelaPrincipal, self).__init__(parent) 
     QDialog.__init__(self, parent) 
     Ui_Form.__init__(self) 
     self.setupUi(self) 

     # -- Barra lateral esconde esconde -- 
     # instala esse EventFilter 
     qApp.installEventFilter(self) 
     QtCore.QTimer.singleShot(0, self.frame.hide) 

    def eventFilter(self, source, event): 
     # do not hide frame when frame shown 
     if qApp.activePopupWidget() is None: 
      if event.type() == QtCore.QEvent.MouseMove: 
       if self.frame.isHidden(): 
        self.frame.show() 
        rect = self.geometry() 
        # set mouse-sensitive zone 
        rect.setHeight(25) 
        if rect.contains(event.globalPos()): 
         self.frame.show() 
       else: 
        rect = QtCore.QRect(
         self.frame.mapToGlobal(QtCore.QPoint(0, 0)), 
         self.frame.size()) 
        if not rect.contains(event.globalPos()): 
         self.frame.hide() 
      elif event.type() == QtCore.QEvent.Leave and source is self: 
       self.frame.hide() 
     return QMainWindow.eventFilter(self, source, event) 

if __name__ == "__main__": 
    import sys 
    app = QtWidgets.QApplication(sys.argv) 
    main = TelaPrincipal() 
    main.show() 
    sys.exit(app.exec()) 

你有什麼建議我?

+0

那麼你有沒有解決這個使用事件? – xyres

+0

@xyres。它工作得很好。但我仍然想知道如果我能用css解決它。關於這個代碼,而不是設置矩形高,我將使用高幀。我也讀過關於使用QPropertyAnimation,但我沒有深入.. –

+0

好吧,當鼠標懸停在框架上時,您可以更改所有按鈕的樣式表。但你仍然需要使用鼠標懸停的事件('enterEvent')。 – xyres

回答

1

花了一點時間閱讀文檔。我發現最簡單的方法是:

在類init中我安裝了一個過濾器,然後我覆蓋了eventFilter方法。

class MainScreen(QMainWindow, QDialog, Ui_TelaPrincipal): 
    def __init__(self, parent=None): 
     super(MainScreen, self).__init__(parent) 
     QDialog.__init__(self, parent) 
     QMainWindow.__init__(self, parent) 
     Ui_MainScreen.__init__(self) 
     self.setupUi(self) 

     # ---- side bar ---- # 
     # installs a filter event 
     qApp.installEventFilter(self) 
     # hide the bar 
     QTimer.singleShot(0, self.plano_barra.hide) 

     self.plano_central.setMouseTracking(1) 
     self.plano_botao_barra.setMouseTracking(1) 
     self.setMouseTracking(1) 
     self.plano_principal.setMouseTracking(1) 


    def eventFilter(self, source, event): 
     if event.type() == QEvent.MouseMove: 
      if source == self.plano_botao_barra: 
       print("i am over the button") 
       self.plano_barra.show() 

      if source == self.plano_central: 
       print("i am at the main plan") 
       self.plano_barra.hide() 

     return QMainWindow.eventFilter(self, source, event) 
+0

如果使用'super',你不需要調用所有基類的'__init__'(你也應該在事件過濾器中使用'super')。從'QMainWindow'和'QDialog'繼承也是錯誤的,所以請去掉後者。 – ekhumoro

相關問題