Python - How to keep a QComboBox's QLineEdit from adding new items - python

Whenever you add a basic QLineEdit to a QComboBox, if you type anything into the QComboBox and press [Enter], the QLineEdit text will get added to the QComboBox.
Just to be clear, I still want to have the ability to manage the QComboBox items (add, remove, etc) like normal. In this case, only QLineEdit is not allowed to add new items.
I'd very much prefer a solution that did not require subclassing a built-in QLineEdit/QComboBox
Does anyone know how to do this?
import functools
import sys
# Use PySide/PyQt. Doesn't matter
from Qt import QtWidgets
def print_items(combo_box):
print([combo_box.itemText(index) for index in range(combo_box.count())])
def main():
app = QtWidgets.QApplication([])
window = QtWidgets.QComboBox()
line_edit = QtWidgets.QLineEdit()
line_edit.editingFinished.connect(functools.partial(print_items, window))
window.setLineEdit(line_edit)
window.show()
sys.exit(app.exec_())
# start writing in the QLineEdit and press [Enter].
# The item is now in the QComboBox and print in the terminal
if __name__ == '__main__':
main()

Related

PyQt5 buttons not connecting

I am trying to build a simple GUI using PyQT5, with 3 buttons to open file browsers and one more to run processing with the selected files, but I can't get my buttons to connect to the functions needed to carry this out.
In the Ctrl class, the _connect_signals function doesn't seem to be calling _input_select. Can anyone help me figure out why?
import sys
# Import QApplication and the required widgets from PyQt5.QtWidgets
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QFileDialog
# Create a subclass of QMainWindow to setup the calculator's GUI
class UI(QMainWindow):
"""App's View (GUI)."""
def __init__(self):
"""View initializer."""
super().__init__()
# Set some main window's properties
self.setFixedSize(300, 150)
# Set the central widget and the general layout
self.generalLayout = QVBoxLayout()
self._centralWidget = QWidget(self)
self.setCentralWidget(self._centralWidget)
self._centralWidget.setLayout(self.generalLayout)
# Create the buttons
self._create_buttons()
def _create_buttons(self):
"""Create the buttons."""
self.buttons = {}
buttons_layout = QVBoxLayout()
# Button text | position on the QVBoxLayout
buttons = {
"Select input file...": 0,
"Select config file...": 1,
"Select output file...": 2,
"Run": 3,
}
# Create the buttons and add them to the grid layout
for btnText, pos in buttons.items():
self.buttons[btnText] = QPushButton(btnText)
buttons_layout.addWidget(self.buttons[btnText], pos)
# Add buttons_layout to the general layout
self.generalLayout.addLayout(buttons_layout)
# Create a Controller class to connect the GUI and the model
class Ctrl:
"""App's Controller."""
def __init__(self, setup, view):
"""Controller initializer."""
self._view = view
self._setup = setup
# Connect signals and slots
self._connect_signals()
def _input_select(self): # Not being called
print("input selection")
options = QFileDialog.Options()
file_select, _ = QFileDialog.getOpenFileNames(
self,
'Select Input File...',
'',
'CSV Files (*.csv);;All Files (*)',
options=options
)
if file_select:
self._setup["input"] = file_select
def _connect_signals(self):
"""Connect signals and slots."""
self._view.buttons["Select input file..."].clicked.connect(self._input_select) # Not working!
# Client code
def main():
"""Main function."""
# Create an instance of `QApplication`
app = QApplication(sys.argv)
# Show the app's GUI
view = UI()
view.show()
setup = {}
# Create instance of the controller
Ctrl(setup=setup, view=view)
# Execute app's main loop
sys.exit(app.exec_())
if __name__ == "__main__":
main()
In case it helps, I started out by butchering this example code from a Real Python tutorial, but must have broken it along the way.
The problem is that you are not keeping any persistent reference to the Ctrl() instance you are creating. This results in python garbage collecting it as soon as the instance is created.
To solve the issue, just assign it to a variable:
def main():
"""Main function."""
# Create an instance of `QApplication`
app = QApplication(sys.argv)
# Show the app's GUI
view = UI()
view.show()
setup = {}
# Create instance of the controller
ctrl = Ctrl(setup=setup, view=view)
# Execute app's main loop
sys.exit(app.exec_())
Some considerations:
while separating logic from interface is usually good practice, it's a concept that needs to be used with care, as sometimes it only makes things more complex than they should be. Most of the times (especially with simple programs), it only makes a bigger codebase without giving any actual benefit: it's harder to read and to debug, and you'll probably end up continuously switching from the logic parts and the ui parts of your code;
your code shows one of the drawback of that concept: when you create the file dialog, you're using self, but in that case it refers to the Ctrl instance, while the argument should be the UI instance instead (which will result in a crash, as Qt will get an unexpected argument type); you can use self._view instead, but, as said, the whole separation in this case just makes things unnecessarily complex;
using strings for dictionary keys that refer to internal objects is rarely a good idea (especially when using long descriptive strings like you did);
when importing more than one element from a module, it's usually better to group them instead of using single line imports: it makes the code tidier and easier to read and inspect: from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QFileDialog)

