Validate if editable QCombobox input is a directory via QValidator - python

I try to validate if a editable QCombobox input is a directory or not before it gets added to the QCombobox.
from PySide import QtGui, QtCore
class DirValidator(QtGui.QValidator):
def __init__(self, cb_input):
super(DirValidator, self).__init__()
self._input = cb_input
def validate(self, _text, _pos):
_dir = QtCore.QDir(_text)
if self._input.hasFocus(): # ignore validation while editing not complete
return QtGui.QValidator.Acceptable
if QtCore.QDir.exists(_dir):
return QtGui.QValidator.Acceptable
return QtGui.QValidator.Invalid
dir_validator = DirValidator(self.cb_path.lineEdit())
self.cb_path.setValidator(dir_validator)
sadly it does not work properly because every input still gets added to the combobox when i hit enter.
Any suggestions?
EDIT: i also tried to use the validator on the QLineEdit like so:
dir_validator = DirValidator(self.cb_path.lineEdit())
self.cb_path.lineEdit().setValidator(dir_validator)
Does not work either.
EDIT: It kinda works...but when i press return "hasFocus" is still True so it is just accepting the input and then of course adding it to the combobox. if i get rid of "if self._input.hasFocus():" it does not accept any input if i type it in...just if a paste a complete directory path.
So what i need is a way to check if the edit is finished to then check if it is a directory.
And as far as i understand i can only check this in a combobox via QValidator...because it adds the input to the box right away...before i can intercept it in any way.
EDIT: i did find solution for my case. I just abandoned the whole validator approach. The purpose of that was to prevent the combobox from creating a new item if it was no valid directory...what i now did instead was to validate the input after it was finished by taking advantage of the QLineEdit().editingFinished() signal. After it created the new Item i just removed it again if the input was not valid and it also gave me the opportunity to add a error Popup telling the user that the input was no directory.

I do not see the need for hasFocus(), because if you are writing to the QLineEdit it obviously has the focus. If the path is incorrect then you must return a QValidator::Intermediate:
from PySide import QtGui, QtCore
class DirValidator(QtGui.QValidator):
def validate(self, _text, _pos):
_dir = QtCore.QDir(_text)
if _dir.exists():
return QtGui.QValidator.Acceptable
return QtGui.QValidator.Intermediate
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
combo = QtGui.QComboBox(editable=True)
dir_validator = DirValidator(combo.lineEdit())
combo.setValidator(dir_validator)
combo.show()
sys.exit(app.exec_())

Related

Menubar sometimes does not become un-greyed when QFileDialog closes

