Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QInputDialog, Set the font for QPlainTextEdit and QLabel separately



  • QInputDialog, Set the font for QPlainTextEdit and QLabel separately
    I want to set font size for this QPlainTextEdit only (not change QLabel font size), but no way.

    import sys
    
    from PySide2.QtGui import QFont
    from PySide2.QtWidgets import QInputDialog, QApplication, QWidget
    
    
    class MyInputDialog(QInputDialog):
        def __init__(self, parent, *args, **kwargs):
            super().__init__(parent, *args, **kwargs)
            self.setOption(QInputDialog.UsePlainTextEditForTextInput)
            self.setWindowTitle('title')
            self.setLabelText("label\nlabel")
            self.setTextValue('I want to set font size for this QPlainTextEdit only, but no way.')
            self.resize(600, 240)
            font = QFont()
            font.setFamily("Consolas")
            # font.setPointSize(10) #
            self.setFont(font)
            self.setStyleSheet("""QPlainTextEdit {
      font-size: 20;
    }""")
    
    
    class Widget(QWidget):
        def __init__(self, parent=None, *args, **kwargs):
            super().__init__(parent, *args, **kwargs)
            dialog = MyInputDialog(self)
            ok = dialog.exec()
            sys.exit(0)
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = Widget()
        sys.exit(app.exec_())
    

    cpp source code:
    https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qinputdialog.cpp.html

    The only way is to use a custom widget?



  • @ideaplus said in QInputDialog, Set the font for QPlainTextEdit and QLabel separately:

    import sys

    from PySide2.QtGui import QFont
    from PySide2.QtWidgets import QInputDialog, QApplication, QWidget

    class MyInputDialog(QInputDialog):
    def init(self, parent, *args, **kwargs):
    super().init(parent, *args, **kwargs)
    self.setOption(QInputDialog.UsePlainTextEditForTextInput)
    self.setWindowTitle('title')
    self.setLabelText("label\nlabel")
    self.setTextValue('I want to set font size for this QPlainTextEdit only, but no way.')
    self.resize(600, 240)
    font = QFont()
    font.setFamily("Consolas")
    # font.setPointSize(10) #
    self.setFont(font)
    self.setStyleSheet("""QPlainTextEdit {
    font-size: 20;
    }""")

    class Widget(QWidget):
    def init(self, parent=None, *args, **kwargs):
    super().init(parent, *args, **kwargs)
    dialog = MyInputDialog(self)
    ok = dialog.exec()
    sys.exit(0)

    if name == 'main':
    app = QApplication(sys.argv)
    w = Widget()
    sys.exit(app.exec_())

    Font size without unit, add it.
    font-size: 20pt;



  • 8a97d3a1-8604-4345-b984-c7f8df345ca9-image.png



  • @ideaplus said in QInputDialog, Set the font for QPlainTextEdit and QLabel separately:

    import sys

    from PySide2.QtGui import QFont
    from PySide2.QtWidgets import QInputDialog, QApplication, QWidget

    class MyInputDialog(QInputDialog):
    def init(self, parent, *args, **kwargs):
    super().init(parent, *args, **kwargs)
    self.setOption(QInputDialog.UsePlainTextEditForTextInput)
    self.setWindowTitle('title')
    self.setLabelText("label\nlabel")
    self.setTextValue('I want to set font size for this QPlainTextEdit only, but no way.')
    self.resize(600, 240)
    font = QFont()
    font.setFamily("Consolas")
    # font.setPointSize(10) #
    self.setFont(font)
    self.setStyleSheet("""QPlainTextEdit {
    font-size: 20;
    }""")

    class Widget(QWidget):
    def init(self, parent=None, *args, **kwargs):
    super().init(parent, *args, **kwargs)
    dialog = MyInputDialog(self)
    ok = dialog.exec()
    sys.exit(0)

    if name == 'main':
    app = QApplication(sys.argv)
    w = Widget()
    sys.exit(app.exec_())

    Font size without unit, add it.
    font-size: 20pt;


  • Banned

    Okay going to K.I.S.S. this question as you are going about it the hard way -- I have also included your version so you can switch between the two as well as to show a more proper way to use it as a MUC or MRE as the QWidget is not needed.

    from PySide2.QtCore    import *
    from PySide2.QtGui     import *
    from PySide2.QtWidgets import *
    
    # Always declare all other Imports after you declare you Qt imports
    from sys import exit as sysExit
    
    # Next to access the elements of the pre-designed QInputDialog Object you would
    # either have to sub-class it and figure out how to intercept all that functionality
    # however the simpler solution is to make your own Custom Dialog Class the mimics
    # what the QInputDialog looks like and add your own functionality and such
    class MyCustomDialog(QDialog):
        def __init__(self, Text):
            QDialog.__init__(self)
            self.setWindowTitle('Dialog Title')
            self.resize(600, 240)
    
            self.btnOkay   = QPushButton('Okay')
            self.btnOkay.clicked.connect(self.ReturnText)
    
            self.btnCancel = QPushButton('Cancel')
            self.btnCancel.clicked.connect(self.CloseDialog)
    
            HBox = QHBoxLayout()
            HBox.addStretch(1)
            HBox.addWidget(self.btnOkay)
            HBox.addWidget(self.btnCancel)
    
            Font = QFont()
            Font.setFamily("Consolas")
            Font.setBold(True)
            Font.setPointSize(20)
    
            self.lblHeader = QLabel(Text)
            self.txeInput  = QTextEdit()
            self.txeInput.setText('I want to set the font size for this Plain Text Edit only, and here is how.')
            self.txeInput.setFont(Font)
    
            VBox = QVBoxLayout()
            VBox.addWidget(self.lblHeader)
            VBox.addWidget(self.txeInput)
            VBox.addLayout(HBox)
    
            self.setLayout(VBox)
    
        @Slot()
        def CloseDialog(self):
            self.close()
    
        @Slot()
        def ReturnText(self):
            Text = self.txeInput.toPlainText()
            print('Returning : ',Text)
    
        def SetHeader(self, Text):
            self.lblHeader.setText(Text)
    
        def SetTextField(self, Text):
            self.txeInput.setText(Text)
    
    class MyInputDialog(QInputDialog):
        def __init__(self):
          # First you should not use super( ) in Python as it introduces 4 known issues 
          # that must be handled properly. Further there were still actual bugs within
          # the usage of super( ) when used in Python. Yes while super( ) works fine 
          # within C++ it does not work as seamlessly within Python due to the major 
          # differences between these 2 languages. Next the reason it was created was 
          # to handle a rather rare issue and unless you are doing some complicated 
          # inheritance you will most likely never run into this extremely rare issue
          # However the 4 major issues that get included by using super( ) you are much 
          # more likely to occur than that rare issue its meant for to solve. Of course
          # using the basic explicit method, as follows, does not cause these issues and 
          # is as just as simple as using `super( )` further you do not actually gain 
          # anything useful by using `super( )` in Python that could not be done in a 
          # much safer manner.
            QInputDialog.__init__(self)
            self.setWindowTitle('Window Title')
    
            self.setOption(QInputDialog.UsePlainTextEditForTextInput)
            self.setLabelText("Two Line\nLabel")
            self.setTextValue('I want to set the font size for this QPlainTextEdit only, but no way.')
            self.resize(600, 240)
            font = QFont()
            font.setFamily("Consolas")
            # font.setPointSize(10) #
            self.setFont(font)
            self.setStyleSheet("""QPlainTextEdit {font-size: 20;}""")
    
    if __name__=="__main__":
        MainEventHandler = QApplication([])
    
        MainApp = MyCustomDialog('Two Line\nLabel')
    #    MainApp = MyInputDialog()
        MainApp.show()
    
        sysExit(MainEventHandler.exec_())
    
      # If anyone wants more extensive free help I run an online lab-like classroom-like 
      # message server feel free and drop by you will not be able to post until I clear 
      # you as a student as this prevents spammers so if interested here is the invite
      # https://discord.gg/3D8huKC
    


  • In C++ I would try to call something like findChildren<QTextEdit*>() on the dialog. I am not sure how this templated call would translate to Python, though. Then you could manipulate the text edit however you want.

    Otherwise the complete implementation of a new dialog suggested by @Denni-0 is the way to go.



  • @Denni-0 @SimonSchroeder Very helpful, thanks. 🤣


Log in to reply