Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6782469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:44:45+00:00 2026-05-26T16:44:45+00:00

This code runs a little window with a toolbar and a QTextEdit area. If

  • 0

This code runs a little window with a toolbar and a QTextEdit area.

If you highlight ‘bananas’ and change the font size, then using zoom from either the toolbar buttons or CTRL + mouse wheel will only resize ‘apples’. Anyone know why?

from PySide import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.textEdit = Editor(self)
        self.toolBar  = QtGui.QToolBar(self)
        self.addToolBar(self.toolBar)
        self.setCentralWidget(self.textEdit)
        self.textEdit.setHtml('<font color=blue>apples bananas</font>')

        # Zoom
        self.actionZoomIn  = QtGui.QAction('Zoom In',  self)
        self.actionZoomOut = QtGui.QAction('Zoom Out', self)
        self.toolBar.addAction(self.actionZoomIn)
        self.toolBar.addAction(self.actionZoomOut)
        self.actionZoomIn.triggered.connect(self.onZoomInClicked)
        self.actionZoomOut.triggered.connect(self.onZoomOutClicked)

        # Font Size
        self.comboSize = QtGui.QComboBox(self.toolBar)
        self.toolBar.addWidget(self.comboSize)
        self.comboSize.addItem('0')
        self.comboSize.addItem('10')
        self.comboSize.addItem('18')
        self.comboSize.addItem('30')
        self.comboSize.addItem('48')
        self.comboSize.setCurrentIndex(1)
        self.comboSize.activated[str].connect(self.textSize)

    def textSize(self, pointSize):
        pointSize = int(pointSize)
        if pointSize > 0:
            fmt = QtGui.QTextCharFormat()
            fmt.setFontPointSize(pointSize)
            self.mergeFormatOnWordOrSelection(fmt)

    def mergeFormatOnWordOrSelection(self, format):
        cursor = self.textEdit.textCursor()
        if not cursor.hasSelection():
            cursor.select(QtGui.QTextCursor.WordUnderCursor)

        cursor.mergeCharFormat(format)
        self.textEdit.mergeCurrentCharFormat(format)

    def onZoomInClicked(self):
        self.textEdit.zoom(+1)

    def onZoomOutClicked(self):
        self.textEdit.zoom(-1)

class Editor(QtGui.QTextEdit):
    def __init__(self, parent=None):
        super(Editor, self).__init__(parent)
        self.zoomValue = 0

    def zoom(self, delta):
        zoomIncrement = 3

        if delta < 0:
            zoomIncrement = 0 - zoomIncrement

        self.zoomIn(zoomIncrement)
        self.zoomValue = self.zoomValue + zoomIncrement

        print "self.zoomValue", self.zoomValue

    def wheelEvent(self, event):
        if (event.modifiers() & QtCore.Qt.ControlModifier):
            self.zoom(event.delta())

if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = MainWindow()
    window.resize(400, 180)
    window.show()
    app.exec_()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:44:46+00:00Added an answer on May 26, 2026 at 4:44 pm

    The default implementation of QTextEdit.zoomIn/Out simply changes the pointSize of the base font for the text-edit.

    The method used in the example to change font size wraps the selected word in a span tag and uses inline css to set the font-size property to a fixed value. This means that when the text-edit is subsequently zoomed, only the unchanged text will be affected.

    It would be possible overcome this problem by using relative font sizes. However, it looks like only a limted subset of css properties is supported, so its only possible to set imprecise values like small, large, etc.

    This can be implemented in the example by making the following changes:

        # Font Size
        self.comboSize = QtGui.QComboBox(self.toolBar)
        self.toolBar.addWidget(self.comboSize)
        self.comboSize.addItem('small')
        self.comboSize.addItem('medium')
        self.comboSize.addItem('large')
        self.comboSize.addItem('x-large')
        self.comboSize.addItem('xx-large')
        self.comboSize.setCurrentIndex(1)
        self.comboSize.activated[int].connect(self.textSize)
    
    def textSize(self, size):
        fmt = QtGui.QTextCharFormat()
        fmt.setProperty(QtGui.QTextFormat.FontSizeAdjustment, size - 1)
        self.mergeFormatOnWordOrSelection(fmt)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a little confused as to why this code compiles and runs: class A
This code runs on my local RoR/Windows 7 (64-bit): sql = ActiveRecord::Base.connection() last_pk =
I have a python script the runs this code: strpath = sudo svnadmin create
This code in a GUI application compiles and runs: procedure TForm1.Button1Click(Sender: TObject); begin Self
This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer
Please check this code out it compiles and runs absolutely fine.. The question is
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
I have some C++ source code with templates maybe like this - doxygen runs
I've written this little MS Outlook 2003 VSTO Add-In using C# and Visual Studio
Can you please explain this code? It seems a little confusing to me Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.