OS: W10. This may be significant. If you have different results on a different platform, feedback would be helpful.
Here is an MRE. If you run it and go Ctrl+O, the menu labels become greyed. If you select a file in the QFileDialog by clicking the "Open" button or using its mnemonic (Alt+O), the open-file dialog is dismissed and the "Files" and "Help" menus become un-greyed.
However, if you go Ctrl+O again, and this time enter the name of a file in the "File name" box (QLineEdit), and then press Return, the dialog is dismissed (with a successful selection result) but the "Files" and "Help" menus remain greyed-out. It looks like this:
import sys, os
from PyQt5 import QtWidgets, QtCore, QtGui
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Greying of menus MRE')
self.setGeometry(QtCore.QRect(100, 100, 400, 200))
menubar = QtWidgets.QMenuBar(self)
self.setMenuBar(menubar)
self.files_menu = QtWidgets.QMenu('&Files', self)
menubar.addMenu(self.files_menu)
self.help_menu = QtWidgets.QMenu('&Help', self)
menubar.addMenu(self.help_menu)
self.new_action = QtWidgets.QAction('&New', self)
self.files_menu.addAction(self.new_action)
self.open_action = QtWidgets.QAction('&Open', self)
self.files_menu.addAction(self.open_action)
self.open_action.setShortcut("Ctrl+O")
self.open_action.triggered.connect(self.open_file)
def focusInEvent(self, event ):
print('main_window focusInEvent')
super().focusInEvent(event)
def focusOutEvent(self, event ):
print('main_window focusOutEvent')
super().focusInEvent(event)
def activateWindow(self):
print('main_window activateWindow')
super().activateWindow()
def open_file(self):
print('open file')
main_window_self = self
# open_doc_dialog = QtWidgets.QFileDialog(self.get_main_window())
class OpenDocFileDialog(QtWidgets.QFileDialog):
def accepted(self):
print('accepted')
super().accepted()
def accept(self):
print('accept')
super().accept()
def close(self):
print('close')
super().close()
def done(self, r):
print(f'done r {r}')
# neither of these solves the problem:
# main_window_self.activateWindow()
# main_window_self.files_menu.activateWindow()
super().done(r)
def hide(self):
print(f'hide')
super().hide()
def focusInEvent(self, event ):
print('focusInEvent')
super().focusInEvent(event)
def focusOutEvent(self, event ):
print('focusOutEvent')
super().focusInEvent(event)
def activateWindow(self):
print('activateWindow')
super().activateWindow()
open_doc_dialog = OpenDocFileDialog(self)
open_doc_dialog.setWindowTitle('Choose file')
open_doc_dialog.setDirectory(os.getcwd())
# we cannot use the native dialog, because we need control over the UI
options = open_doc_dialog.Options(open_doc_dialog.DontUseNativeDialog)
open_doc_dialog.setOptions(options)
open_doc_button = open_doc_dialog.findChild(QtWidgets.QDialogButtonBox).button(QtWidgets.QDialogButtonBox.Open)
lineEdit = open_doc_dialog.findChild(QtWidgets.QLineEdit)
# this does not solve the problem
# lineEdit.returnPressed.disconnect()
# lineEdit.returnPressed.connect(open_doc_button.click)
print(f'open_doc_button {open_doc_button}, lineEdit {lineEdit}')
# show the dialog
dialog_code = open_doc_dialog.exec()
if dialog_code != QtWidgets.QDialog.Accepted: return
sel_files = open_doc_dialog.selectedFiles()
print(f'sel_files: {sel_files}')
app = QtWidgets.QApplication([])
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())
This problem can be understood, if not solved, with reference to this answer.
Note that this greying-out is not disablement. As explained in the above link, this has to do with "active/inactive states" of the menus (or their labels). The menus remain enabled throughout, although in this case it's impossible to know that while the open-file dialog is showing because it is modal. Clicking on one menu after the dialog has gone, or just hovering over it, is enough to un-grey them both...
The explanation, as I understand it, is that the "File name" box QLineEdit has a signal, returnPressed, which appears to activate something subtley different to the slot which is invoked when you use the "Choose" button. You can see I have experimented with trying to re-wire that signal, to no avail.
The method done of the QFileDialog appears to be called however the dialog closes (unlike close!), so I tried "activating" the main window... and then the individual QMenus... Doesn't work.
I am not clear how to get a handle on this "active state" business or why the slot connected to returnPressed is (seemingly) unable to give the "active state" back to the menus when the other slot manages to do so.
Edit
Searching on Musicamante's "unpolishing" suggestion led me to this:
lineEdit.returnPressed.disconnect()
def return_pressed():
style = main_window_self.menubar.style()
style.unpolish(main_window_self.menubar)
open_doc_button.click()
lineEdit.returnPressed.connect(return_pressed)
... unfortunately this doesn't work.
This looks like a possible Windows-related bug, since I can't reproduce it on Linux. As a work-around, you could try forcing a repaint after the dialog closes:
# show the dialog
dialog_code = open_doc_dialog.exec()
self.menubar.repaint()
Finally got it, thanks to Musicamante's suggestion:
lineEdit.returnPressed.disconnect()
def return_pressed():
style = main_window_self.menubar.style()
style.unpolish(main_window_self.menubar)
open_doc_button.click()
main_window_self.menubar.repaint()
lineEdit.returnPressed.connect(return_pressed)
... I actually tried this several times, just to make sure it was doing what was intended. So in fact, fortunately, no single-shot timer was needed in this case.

How to avoid Null or empty value in Lineedit of Pyqt5

