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 8316011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:15:37+00:00 2026-06-08T21:15:37+00:00

I am wondering how to best truncate text in a QLabel based on it’s

  • 0

I am wondering how to best truncate text in a QLabel based on it’s maximum width/height.
The incoming text could be any length, but in order to keep a tidy layout I’d like to truncate long strings to fill a maximum amount of space (widget’s maximum width/height).

E.g.:

 'A very long string where there should only be a short one, but I can't control input to the widget as it's a user given value'

would become:

'A very long string where there should only be a short one, but ...'

based on the required space the current font needs.

How can I achieve this best?

Here is a simple example of what I’m after, though this is based on word count, not available space:

import sys
from PySide.QtGui import *
from PySide.QtCore import *


def truncateText(text):
    maxWords = 10
    words = text.split(' ')
    return ' '.join(words[:maxWords]) + ' ...'

app = QApplication(sys.argv)

mainWindow = QWidget()
layout = QHBoxLayout()
mainWindow.setLayout(layout)

text = 'this is a very long string, '*10
label = QLabel(truncateText(text))
label.setWordWrap(True)
label.setFixedWidth(200)
layout.addWidget(label)

mainWindow.show()
sys.exit(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-06-08T21:15:38+00:00Added an answer on June 8, 2026 at 9:15 pm

    Even easier – use the QFontMetrics.elidedText method and overload the paintEvent, here’s an example:

    from PyQt4.QtCore import Qt
    from PyQt4.QtGui import QApplication,\
                            QLabel,\
                            QFontMetrics,\
                            QPainter
    
    class MyLabel(QLabel):
        def paintEvent( self, event ):
            painter = QPainter(self)
    
            metrics = QFontMetrics(self.font())
            elided  = metrics.elidedText(self.text(), Qt.ElideRight, self.width())
    
            painter.drawText(self.rect(), self.alignment(), elided)
    
    if ( __name__ == '__main__' ):
        app = None
        if ( not QApplication.instance() ):
            app = QApplication([])
    
        label = MyLabel()
        label.setText('This is a really, long and poorly formatted runon sentence used to illustrate a point')
        label.setWindowFlags(Qt.Dialog)
        label.show()
    
        if ( app ):
            app.exec_()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking into using FluentNHibernate as my ORM but I'm wondering how best to
I'm writing a console based program for my coursework, and am wondering how best
Apologies if this is a silly question, but I'm just wondering the best way
Just wondering the best way to replace in place matches on a string. value.replace(bob,
I'm wondering about best practice here. Is it good practice for a factory method
I was wondering the best way to upload file to a web server in
I'm new to web development and am just wondering about best practices for java
I am writing a simple multithreaded socketserver and I am wondering how best to
I'm just getting started with rails gems, and wondering how best to add functionality
Wondering what the best / good way of doing this would be in jQuery.

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.