How to insert a button inside a QLineEdit

Related searches

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

For example, like this google image:

Put button and icon inside QLineEdit, I need to put an SVG icon on the left and button on the right inside QLineEdit to get something like this I sucessfully managed to add� I need to put an SVG icon on the left and button on the right inside QLineEdit to get something like this I sucessfully managed to add QToolButton on the right with following code class MyLineEdit(QLineEdit): def __init__(self, parent=None):

Here is the runnable code:

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_())

QLineEdit Class, You can change the text with setText() or insert(). This property holds whether the line edit displays a clear button when it is not empty. When the property is Qt::LogicalMoveStyle (the default), within a LTR text block, increase cursor� OPTION 1 - Multiple signals and slots. Connect all pushbuttons clicked () signal to a slot. // Let button01 be the A connect(ui->button01, SIGNAL(clicked()), this, SLOT(buttonClicked())); // Let button 60 be the Q connect(ui->button60, SIGNAL(clicked()), this, SLOT(buttonClicked()));

As of Qt 5.2 there is QLineEdit.addAction() which is a built-in way to do that. Also QLineEdit.setClearButtonEnabled() adds a cross button to the right (as on certain OSX controls) to clear the widget's contents.

Qlineedit clear button, PySide. Merged sledgehammer999 merged 2 commits into qbittorrent : Sep 18, 2014 � Add clear button inside QLineEdit widget Loading branch information;� Theres are a few ways of doing this, but one simple way that I think would work, is to add a QLabel and a QLineEdit in a QFrame, remove the borders from the QLineEdit and make its background transparent. Then set a pixmap to the label. Then you just set a frame style and the background color for your QFrame object.

Thanks to our colleague, Avaris, But his example is did not convince me and I decided to make a on another more easier to and less code. Go to learn!
#this code for example in btninlineedit.py

from PyQt4.QtGui import *
from PyQt4.QtCore import Qt
from PyQt4 import QtCore, QtGui
#Andrey Zhuk.
#####
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class ButtonInLineEdit(QLineEdit):
    def __init__(self,parent=None):
        QLineEdit.__init__(self,parent)

        self.ButtonShowKeyboard = QToolButton(self)
        self.ButtonShowKeyboard.setCursor(Qt.PointingHandCursor)
        #self.ButtonShowKeyboard.show()
        self.ButtonShowKeyboard.setFocusPolicy(Qt.NoFocus)
        self.ButtonShowKeyboard.setIcon(QtGui.QIcon("images/YourIcon.svg"))
        self.ButtonShowKeyboard.setStyleSheet("background: transparent; border: none;")

        layout = QHBoxLayout(self)
        layout.addWidget(self.ButtonShowKeyboard,0,Qt.AlignRight)

        layout.setSpacing(0)
        layout.setMargin(5)
        # ToolTip 
        self.ButtonShowKeyboard.setToolTip(QtGui.QApplication.translate("None", "Show virtual keyboard", None, QtGui.QApplication.UnicodeUTF8))


#this code for example in main.py            
class main(/////****///**/): 
    def __init__(self):
     #blablablablaaaa

        self.KeyboardShow = False

self.connect(self.LineEdit.ButtonShowKeyboard, QtCore.SIGNAL("clicked()"), self.KeyboardShowHide)


def KeyboardShowHide(self):
    try:
        if self.KeyboardShow:
            self.KeyboardShow = False
            self.WidgetKeyboard.hide()
        else:
            self.KeyboardShow = True
            self.WidgetKeyboard.show()
    except:
            debug ("ошибка при вызове функции скрытые или показа клавиатуры (Main Window)")
#this code for example in btninlineedit.py

from forms.btninlineedit import ButtonInLineEdit



        self.LineEdit = ButtonInLineEdit()

PyQt - QLineEdit Widget, PyQt - QLineEdit Widget - QLineEdit object is the most commonly used input field. It provides a Controls the appearance of the text inside the box. Echomode� QLineEdit object is the most commonly used input field. It provides a box in which one line of text can be entered. In order to enter multi-line text, QTextEdit object is required. The following table lists a few important methods of QLineEdit class − Given below are the most commonly used methods of QLineEdit.

In qt C++, I can drag and drop pushButton in the left of LineEdit. After that, I just need to set styleSheet for LineEdit with this code:

int FramWidth = lineEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);

lineEdit->setStyleSheet(QString("QLineEdit{padding-right: %1px; }").arg(ui->pushButton->sizeHint().width() + FramWidth +5));

And it works for me. Hope it can help.

QLineEdit Class Reference, The QLineEdit widget is a one-line text editor. You can change the text with setText() or insert(). menu which is shown when the user clicks on the line edit with the right mouse button. Sets the margins around the text inside the frame. A QVBoxLayout has been assigned to it. 3 elements are inserted into QVBoxLayout: QHBoxLayout, Spacer and another QHBoxLayout. In the upper QHBoxLayout on the left is a Spacer, and on the right is QLineEdit (see picture). The lower QHBoxLayout is about the same, but instead of the QLineEdit there is a button with a "+". Source:

if you want that a variable can be accessed in all parts of the class as in your case is the button_clicked method you must make it a member of the class for it you must use self when you create it.

The button will always be just as big as it needs to be. It won't stretch if the screen is wide, or shrink if the screen is narrow. The input text will not go behind the button. Caveats and Browser Support. There's limited Flexbox support in IE9, so the button will not be on the right of the form.

QLineEdit.Normal. Display characters as they are entered. This is the default. QLineEdit.NoEcho. Do not display anything. This may be appropriate for passwords where even the length of the password should be kept secret. QLineEdit.Password. Display platform-dependent password mask characters instead of the characters actually entered.

Comments
  • As of Qt 5.2 there is QLineEdit.addAction() which is a built-in way to do that. Also QLineEdit.setClearButtonEnabled() adds a cross button to the right (as on certain OSX controls) to clear the widget's contents.
  • @Avaris I have adapted your solution to my needs but there is a slight problem. When the content of the QLineEdit is too long it overlaps with the button. I managed to mitigate the problem to some extent by setting the background color of the button to white. The problem with this approach though is that part of the contents disappear behind the button. One cannot scroll to e right with the cursor or mouse to reveal the rest of the contents. How can I solve this problem? Thanks.
  • Hey there, I am doing something similar, however I am trying to append such 'button' onto the UI that I have created in Qt Designer. Was wondering if it is possible to do so using your solution?
  • Could you give an example?