I have created a form using PyQT5 using python, in the form I getting value from user through QLineEdit.
My problem is that user should not leave any of the field empty in the form.
How to avoid empty Lineedit?
A simple solution is to use a custom QValidator:
from PyQt5 import QtGui, QtWidgets
class NotEmptyValidator(QtGui.QValidator):
def validate(self, text, pos):
state = QtGui.QIntValidator.Acceptable if bool(text) else QtGui.QIntValidator.Invalid
return state, text, pos
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QLineEdit("Hello World")
validator = NotEmptyValidator(w)
w.setValidator(validator)
w.show()
sys.exit(app.exec_())
Extending on eyllanesc's answer:
If you want to make it possible that the user empties the field temporarily (e.g. to remove all text and write completely new content), you should not return an empty field as Invalid but rather as Intermediate. If there is one letter left but an empty QLineEdit would be Invalid, then the user may be blocked from removing the last letter and would instead have to do some kind of workaround. Also, the code below strips possible whitespace before checking for emptiness.
class NotEmptyValidator(QValidator):
def validate(self, text: str, pos):
if bool(text.strip()):
state = QValidator.Acceptable
else:
state = QValidator.Intermediate # so that field can be made empty temporarily
return state, text, pos
You can first convert it into text with
name = lineEdit.text()
use a if statement to detect empty value like this
if name == "":
print("Empty")
More details here

Make an action when the QLineEdit text is changed (programmatically)

I have written the following code snippet with an QLineEdit that can be edited by pushing the button "Add Text".
import sys
import os
from PyQt4 import QtGui
from PyQt4 import *
class SmallGUI(QtGui.QMainWindow):
def __init__(self):
super(SmallGUI,self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300,300,300,300)
self.setWindowTitle('Sample')
#One input
self.MyInput = QtGui.QLineEdit(self)
self.MyInput.setGeometry(88,25,110,20)
###############
QtCore.QObject.connect(self.MyInput,QtCore.SIGNAL("textChanged(bool)"),self.doSomething)
#Add Text
self.MyButton = QtGui.QPushButton(self)
self.MyButton.setGeometry(QtCore.QRect(88,65,110,20))
self.MyButton.setText('Add Text')
###############
QtCore.QObject.connect(self.MyButton,QtCore.SIGNAL("clicked(bool)"),self.addText)
self.show()
def addText(self):
self.MyInput.setText('write something')
def doSomething(self):
print "I'm doing something"
def main():
app = QtGui.QApplication(sys.argv)
sampleForm = SmallGUI()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
What I would like to do is to execute an action when the text of the QLineEdit is changed programmatically, i.e. by clicking the button 'Add Text', doing the following:
QtCore.QObject.connect(self.MyInput,QtCore.SIGNAL("textChanged(bool)"),self.doSomething)
The reason why I have used the signal "textChanged" is related to what the class documentation says, that is "this signal is also emitted when the text is changed programmatically, for example, by calling setText()."
However this does not work cause the print statement is not executed. Can anyone help me out with that?
The problem is that the signal is not textChanged(bool) because it takes a string argument, so it should probably be: textChanged(str).
To avoid this kind of errors you should use the new-style syntax for connecting signals:
self.MyInput.textChanged.connect(self.doSomething)
# or:
self.MyInput.textChanged[str].connect(self.doSomething)
This syntax has several advantages:
It is clearer
It's less verbose and more readable
It provides more error checking because if the signal doesn't exist it raises an error. With the old syntax no error is raised, but the signal isn't connected either and the result is the behaviour you have seen.

Python Qt How to open a pop up QDialog from a QMainWindow

