简体   繁体   中英

How to change the font size of a QInputDialog in PyQt?

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.

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')

QInputDialog consists of QLabel, QLineEdit, and QPushButton. You can independently control their style using setStyleSheet method.

def RequireInputText(Parent, Title="TextInput", Text="Text"):
    InputDialog = QInputDialog(None)
    InputDialog.setInputMode(QInputDialog.TextInput)
    InputDialog.setWindowTitle(Title)
    InputDialog.setLabelText(Text)

    InputDialog.setStyleSheet(
        """
        QLabel{
            font-size:20px;
            font-weight:bold;
            font-family:Arial;
        }
        QLineEdit{
            font-size:20px;
            font-weight:bold;
            font-family:Arial;
        }
        QPushButton{
            font-size:20px;
            font-weight:bold;
            font-family:Arial;
            border-style:solid;
            border-color:black;
            border-width:2px; 
        }
        """
    )

    Ok = InputDialog.exec_()
    if(Ok):
        InputText = InputDialog.textValue()
        return InputText
    else:
        return None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM