How to insert a button inside a QLineEdit

前端 未结 6 967
悲&欢浪女
悲&欢浪女 2020-12-09 06:45

I need help inserting a button inside in a QLineEdit that can call a function.

For example, like this google image:

6条回答
  •  半阙折子戏
    2020-12-09 07:25

    Here is the runnable code:

    enter image description here

    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    from sys import argv, exit
    
    class ButtonInLineEdit(QLineEdit):
        def __init__(self,parent=None):
            QLineEdit.__init__(self,parent)
    
            self.ButtonShowKeyboard = QToolButton(self)
            self.ButtonShowKeyboard.setCursor(Qt.PointingHandCursor)
    
            self.ButtonShowKeyboard.setFocusPolicy(Qt.NoFocus)
            self.ButtonShowKeyboard.setIcon(QIcon("icons/myIcon.png"))
            self.ButtonShowKeyboard.setStyleSheet("background: transparent; border: none;")
    
            layout = QHBoxLayout(self)
            layout.addWidget(self.ButtonShowKeyboard,0,Qt.AlignRight)
    
            layout.setSpacing(0)
            layout.setMargin(5)
    
            self.ButtonShowKeyboard.setToolTip(QApplication.translate("None", "Show virtual keyboard", None, QApplication.UnicodeUTF8))
    
    def MyFunction(arg=None):
        print "MyFunction() called: arg = %s"%arg
    
    a=QApplication(argv)
    LineEdit = ButtonInLineEdit()
    LineEdit.connect(LineEdit.ButtonShowKeyboard, SIGNAL("clicked()"), MyFunction)
    LineEdit.show()
    exit(a.exec_())
    

提交回复
热议问题