4/7/2023 0 Comments Pyqt plain text editorBoth menus are invoked by particular key sequences, and the text effects menu is also popped up when a context menu event occurs.ĭef contextMenuEvent(self, event): self.textEffectMenu()Ī context menu event occurs when the user right-clicks on some platforms, or presses a particular key or key sequence on others. We have done this by providing a text effects menu and a color menu, and by supporting some key sequences for formatting. To support this we must provide some means by which the user can apply such changes. One thing that distinguishes the RichTextLineEdit from a QLineEdit is the user's ability to change the format and color of words and characters. Return QSize(fm.width("Alternatively, we could have simply used an arbitrary amount, say, 40 pixels. The QTextDocument.idealWidth() method was introduced with Qt 4.2. The preferred size is the "ideal" width of the text (taking into account font sizes and attributes like bold and italic), with a bit of padding to give a little margin, and the maximum height. Return QSize(self.document().idealWidth() 5, self.maximumHeight()) They are essential in this case, since without them, on non-Windows platforms, h would always be set to 1 instead of the 20 or 30 or so that we would expect. Notice that the conditional expression is enclosed in parentheses. The tooltip is there to give users a hint that the widget has some special key presses. Calculating a minimum and maximum height will help when we implement the size hint methods, but it is complicated slightly by differences in font metrics across platforms. We also ensure that Tab causes a change of focus rather than the insertion of a Tab character. Since the widget is a single line, we switch off line wrapping and scrollbars. Nowadays, every platform can be expected to provide Courier, Helvetica, and Times fonts (or fonts for which these names are aliases). We begin by setting some default font families. tToolTip("P ress Ctrl M for the text effects " "menu and Ctrl K for the color menu") tMinimumHeight(h) tMaximumHeight(int(h * 1.2)) H = int(fm.height() * (1.4 if platform.system() = "Windows" \ tVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) tHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) fm = QFontMetrics(self.font()) Self.monofamily = QString("courier") self.sansfamily = QString("helvetica") riffamily = QString("times") tLineWrapMode(QTextEdit.NoWrap) tTabChangesFocus(True) Super(RichTextLineEdit, self)._init_(parent) (Bold, Italic, Underline, StrikeOut, Monospaced, Sans, Serif, NoSuperOrSubscript, Subscript, Superscript) = range(10) We will begin with some static constants and the initializer. In addition, three font styles will be supported-monospaced, sans serif, and serif-and the ability to set the text's color to a limited range of colors. The rich text line edit will support the most common kinds of text formatting that apply to single lines: bold, italic, underline, strikeout, superscript, and subscript. The e = mc2formula for dummies] Figure 13.2 The Rich Text Line Edit In the process we will learn how to program-matically format pieces of text in a QTextEdit, and how to iterate over a QTextE-dit's QTextDocument to extract the text and its formatting. In this subsection we will create a RichTextLineEdit, a subclass of QTextEdit that provides the functionality we need it is shown in Figure 13.2. Unfortunately, PyQt does not provide such a widget. We will see examples of this in Chapter 14 and Chapter 16. For example, a database application may have a "description" field where we want the user to be able to use bold, italic, and colors for particular words if they want. In some applications it is a requirement that users can enter single lines of rich text.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |