How to avoid to get special characters on QInputDialog? [duplicate] - python

I am writing a Python Application where the user can enter a String in an QInputDialog. How can i use the QCompleter to make Inputs easier?
I've already been searching on different websites and read the doc from
https://doc.qt.io/qt-5/qcompleter.html#details
but couldn't find any help for this problem.
To me, it seems like the QCompleter is only available for QLineEdit and QComboBox. (Please proof me wrong)
ian, okPressed = QInputDialog.getText(self, "IAN", "Please enter IAN:")
It would help me a lot if anyone could show me some code examples how to deal with this problem.
If it's not possible to use the QCompleter within the QInputDialog, do you guys have an idea for a workaround?
Much thanks =)

There are 2 possible solutions:
Get the QInputDialog through the parent-child relationship between the widgets using findChild():
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
button = QtWidgets.QPushButton("Press me", clicked=self.onClicked)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(button)
#QtCore.pyqtSlot()
def onClicked(self):
QtCore.QTimer.singleShot(0, self.onTimeout)
ian, okPressed = QtWidgets.QInputDialog.getText(
self, "IAN", "Please enter IAN:"
)
#QtCore.pyqtSlot()
def onTimeout(self):
dialog = self.findChild(QtWidgets.QInputDialog)
if dialog is not None:
le = dialog.findChild(QtWidgets.QLineEdit)
if le is not None:
words = ["alpha", "omega", "omicron", "zeta"]
completer = QtWidgets.QCompleter(words, le)
le.setCompleter(completer)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.resize(320, 240)
w.show()
sys.exit(app.exec_())
Do not use the static method and create the QInputDialog with the same elements:
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
button = QtWidgets.QPushButton("Press me", clicked=self.onClicked)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(button)
#QtCore.pyqtSlot()
def onClicked(self):
dialog = QtWidgets.QInputDialog(self)
dialog.setWindowTitle("IAN")
dialog.setLabelText("Please enter IAN:")
dialog.setTextValue("")
le = dialog.findChild(QtWidgets.QLineEdit)
words = ["alpha", "omega", "omicron", "zeta"]
completer = QtWidgets.QCompleter(words, le)
le.setCompleter(completer)
ok, text = (
dialog.exec_() == QtWidgets.QDialog.Accepted,
dialog.textValue(),
)
if ok:
print(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.resize(320, 240)
w.show()
sys.exit(app.exec_())

Related

How to make the tool buttons in a toolbar only select one at a time?

I have five tool buttons in a QToolbar via toolbar.addAction() and if I choose one of them I would like to make the others not looking checked.
Example: If I choose the 'A' button and the program is now in the 'A' mode, others can't work.
You have to use a QActionGroup, you must also activate the checkable property of the QAction:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class ToolDemo(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(ToolDemo, self).__init__(parent)
self.setWindowTitle("toolbar demo")
toolbarBox = QtWidgets.QToolBar(self)
toolbarBox.setFixedWidth(180)
self.addToolBar(QtCore.Qt.RightToolBarArea, toolbarBox)
vscode_action = QtWidgets.QAction("VSCode", self, checkable=True)
ptt_action = QtWidgets.QAction("Ppt", self, checkable=True)
word_action = QtWidgets.QAction("Word", self, checkable=True)
excel_action = QtWidgets.QAction("Excel", self, checkable=True)
other_action = QtWidgets.QAction("other", self, checkable=True)
group = QtWidgets.QActionGroup(self, exclusive=True)
for action in (
vscode_action,
ptt_action,
word_action,
excel_action,
other_action,
):
toolbarBox.addAction(action)
group.addAction(action)
def main():
app = QtWidgets.QApplication(sys.argv)
ex = ToolDemo()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Update:
If you want to use buttons like QCheckBox, QRadioButton, QPushButton and QToolButton are exclusive in the QToolBar then you must use QButtonGroup:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class ToolDemo(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(ToolDemo, self).__init__(parent)
self.setWindowTitle("toolbar demo")
toolbarBox = QtWidgets.QToolBar(self)
toolbarBox.setFixedWidth(180)
self.addToolBar(QtCore.Qt.RightToolBarArea, toolbarBox)
vscode_button = QtWidgets.QCheckBox(self, text="VSCode", checkable=True)
ptt_button = QtWidgets.QCheckBox(self, text="Ppt", checkable=True)
word_button = QtWidgets.QRadioButton(self, text="Word", checkable=True)
excel_button = QtWidgets.QPushButton(self, text="Excel", checkable=True)
other_button = QtWidgets.QToolButton(self, text="other", checkable=True)
group = QtWidgets.QButtonGroup(self, exclusive=True)
for button in (
vscode_button,
ptt_button,
word_button,
excel_button,
other_button,
):
toolbarBox.addWidget(button)
group.addButton(button)
def main():
app = QtWidgets.QApplication(sys.argv)
ex = ToolDemo()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

Show close button (x) after hiding it (QTabBar)

I'm creating a method to hide and show the close button of a tab. I found a way to hide it. However, I don't know how to do it in reverse.
This is my existing code for hiding the close button. Using the same lines of codes, how can I show the close button of the tab?
def disable_close_button(self):
self.ui.tab_widget.tabBar().setTabButton(self.current_index(), QTabBar.RightSide, None)
def enable_close_button(self):
pass
Thanks in advance!
You are not hiding the button, you are eliminating it. So in my solution I get the button and then I hide it or show it as needed.
import sys
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
show_button = QtWidgets.QPushButton(
text="show",
clicked=self.enable_close_button
)
hide_button = QtWidgets.QPushButton(
text="hide",
clicked=self.disable_close_button
)
self.tab_widget = QtWidgets.QTabWidget(tabsClosable=True)
for i in range(4):
label = QtWidgets.QLabel(
text="label {}".format(i),
alignment=QtCore.Qt.AlignCenter
)
self.tab_widget.addTab(label , "tab-{}".format(i))
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(show_button)
lay.addWidget(hide_button)
lay.addWidget(self.tab_widget)
#QtCore.pyqtSlot()
def enable_close_button(self):
ix = self.tab_widget.currentIndex()
button = self.tab_widget.tabBar().tabButton(ix, QtWidgets.QTabBar.RightSide)
if button is not None:
button.show()
#QtCore.pyqtSlot()
def disable_close_button(self):
ix = self.tab_widget.currentIndex()
button = self.tab_widget.tabBar().tabButton(ix, QtWidgets.QTabBar.RightSide)
if button is not None:
button.hide()
if __name__ == '__main__':
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
app.setStyle("fusion")
w = Widget()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

How to record a pressed key combination in the PyQT5 dialog window

I open the dialog from the main window, where by clamping the keys, I fill the line with their names. The problem is that I can not understand where you need to do a cycle of checking all the keys on their state. Maybe there is another way to get the keys pressed? Or where you need to listen to the clamping so that the dialog box does not hang and the string is updated.
MainWindow:
def showBindings(self, param):
from dialogs import KeyBindingsDialog
self.dialog = KeyBindingsDialog()
self.dialog.show()
Dialog:
class KeyBindingsDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(KeyBindingsDialog, self).__init__(parent)
self.ui = KeyBindings()
self.ui.setupUi(self)
Use QKeySequenceEdit:
from PyQt5 import QtCore, QtGui, QtWidgets
class KeySequenceEdit(QtWidgets.QKeySequenceEdit):
def keyPressEvent(self, event):
super(KeySequenceEdit, self).keyPressEvent(event)
seq_string = self.keySequence().toString(QtGui.QKeySequence.NativeText)
if seq_string:
last_seq = seq_string.split(",")[-1].strip()
le = self.findChild(QtWidgets.QLineEdit, "qt_keysequenceedit_lineedit")
self.setKeySequence(QtGui.QKeySequence(last_seq))
le.setText(last_seq)
self.editingFinished.emit()
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self._keysequenceedit = KeySequenceEdit(editingFinished=self.on_editingFinished)
button = QtWidgets.QPushButton("clear", clicked=self._keysequenceedit.clear)
hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(self._keysequenceedit)
hlay.addWidget(button)
#QtCore.pyqtSlot()
def on_editingFinished(self):
sequence = self._keysequenceedit.keySequence()
seq_string = sequence.toString(QtGui.QKeySequence.NativeText)
print("sequence: ", seq_string)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

Why does this code in PyQt allow me to leave all radio buttons unchecked?

I wrote a mini code using PyQt and I got surprised when I noticed that it allows me to leave all buttons on the left unchecked. I don't want this behaviour: one of them should always be selected. The right side of the window works, though. I don't really know why. Here's the code:
from PyQt5 import QtWidgets, QtGui, QtCore
class Win(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
top_layout = QtWidgets.QHBoxLayout()
top_layout_1 = QtWidgets.QHBoxLayout()
self.thresh_btns = [QtWidgets.QRadioButton('L1'),
QtWidgets.QRadioButton('L2'),
QtWidgets.QRadioButton('L3')]
self.thresh_btns[0].setChecked(True)
for btn in self.thresh_btns:
top_layout_1.addWidget(btn)
timestamp_groupbox = QtWidgets.QGroupBox('Timestamp')
top_layout_2 = QtWidgets.QVBoxLayout()
self.timestamp_current = QtWidgets.QRadioButton('Current')
self.timestamp_current.setChecked(True)
self.timestamp_target = QtWidgets.QRadioButton('Last')
top_layout_2.addWidget(self.timestamp_current)
top_layout_2.addWidget(self.timestamp_target)
timestamp_groupbox.setLayout(top_layout_2)
top_layout.addLayout(top_layout_1)
top_layout.addWidget(timestamp_groupbox)
self.setLayout(top_layout)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
ex = Win()
ex.show()
app.exec_()
If you want this behavior you have to use a QButtonGroup:
from PyQt5 import QtCore, QtGui, QtWidgets
class Win(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
top_layout = QtWidgets.QHBoxLayout()
top_layout_1 = QtWidgets.QHBoxLayout()
self.thresh_btns = [QtWidgets.QRadioButton('L1'),
QtWidgets.QRadioButton('L2'),
QtWidgets.QRadioButton('L3')]
group_button = QtWidgets.QButtonGroup(self) # <---
self.thresh_btns[0].setChecked(True)
for btn in self.thresh_btns:
top_layout_1.addWidget(btn)
group_button.addButton(btn) # <---
timestamp_groupbox = QtWidgets.QGroupBox('Timestamp')
top_layout_2 = QtWidgets.QVBoxLayout()
self.timestamp_current = QtWidgets.QRadioButton('Current')
self.timestamp_current.setChecked(True)
self.timestamp_target = QtWidgets.QRadioButton('Last')
top_layout_2.addWidget(self.timestamp_current)
top_layout_2.addWidget(self.timestamp_target)
timestamp_groupbox.setLayout(top_layout_2)
top_layout.addLayout(top_layout_1)
top_layout.addWidget(timestamp_groupbox)
self.setLayout(top_layout)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
ex = Win()
ex.show()
sys.exit(app.exec_())

PyQt 5 call a function without clicking in python

Hi guys im new in python and PyQt already im using PyQt 5 lib and already faced with really primary problem.
I want to call print function (or any other functions like this) with just cursor moving no clicking on that.
some thing like tooltips that not needed to click on any button.
Thank you for your help.
There are several ways to implement:
1. customized button.
from PyQt5 import QtCore, QtWidgets
class HoverButton(QtWidgets.QPushButton):
hovered = QtCore.pyqtSignal()
def enterEvent(self, event):
self.hovered.emit()
super(HoverButton, self).enterEvent(event)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
btn = HoverButton("Press me")
btn.hovered.connect(self.onHovered)
lay.addWidget(btn)
lay.addWidget(QtWidgets.QLineEdit())
def onHovered(self):
print("hovered")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
2. eventFilter
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
self.btn = QtWidgets.QPushButton("Press me")
self.btn.installEventFilter(self)
lay.addWidget(self.btn)
lay.addWidget(QtWidgets.QLineEdit())
def eventFilter(self, obj, event):
if obj == self.btn and event.type() == QtCore.QEvent.HoverEnter:
self.onHovered()
return super(Widget, self).eventFilter(obj, event)
def onHovered(self):
print("hovered")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

Categories