I'm working on a project where I have a Database linked with a Python interface (I'm using Qt Designer for the design). I want to have a delete button from my main window (QMainWindow) where, when I'm pressing it, it opens a pop up (QDialog) which says
Are you sure you want to delete this item?
But I have no idea how to do it.
Thanks for you help!
def button_click():
dialog = QtGui.QMessageBox.information(self, 'Delete?', 'Are you sure you want to delete this item?', buttons = QtGui.QMessageBox.Ok|QtGui.QMessageBox.Cancel)
Bind this function to the button click event.
Let's say your Qt Designer ui has a main window called "MainWindow" and a button called "buttonDelete".
The first step is to set up your main window class and connect the button's clicked signal to a handler:
from PyQt4 import QtCore, QtGui
from mainwindow_ui import Ui_MainWindow
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
self.buttonDelete.clicked.connect(self.handleButtonDelete)
Next you need to add a method to the MainWindow class that handles the signal and opens the dialog:
def handleButtonDelete(self):
answer = QtGui.QMessageBox.question(
self, 'Delete Item', 'Are you sure you want to delete this item?',
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No |
QtGui.QMessageBox.Cancel,
QtGui.QMessageBox.No)
if answer == QtGui.QMessageBox.Yes:
# code to delete the item
print('Yes')
elif answer == QtGui.QMessageBox.No:
# code to carry on without deleting
print('No')
else:
# code to abort the whole operation
print('Cancel')
This uses one of the built-in QMessageBox functions to create the dialog. The first three arguments set the parent, title and text. The next two arguments set the group of buttons that are shown, plus the default button (the one that is initially highlighted). If you want to use different buttons, the available ones can be found here.
To complete the example, you just need some code to start the application and show the window:
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
I got the same error for qt5.6
TypeError: question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 1 has unexpected type 'Ui_MainWindow'
So I changed self to None in the following code, and it works.
def handleButtonDelete(self):
answer = QtGui.QMessageBox.question(
None, 'Delete Item', 'Are you sure you want to delete this item?',
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No |
QtGui.QMessageBox.Cancel,
QtGui.QMessageBox.No)
if answer == QtGui.QMessageBox.Yes:
# code to delete the item
print('Yes')
elif answer == QtGui.QMessageBox.No:
# code to carry on without deleting
print('No')
else:
# code to abort the whole operation
print('Cancel')

(no longer seeking answers) Python input box inside a message box

is there any way to have an input box inside of an message box opened with the ctypes library? so far I have:
import ctypes
messageBox = ctypes.windll.user32.MessageBoxA
title = 'Title'
text = 'Message box!'
returnValue = messageBox(None, text, title, 0x40 | 0x1)
print returnValue
and this gives a message box with an image icon and two buttons, both of which I know how to change, and it sets a variable "returnValue" to a number representing the button clicked. However, I also need a variable that will set to a string input in the message box. The reason I need this and I can't just do simple a = raw_input('prompt') is that I want the program itself to run in the background (it would launch itself on logon).
If you want a simple solution, use the PyMsgBox module. It uses Python's built-in tkinter library to create message boxes, including ones that let the user type a response. Install it with pip install pymsgbox.
The documentation is here: https://pymsgbox.readthedocs.org/
The code you want is:
>>> import pymsgbox
>>> returnValue = pymsgbox.prompt('Message box!', 'Title')
Message box is for messages only. What you need is QDialog. You can create it in QtDesigner(I have login dialog created this way, with 2 QLineEdit for username and pass, 2 buttons in QDialogButtonBox and QCombobox for language choose). You'll get .ui file, which you'll need to convert into .py this way in cmd:
pyuic4 -x YourLoginDialogWindow.ui -o YourLoginDialogWindow.py
import created YourLoginDialogWindow.py and you can use it and implement any method you need:
import YourLoginDialogWindow
class YourLoginDialog(QtGui.QDialog):
def __init__(self, parent = None):
super(YourLoginDialog, self).__init__(parent)
self.__ui = YourLoginDialogWindow.Ui_Dialog()
self.__ui.setupUi(self)
...
self.__ui.buttonBox.accepted.connect(self.CheckUserCredentials)
self.__ui.buttonBox.rejected.connect(self.reject)
def GetUsername(self):
return self.__ui.usernameLineEdit.text()
def GetUserPass(self):
return self.__ui.passwordLineEdit.text()
def CheckUserCredentials(self):
#check if user and pass are ok here
#you can use self.GetUsername() and self.GetUserPass() to get them
if THEY_ARE_OK :
self.accept()# this will close dialog and open YourMainProgram in main
else:# message box to inform user that username or password are incorect
QtGui.QMessageBox.about(self,'MESSAGE_APPLICATION_TITLE_STR', 'MESSAGE_WRONG_USERNAM_OR_PASSWORD_STR')
in your __main__ first create login dialog and then your main window...
if __name__ == "__main__":
qtApp = QtGui.QApplication(sys.argv)
loginDlg = YourLoginDialog.YourLoginDialog()
if (not loginDlg.exec_()):
sys.exit(-1)
theApp = YourMainProgram.YourMainProgram( loginDlg.GetUsername(), loginDlg.GetPassword())
qtApp.setActiveWindow(theApp)
theApp.show()
sys.exit(qtApp.exec_())

Categories