pyqt5 dialog 顯示無框窗口,點擊非窗口區,dialog會自動關閉,QLineEdit彈出下拉框,自動補齊內容

pyqt5 dialog 顯示無框窗口,點擊非窗口區,dialog會自動關閉
QLineEdit 自動補齊,顯示下拉框
使用QT.Popup會導致QLineedit無法輸入中文,所以使用QT.Tool窗口屬性,然後在事件中處理窗口deactive事件

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QKeySequence, QPixmap
from PyQt5.QtCore import Qt, QEvent


class FindWdgt(QDialog):
    Internal_Widget_Height = 30
    def __init__(self, List, parent=None):
        super(FindWdgt, self).__init__(parent)
        self.setup_Widgets(List)
        self.setup_SinglConnection()
        self.setup_Layout()
        self.setup_Appearence()
        self.setup_cfg()

    def setup_cfg(self):
        self.installEventFilter(self)
        self.hide()


    def setup_Appearence(self):

        self.setWindowIcon(QIcon(FindIcon_Path))#設置窗口圖標
        self.setWindowFlags( Qt.FramelessWindowHint| Qt.Tool) #使用QT.Popup會QLineEdit導致無法輸入中文
        self.setStyleSheet('QDialog{background-color: rgb(255,162,108);font-size:40px}')
    def setup_Widgets(self, List):
        self.Find_LineEdit  = QLineEdit(self)
        self.IconLabel      = QLabel(self)
        self.ExitBtn        = QPushButton( "退出", self)
        self.Completer      = QCompleter(List)

        self.ExitBtn.setFixedSize(QSize(60, self.Internal_Widget_Height))

        self.IconLabel.setPixmap(QPixmap(FindPNG_Path))#設置label顯示圖片

        #設置QLineEdit字體格式
        self.Find_LineEdit.setPlaceholderText("輸入搜索內容")
        self.Find_LineEdit.setFixedSize(QSize(300, self.Internal_Widget_Height))
        self.Find_LineEdit.setCompleter(self.Completer)
        self.Find_LineEdit.setFont(QFont( "微軟雅黑" , 13) )

    def setup_Layout(self):
        """
        設置佈局
        """
        self.HBoxLayout     = QHBoxLayout(self)

        self.HBoxLayout.addWidget(self.IconLabel)
        self.HBoxLayout.addWidget(self.Find_LineEdit)
        self.HBoxLayout.addWidget(self.ExitBtn)
        self.HBoxLayout.setContentsMargins(7, 7, 7, 7)#...(int left, int top, int right, int bottom)
        self.HBoxLayout.setSpacing(2)

        self.setLayout(self.HBoxLayout)
    def setup_SinglConnection(self):
        """
        信號連接
        """
        self.ExitBtn.clicked.connect(self.close)

    def eventFilter(self, obj, event):
        """
        事件過濾
        """
        if event.type() == QEvent.WindowDeactivate:
            # print("WindowDeactivate")
            self.close()  #點擊其他程序窗口,會關閉該對話框
            return True
        else:
            return super(FindWdgt, self).eventFilter(obj, event)

    def Show_Handler(self):
        self.show()
        self.activateWindow()#激活窗口,這樣在點擊母窗口的時候,eventFilter裏會捕獲到WindowDeactivate事件,從而關閉搜索窗口
        self.Find_LineEdit.setFocus()
        self.Find_LineEdit.clear()

if __name__ == '__main__':

    app         = QtWidgets.QApplication(sys.argv)
    List        = ['1', '111', '1234', '22', '345', '678']
    widget      = QWidget()
    Find        = FindWdgt(List, widget)
    Btn         = QPushButton("點擊彈出", widget)
    Btn.clicked.connect(Find.Show_Handler)
    Btn.resize(QSize(100, 50))
    widget.resize(QSize(300, 150))
    widget.show()
    sys.exit(app.exec_())

實現該功能,搜索了許多文章,具體忘記哪些鏈接了,再此謝謝提供幫助的那些文章!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章