Issue
The case of a simple message box
I have figured out how to change the font size in simple PyQt dialog windows. Take this example:
# Create a custom font
# ---------------------
font = QFont()
font.setFamily("Arial")
font.setPointSize(10)
# Show simple message box
# ------------------------
msg = QMessageBox()
msg.setIcon(QMessageBox.Question)
msg.setText("Are you sure you want to delete this file?")
msg.setWindowTitle("Sure?")
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
msg.setFont(font)
retval = msg.exec_()
if retval == QMessageBox.Ok:
print('OK')
elif retval == QMessageBox.Cancel:
print('CANCEL')
The key to changing the font size is that you actually have a 'handle' to your message box. The variable msg
is available to tweak the message box to your needs before showing it with msg.exec_()
.
The case of a simple input dialog
The problem about the input dialog is that such handle is not present. Take this example:
# Show simple input dialog
# -------------------------
filename, ok = QInputDialog.getText(None, 'Input Dialog', 'Enter the file name:')
if(ok):
print('Name of file = ' + filename)
else:
print('Cancelled')
The input dialog object is created on-the-fly. I have no way to tweak it to my needs (eg. apply a different font).
Is there a way to get a handle to this QInputDialog
object, before showing it?
EDIT :
I was adviced in the comments to try it with an HTML snippet:
filename, ok = QInputDialog.getText(None, 'Input Dialog', '<html style="font-size:12pt;">Enter the file name:</html>')
The result is as follows:
As you can see, the text input field has still the small (unchanged) font size.
Solution
Thanks to the comments of @denvaar and @ekhumoro, I got the solution. Here it is:
# Create a custom font
# ---------------------
font = QFont()
font.setFamily("Arial")
font.setPointSize(10)
# Create and show the input dialog
# ---------------------------------
inputDialog = QInputDialog(None)
inputDialog.setInputMode(QInputDialog.TextInput)
inputDialog.setWindowTitle('Input')
inputDialog.setLabelText('Enter the name for this new file:')
inputDialog.setFont(font)
ok = inputDialog.exec_()
filename = inputDialog.textValue()
if(ok):
print('Name of file = ' + filename)
else:
print('Cancelled')
Answered By - K.Mulier
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.