I need help inserting a button inside in a QLineEdit that can call a function.
For example, like this google image:
Below is a nearly direct translation of the Qt code from here.
Differences:
button is always visible
clicking on the button emits buttonClicked(bool) signal
Code:
from PyQt4 import QtGui, QtCore
class ButtonLineEdit(QtGui.QLineEdit):
buttonClicked = QtCore.pyqtSignal(bool)
def __init__(self, icon_file, parent=None):
super(ButtonLineEdit, self).__init__(parent)
self.button = QtGui.QToolButton(self)
self.button.setIcon(QtGui.QIcon(icon_file))
self.button.setStyleSheet('border: 0px; padding: 0px;')
self.button.setCursor(QtCore.Qt.ArrowCursor)
self.button.clicked.connect(self.buttonClicked.emit)
frameWidth = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth)
buttonSize = self.button.sizeHint()
self.setStyleSheet('QLineEdit {padding-right: %dpx; }' % (buttonSize.width() + frameWidth + 1))
self.setMinimumSize(max(self.minimumSizeHint().width(), buttonSize.width() + frameWidth*2 + 2),
max(self.minimumSizeHint().height(), buttonSize.height() + frameWidth*2 + 2))
def resizeEvent(self, event):
buttonSize = self.button.sizeHint()
frameWidth = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth)
self.button.move(self.rect().right() - frameWidth - buttonSize.width(),
(self.rect().bottom() - buttonSize.height() + 1)/2)
super(ButtonLineEdit, self).resizeEvent(event)
Usage:
import sys
from PyQt4 import QtGui
def buttonClicked():
print 'You clicked the button!'
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = ButtonLineEdit('/path/to/my_fancy_icon.png')
main.buttonClicked.connect(buttonClicked)
main.show()
sys.exit(app.exec_())
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_())
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.
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()
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.
class LineEditFileDialogWidget(QtWidgets.QLineEdit):
def __init__(self, parent=None):
super(LineEditFileDialogWidget, self).__init__(parent)
self.setReadOnly(True)
icon = QtWidgets.QApplication.style().standardIcon(QtWidgets.QStyle.SP_DirIcon)
self.action = self.addAction(icon, QtWidgets.QLineEdit.TrailingPosition)
self.action.triggered.connect(some function)
Here is an example of using the icon along with QLineEdit
Related
Actually, my question is the same as this question but the answer didn't help too much, tried the same but didn't work.
I want to make a slider works in the tray but what I got is a blank field only.
Is it possible to accomplish this in PyQt5? I tried a lot of things but couldn't find a solution. If it is not possible in PyQt5 then which else library can I use for this?
My code:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (
QSlider,
QHBoxLayout,
QLabel,
QApplication,
QWidgetAction,
)
from PyQt5.QtCore import Qt
def change_sound(value):
print(value)
class SliderWidgetAction(QFrame):
pass
class SliderAction(QWidgetAction):
def __init__(self, label="", parent=None):
QWidgetAction.__init__(self, parent)
self._widget = SliderWidgetAction(parent)
self._label = QLabel(label, self._widget)
self._slider = QSlider(Qt.Horizontal, self._widget)
self._slider.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.value_changed = self._slider.valueChanged
def widget(self):
return self._widget
def label(self):
return self._label
def slider(self):
return self._slider
def createWidget(self, menu):
"""
This method is called by the QWidgetAction base class.
"""
actionWidget = self.widget()
actionLayout = QHBoxLayout(actionWidget)
actionLayout.setContentsMargins(0, 0, 0, 0)
actionLayout.addWidget(self.label())
actionLayout.addWidget(self.slider())
actionWidget.setLayout(actionLayout)
return actionWidget
app = QApplication([])
app.setQuitOnLastWindowClosed(False)
icon = QIcon("icon.png")
tray = QSystemTrayIcon()
tray.setIcon(icon)
tray.setVisible(True)
menu = QMenu()
action = SliderAction("Test", menu)
action.slider().setMinimum(10)
action.slider().setMaximum(100)
action.slider().setValue(90)
action.slider().valueChanged.connect(change_sound)
label_widget = QLabel("Hello World")
action.setDefaultWidget(label_widget)
menu.addAction(action)
tray.setContextMenu(menu)
app.exec_()
I have written the below code to which I finally managed to add menu but connecitn menu to a function doesnt seem to work:
import os
from PyQt5 import uic
from PyQt5 import QtWidgets
from PyQt5 import QtCore
FILE_LOCATION = os.path.dirname(os.path.realpath(__file__))
class MainDialogWindow(QtWidgets.QDialog):
def __init__(self):
super(MainDialogWindow,self).__init__()
ui_file = os.path.join(FILE_LOCATION, "example.ui")
self._ui = uic.loadUi(ui_file, self)
self.registerCallbacks()
self.initUI()
def initUI(self):
"""Initialize the UI.
"""
self.textBrowser.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
def registerCallbacks(self):
self.textBrowser.customContextMenuRequested.connect(self.context_menu)
# self.connect(self.textBrowser, QtCore.Signal('customContextMenuRequested(const QPoint &)'), self.context_menu)
def context_menu(self, pos):
menu = QtWidgets.QMenu(self)
action = menu.addAction("clear")
menu.exec_(self.mapToGlobal(pos))
action.trigered.connect(self.clear)
def clear(self):
"""Slot to claer text.
"""
print("clear")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainDialogWindow()
window.show()
window.setGeometry(500, 300, 300, 300)
sys.exit(app.exec_())
please helpp,, I want call the clear function from the right click menu
I don't seem to understand how the menu.exec_() method works, that method blocks the execution of sequential tasks until the user selects a QAction from the QMenu. In your case, for example, until when you press "clear" and the triggered signal is emitted (note: you have a typo), but at that moment there is no connection, so the clear method will not be called. The solution is to make the connection before invoking the QMenu exec_():
def context_menu(self, pos):
menu = QtWidgets.QMenu(self)
action = menu.addAction("clear")
action.triggered.connect(self.clear)
menu.exec_(self.mapToGlobal(pos))
I am trying to design data input form that will add text to a label when the text is entered into a line edit widget when the ok button is pressed, but the answer is completely eluding me:
#!/usr/bin/python3
#-*- coding: utf-8 -*-
"""
Set label text from line edit with
ok click
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.qle = QtGui.QLineEdit(self)
self.qle.move(100, 0)
sometext = self.qle.text
self.lbl = QtGui.QLabel(self)
self.lbl.move(100, 100)
btn = QtGui.QPushButton("Ok", self)
btn.move(30, 100)
btn.clicked.connect(self.buttonClicked)
self.setGeometry(200, 200, 300, 200)
self.show
def buttonClicked(self, sometext):
sender = self.sender()
self.lbl.setText(sometext)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
How do you get this to work please?
You can use this to set text to a label: QLabel.setText("string text")
Combined with: QPushButton.clicked.connect(lambda: func())
And get the text in the Line Edit: QLineEdit.text()
So complete code should look something like:
#!/usr/bin/python3
#-*- coding: utf-8 -*-
"""
Set label text from line edit with
ok click
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.qle = QtGui.QLineEdit(self)
self.qle.move(100, 0)
sometext = self.qle.text
self.lbl = QtGui.QLabel(self)
self.lbl.move(100, 100)
btn = QtGui.QPushButton("Ok", self)
btn.move(30, 100)
self.setGeometry(200, 200, 300, 200)
self.show
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Example()
def setLabelText():
text = qle.text()
ex.lbl.setText(text)
ex.btn.clicked.connect(lambda: setLabelText())
sys.exit(app.exec_())
I can't actually test this code, but it should work fine.
Tell me if you have any problems, and please give us a traceback.
You might also want to consider moving to PyQt5.6 (Slightly older than the newest, but it has the QtDesigner built in. A quick google search should find you the right executable installer, it's not on their website anymore. If you can't find it I can send it to you.)
Hope it works, and wish you luck!
Edit:
I just thought of a simpler way. Should have just said this.
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.btn.clicked.connect(lambda: ex.lbl.setText(qle.text()))
sys.exit(app.exec_())
A bit of a simplified answer. Did some edits to your code. It works fine, and runs successfully.
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.qle = QtGui.QLineEdit(self)
self.qle.move(10, 10)
self.lbl = QtGui.QLabel(self)
self.lbl.setGeometry(10, 55,200,20)
self.lbl.setText("Type Something and Press Ok!")
btn = QtGui.QPushButton("Ok", self)
btn.move(10, 100)
btn.clicked.connect(self.buttonClicked)
self.setGeometry(200, 200, 300, 200)
self.show
def buttonClicked(self):
sender = self.sender()
self.lbl.setText(self.qle.text())
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Also you QLabel was invisible because it was out of your QMainWindow area.
Hope it Helps!
For past few days I was trying to solve the issue of widget movement. At some point I tried rewriting QComboBox classes with mouse signals but that did not work. As a work around I settled for parenting my widget to a QGraphicsWidget but once I try to add another item it does not display any more and I'm not sure what to do. Here is full test script:
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QApplication,QGraphicsItem, QGraphicsView, QGraphicsScene, QDesktopWidget, QCheckBox, QGroupBox, QPushButton, QGridLayout, QLabel, QLineEdit, QComboBox, QFont, QRadioButton, QButtonGroup, QWidget, QShortcut, QKeySequence, QIcon, QListView, QStandardItemModel, QStandardItem, QAction, QIntValidator, QListWidget, QProgressBar, QSpacerItem
from PyQt4.QtCore import QRect
from functools import partial
import sys
class node_GUI(QtGui.QWidget):
def __init__(self):
super(node_GUI, self).__init__()
class Main(QtGui.QMainWindow):
def __init__(self, *args):
super(Main, self).__init__(*args)#QtGui.QMainWindow.__init__(self)
self.init_defaults()
def init_defaults(self):
self.setGeometry(800,800,500,200)
self.lay_main = QGridLayout()
self.centralwidget = QtGui.QWidget()
self.centralwidget.setLayout(self.lay_main)
self.setCentralWidget(self.centralwidget)
btn_create_node = QPushButton("Create Node View")
btn_create_node.clicked.connect(self.create_node_view)
self.lay_main.addWidget(btn_create_node)
def showWindow(self,window):
window.show()
def printTest(self):
print "Start"
box = QGroupBox("subWidget")
box_btn = QPushButton("Test")
box_btn.clicked.connect(self.printTest)
le_edit = QLineEdit()
lay = QGridLayout()
box.setLayout(lay)
lay.addWidget(box_btn)
lay.addWidget(le_edit)
area = QtGui.QGraphicsWidget()
area.setMinimumSize(QtCore.QSizeF(400,300))
area.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
area.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
proxy = self.scene.addWidget(box)
proxy.setParentItem(area)
print "END"
def create_node_view(self):
print "creting node view"
window = node_GUI()
window.setGeometry(QRect(100, 100, 400, 200))
window.setWindowTitle("node ")
window.setObjectName("node")
show_window = QPushButton("Show Node Editor")
show_window.setObjectName("btn")
show_window.clicked.connect(partial(self.showWindow,window))
self.lay_main.addWidget(show_window)
box = QGroupBox("Widgets")
box_btn = QPushButton("Test")
box_btn.clicked.connect(self.printTest)
le_edit = QLineEdit()
lay = QGridLayout()
box.setLayout(lay)
lay.addWidget(box_btn)
lay.addWidget(le_edit)
area = QtGui.QGraphicsWidget()
area.setMinimumSize(QtCore.QSizeF(300,300))
area.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
area.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
area.setAutoFillBackground(True)
ecs = QtGui.QGraphicsEllipseItem()
ecs.setRect(QtCore.QRectF(79,79,79,79))
ecs.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
ecs.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
view = QGraphicsView()
self.scene = QGraphicsScene()
self.scene.addItem(area)
proxy = self.scene.addWidget(box)
proxy.setParentItem(area)
self.scene.addItem(ecs)
view.setScene(self.scene)
lay_window = QGridLayout()
window.setLayout(lay_window)
lay_window.addWidget(view)
def main():
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
When you click on Create Node View > Show Node Editor > Test button > a new GroupBox should appear but that does not work. Not sure why.
Right so I stopped using QGraphicsWidget() and instead I just use QGraphicsRectItem(ecs for example) once I did that change everything started to work as expected.
In the following example, when you click the button, the entire form is rebuilt adding a new label each time. At the end is a resize call that doesn't appear to work. While debugging, I validated the sizeHint() is returning the correct dimensions, and internally the widget thinks it is the correct size, but what is drawn is not correct. What can I do to force the MDI window to resize correctly? Also of note, when not sized correctly, if you manually start resizing, it suddenly snaps to the appropriate size.
import sys
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import PyQt4.Qt
class MdiWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.count = 0
self.buildWindow()
def buildWindow(self):
main = QVBoxLayout()
button = QPushButton('Change Count')
button.clicked.connect(self.changeCount)
main.addWidget(button)
for i in range(self.count):
main.addWidget(QLabel(str(i)))
widget = QWidget()
widget.setLayout(main)
self.setCentralWidget(widget)
self.resize(main.sizeHint())
def changeCount(self, event):
self.count += 1
self.buildWindow()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Resize Test')
self.mdiArea = QMdiArea()
self.setCentralWidget(self.mdiArea)
child = MdiWindow()
self.mdiArea.addSubWindow(child)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())