Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QInputDialog, Set the font for QPlainTextEdit and QLabel separately

QInputDialog, Set the font for QPlainTextEdit and QLabel separately

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 3 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ideaplus
    wrote on last edited by ideaplus
    #1

    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?

    1 Reply Last reply
    0
    • I Offline
      I Offline
      ideaplus
      wrote on last edited by
      #3

      @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;

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ideaplus
        wrote on last edited by
        #2

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

        1 Reply Last reply
        0
        • I Offline
          I Offline
          ideaplus
          wrote on last edited by
          #3

          @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;

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #4

            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.

            1 Reply Last reply
            1
            • I Offline
              I Offline
              ideaplus
              wrote on last edited by
              #5

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved