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. How to add ellipses to the end of a QTreeWidgetItem in PyQt6?
Forum Updated to NodeBB v4.3 + New Features

How to add ellipses to the end of a QTreeWidgetItem in PyQt6?

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 38 Views
  • 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.
  • W Offline
    W Offline
    wayfarer
    wrote last edited by
    #1

    I'm playing around with an idea for an app that uses a QTreeWidget for part of its interface (unrelated to my earlier question on this forum - this is a different project), but the QTreeWidgetItem text goes over the edge of the window and vanishes if it's long enough.

    I don't want a word or line-wrapping solution since I'd prefer to keep each line of text on its own line, so I'm trying to figure out how to add ellipses to the end of any line that goes over the edge.

    I did a bunch of web searches and figured out a partial solution, but it doesn't work right at different/multiple font sizes and it doesn't adjust when the window is resized:

    import sys
    from PyQt6.QtWidgets import QApplication, QMainWindow, QTreeWidget, QTreeWidgetItem, QLabel
    from PyQt6.QtCore import Qt, QSize
    from PyQt6.QtGui import QIcon, QAction, QFontMetrics
    
    class MainWindow(QMainWindow):
    	def __init__(self):
    		super().__init__()
    		self.setWindowTitle('Tree')
    		self.setMinimumWidth(500), self.setMinimumHeight(400)
    		tree = QTreeWidget(self)
    		tree.setColumnCount(1)
    		tree.setHeaderLabel('Entries')
    		entries = ['Entry 1','Entry 2','Entry wheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee']
    		for item in entries:
    			entry = QTreeWidgetItem(tree)
    			label = QLabel()
    			label.setTextFormat(Qt.TextFormat.RichText)
    			metrics = QFontMetrics(label.font())
    			elidedText = metrics.elidedText(item, Qt.TextElideMode.ElideRight, 400)
    			label.setText(f"<h3 style='margin-bottom: 0;'>{elidedText}</h3><span style='color: gray; font-size: 18px;'>{elidedText}</span>")
    			tree.setItemWidget(entry, 0, label)
    		self.setCentralWidget(tree)
    		items = tree.findItems("", Qt.MatchFlag.MatchContains, 0)
    		items[0].setSelected(True)
    
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec()
    
    jsulmJ 1 Reply Last reply
    0
    • W wayfarer

      I'm playing around with an idea for an app that uses a QTreeWidget for part of its interface (unrelated to my earlier question on this forum - this is a different project), but the QTreeWidgetItem text goes over the edge of the window and vanishes if it's long enough.

      I don't want a word or line-wrapping solution since I'd prefer to keep each line of text on its own line, so I'm trying to figure out how to add ellipses to the end of any line that goes over the edge.

      I did a bunch of web searches and figured out a partial solution, but it doesn't work right at different/multiple font sizes and it doesn't adjust when the window is resized:

      import sys
      from PyQt6.QtWidgets import QApplication, QMainWindow, QTreeWidget, QTreeWidgetItem, QLabel
      from PyQt6.QtCore import Qt, QSize
      from PyQt6.QtGui import QIcon, QAction, QFontMetrics
      
      class MainWindow(QMainWindow):
      	def __init__(self):
      		super().__init__()
      		self.setWindowTitle('Tree')
      		self.setMinimumWidth(500), self.setMinimumHeight(400)
      		tree = QTreeWidget(self)
      		tree.setColumnCount(1)
      		tree.setHeaderLabel('Entries')
      		entries = ['Entry 1','Entry 2','Entry wheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee']
      		for item in entries:
      			entry = QTreeWidgetItem(tree)
      			label = QLabel()
      			label.setTextFormat(Qt.TextFormat.RichText)
      			metrics = QFontMetrics(label.font())
      			elidedText = metrics.elidedText(item, Qt.TextElideMode.ElideRight, 400)
      			label.setText(f"<h3 style='margin-bottom: 0;'>{elidedText}</h3><span style='color: gray; font-size: 18px;'>{elidedText}</span>")
      			tree.setItemWidget(entry, 0, label)
      		self.setCentralWidget(tree)
      		items = tree.findItems("", Qt.MatchFlag.MatchContains, 0)
      		items[0].setSelected(True)
      
      app = QApplication(sys.argv)
      window = MainWindow()
      window.show()
      app.exec()
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote last edited by
      #2

      @wayfarer You're setting font size in setText - are you sure it is same as what was set before when elidedText was calculated?
      Regarding resizing the window: you will need to do these calculations again when the window is resized, currently you don't do that.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

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