Printing some text in a Label, depending on textbox input - PyQT5

I need help figuring out how to use the value written in a textbox in PyQT5, and use that value to build an IF statement. Any suggestions on how to do it? I have tried to declare the text in the textbox as a variable and use it in the IF statement but I can't seem to figure it out how to do it properly, and every time i run the code, some exit code shows (-1073741819 (0xC0000005) ).
Summing up, can't use pass the value of the textbox to the variable in order to do an IF statement.
I had this code down below:
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QTextEdit
def window():
app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(200, 200, 400, 400)
win.setWindowTitle("Register Program")
label = QtWidgets.QLabel(win)
label.setText("Random Text")
label.move(169, 15)
label2 = QtWidgets.QLabel(win)
label2.resize(300, 100)
label2.setText("1- Register new person\n2- See all regestries\n3- See last regestry\n\nPress ESC to exit\n")
label2.move(70, 50)
textbox = QtWidgets.QLineEdit(win)
textbox.setText("")
textbox.resize(250, 25)
textbox.move(70, 250)
button1 = QtWidgets.QPushButton(win)
button1.move(150, 300)
button1.setText("Submit")
button1.clicked.connect(clicked)
button2 = QtWidgets.QPushButton(win)
button2.move(150, 335)
button2.setText("Close")
button2.clicked.connect(close)
win.show()
sys.exit(app.exec_())
def clicked():
inpt = int(window().textbox.text)
if inpt == 1:
print("Hello")
def close():
sys.exit()
window()```
If you're just looking to get user input, there's a builtin static method you can call for requesting input of a particular type: https://doc.qt.io/qt-5/qinputdialog.html#getText
If you want to make your own widget however, you need to use the signals and slots to trigger a python method to store the value. This is easiest to do in a class. You can trigger the method whenever the text changes with the textChanged signal and do whatever you need to do with it.
(Note, I haven't run this as I don't have PyQt5 currently installed, but it should work)
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
# type: (QtWidgets.QWidget) -> None
super(Widget, self).__init__(parent)
self.line_edit = QtWidgets.QLineEdit()
main_layout = QtWidgets.QVBoxLayout()
main_layout.addWidget(self.line_edit)
self.setLayout(main_layout)
self.line_edit.textChanged.connect(self.on_text_changed)
def get_text(self):
return self.line_edit.text()
def on_text_changed(self, text):
print("The text was changed to:", text)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
widget = Widget()
widget.show()
app.exec_()
Edit: Also, to clarify why you're getting an error, QApplication is a singleton. This means there can only ever be one created. If you try to create a second, you'll get an error. The best way to access the current QApplication is to call QApplication.instance(). You also only call app.exec_() once, as once the application is running it will continue to run in the background. You should use signal/slots to interact with the UI and trigger the code you want to run.

Why does a QLineEdit widget generate an "editingFinished" signal when other widgets are activated?

I'm trying to understand why QLineEdit "editingFinished" signals are generated when other widgets are selected. In the example below the "on_lineedit" method is called when the combo box is selected. Why?
import sys
from PyQt5 import QtWidgets
class MyApp(QtWidgets.QDialog):
def __init__(self, *args):
super().__init__(*args)
# create combobox:
combobox = QtWidgets.QComboBox(self)
combobox.addItems(['Item 1', 'Item 2'])
# create line edit
lineedit = QtWidgets.QLineEdit(self)
lineedit.editingFinished.connect(self.on_lineedit)
# layout:
vbox = QtWidgets.QVBoxLayout()
vbox.addWidget( combobox )
vbox.addWidget( lineedit )
self.setLayout(vbox)
def on_lineedit(self):
print('on_lineedit')
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
I know that this issue can be avoided by connecting the "textChanged" signal instead of the "editingFinished" signal like this:
lineedit.textChanged.connect(self.on_lineedit)
and I've seen similar issues raised elsewhere (links below) but I still don't understand why the "editingFinished" signal is generated when the combobox is selected.
Qt qspinbox editingFinished signal on value changed
Suppress QLineEdit editingFinished signal when certain button is clicked
From http://doc.qt.io/archives/qt-4.8/qlineedit.html#editingFinished
This signal is emitted when the Return or Enter key is pressed or the line edit loses focus.
The signal is emitted because it is designed to be. The other widget your click on is not really relevant here, what is relevant is that it the line edit loses focus and it is that which causes the signal to be emitted. Clicking on another widget is just one of many ways your line edit might lose focus.

EventFilter for Drop-Events within QTableView

I try to create a QTableView that can handle drop events. For reasons of application architecture, I want that to be done by an eventFilter of mine (that handles some QAction-triggers for clipboard interaction as well). But the drop-event does not seem to get through to the eventFilter.
I do not care where the data are dropped within the view. That is one of the reasons I want it to be handled by that eventFilter and not by the model. Furthermore, I do not want the model to pop up a dialog ("Are you sure to drop so many elements?"), because user interaction should be done by gui elements.
btw: It did actually work in Qt4/PySide.
I set up an example code snippet to illustrate the problem. The interesting thing about this is that there can appear QDrop Events, but only in the headers of the item view.
#!/usr/bin/env python2.7
# coding: utf-8
from PyQt5.QtWidgets import (
QApplication,
QMainWindow,
QTableView,
QWidget,
)
from PyQt5.QtCore import (
Qt,
QStringListModel,
QEvent
)
app = QApplication([])
window = QMainWindow()
# Table View with Drop-Options
view = QTableView(window)
view.setDropIndicatorShown(True)
view.setDragEnabled(True)
view.setAcceptDrops(True)
view.setDragDropMode(QTableView.DragDrop)
view.setDefaultDropAction(Qt.LinkAction)
view.setDropIndicatorShown(True)
window.setCentralWidget(view)
# Simple Event Filter for TableView
class Filter(QWidget):
def eventFilter(self, widget, event):
print widget, event, event.type()
if event.type() in (QEvent.DragEnter, QEvent.DragMove, QEvent.Drop):
print "Drag'n'Drop-Event"
if event.type() != QEvent.Drop:
print "\tbut no DropEvent"
event.acceptProposedAction()
else:
print "\tan actual DropEvent"
return True
return False
filter = Filter(window)
view.installEventFilter(filter)
class MyModel(QStringListModel):
# Model with activated DragDrop functionality
# vor view
def supportedDragActions(self):
print "asks supported drop actions"
return Qt.LinkAction | Qt.CopyAction
def canDropMimeData(self, *args):
print "canDropMimeData"
return True
def dropMimeData(self, *args):
print "dropMimeData"
return True
model = MyModel("Entry_A Entry_B Entry_C".split())
view.setModel(model)
window.show()
window.raise_()
app.exec_()
Final question: What widget handles the QDropEvents within the QTableView, or what widget should I install the eventFilter on?
view.viewport() gets all the remaining events. So simply adding
view.viewport().installEventFilter(filter)
will do.

PyQt4 menu acction to add new tab to QTabWidget

I'm working on a small application for work w/ python and PyQt4 for the GUI. What I'm trying to accomplish is having a tabbed GUI where when a user clicks on a menu item, the action taken adds a tab to the QTabWidget. I'm currently having trouble getting an action to do such a thing. I've tried creating the GUI by hand and with QT designer, but I cant figure out how, if at all possible, to get an action to add a tab to the QTabWidget. This is my python code:
import sys
from PyQt4 import QtGui, uic
class TestGUI(QtGui.QMainWindow):
def __init__(self):
super(TestGUI, self).__init__()
uic.loadUi('TEST.ui', self)
self.show()
self.actionAdd_Tab.triggered.connect(addTab)
def addTab():
print 'This works'
#Add new tab to GUI
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = TestGUI()
sys.exit(app.exec_())
Pressing the menu item prints 'This works' to the console, so I know that its calling the addTab() function, but how do I get it to add a Tab?
Let me know if you would like to see the .ui file if it will help
The handler for your action needs to create a tab label, and also a widget for the contents of the tab, so that they can be added to the tabwidget.
As a start, try something like this:
import sys
from PyQt4 import QtGui, uic
class TestGUI(QtGui.QMainWindow):
def __init__(self):
super(TestGUI, self).__init__()
uic.loadUi('TEST.ui', self)
self.actionAdd_Tab.triggered.connect(self.handleAddTab)
def handleAddTab(self):
contents = QtGui.QWidget(self.tabWidget)
layout = QtGui.QVBoxLayout(contents)
# add other widgets to the contents layout here
# i.e. layout.addWidget(widget), etc
self.tabWidget.addTab(contents, 'Tab One')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = TestGUI()
window.show()
sys.exit(app.exec_())
QTabWidget's addTab() method, coincidentally named the same, adds a tab.

Categories