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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:39:25+00:00 2026-06-11T16:39:25+00:00

I have a QTreeWidget filling with some itemwidgets, whose main widget will be a

  • 0

I have a QTreeWidget filling with some itemwidgets, whose main widget will be a QTextEdit. The problem I’m having seems to be getting the size down to something managable. The usual method of setting the size policy to maximum doesn’t seem to work. Here’s the example code:

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.tree = QTreeWidget(self)
        self.tree.setColumnCount(1)
        self.setCentralWidget(self.tree)

        textEdit = QTextEdit()
        textEdit.setText("very Small Text Edit")
        sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.MinimumExpanding)
        textEdit.setSizePolicy(sizePolicy)

        itemWidget = QTreeWidgetItem()
        itemWidget.setText(0, "")
        self.tree.addTopLevelItem(itemWidget)
        self.tree.setItemWidget(itemWidget, 0, textEdit)        

        biggerTextEdit = QTextEdit()
        biggerTextEdit.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas et mauris in felis tempus molestie eu sit amet sapien. Proin dapibus pretium ipsum. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque feugiat semper sem a accumsan. Nulla sollicitudin enim quis velit blandit posuere. Ut fringilla vulputate dolor, a accumsan lectus gravida a. Sed convallis facilisis mi et ullamcorper. Integer consectetur aliquet odio sit amet posuere.")        

        itemWidget2 = QTreeWidgetItem()
        itemWidget2.setText(0, "")
        self.tree.addTopLevelItem(itemWidget2)
        self.tree.setItemWidget(itemWidget2, 0, biggerTextEdit)        

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    ui = MyMainWindow()
    ui.show()
    sys.exit(app.exec_())

Note that no matter the length of the text doesn’t seem to have any effect on the height of the textedits. How does one have a QTextEdit scale itself (vertically, at least) to the smallest possible size, and for the sake of knowledge, why doesn’t it behave like many other widgets in this manner? Thanks in advance!

Edit: I should now note that I tried to cheat this effect by using a QLabel (which in regards to resizing, display, and word-warp, works just like i want) and setting the Text Interaction flags to editable. This almost works were there a way to accessing the Label’s edited text. Unfortunately any call of .text() on the label yields the original text. A QLineEdit cannot be multi-line or word-wrap, a QTextEdit seems overly complicated.

The question now becomes, what’s the best widget for creating a small (300 characters or less) text-displaying widget that can also be editable?

  • 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-11T16:39:27+00:00Added an answer on June 11, 2026 at 4:39 pm

    For achieving your goal you have to customise item delegates, because they provide presentation and editing services to your tree widget. The Qt docs have some useful information regarding models, views and delegates (including a tutorial). The following code fixes your problem using delegates:

    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    
    class MyMainWindow(QMainWindow):
        def __init__(self, parent=None):
            QMainWindow.__init__(self, parent)
    
            self.tree = QTreeWidget(self)
            self.tree.setColumnCount(1)
            self.tree.setItemDelegate(MyDelegate(self))
            self.setCentralWidget(self.tree)
    
            itemWidget = QTreeWidgetItem()
            itemWidget.setFlags(itemWidget.flags() | Qt.ItemIsEditable)
            itemWidget.setText(0, "very Small Text Edit")
            self.tree.addTopLevelItem(itemWidget)
    
            itemWidget2 = QTreeWidgetItem()
            itemWidget2.setFlags(itemWidget.flags() | Qt.ItemIsEditable)
            itemWidget2.setText(0, """very Small Text Edit\n
            very Small Text Edit\n
            very Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Editvery Small Text Edit""")
            self.tree.addTopLevelItem(itemWidget2)
    
    class MyDelegate(QStyledItemDelegate):
    
        def sizeHint(self, option, index):
            default = QStyledItemDelegate.sizeHint(self, option, index)
            return QSize(default.width(), default.height() + 12)
    
        def createEditor(self, parent, option, index):
            editor = QTextEdit(parent)
            return editor
    
        def setEditorData(self, editor, index):
            text = index.model().data(index, Qt.DisplayRole).toString()
            editor.setText(text)
    
        def setModelData(self, editor, model, index):
            model.setData(index, QVariant(editor.toPlainText()))
    
    
    if __name__ == "__main__":
        import sys
        app = QApplication(sys.argv)
        ui = MyMainWindow()
        ui.show()
        sys.exit(app.exec_())
    

    The code contains a naive reimplementation of sizeHint(). Note also that you can customise your QTextEdit in the createEditor method. You may want to reimplement the paint() method too (it depends on your needs).

    Please note that subclassing QTreeWidgetItem is not the way to go (it is not even a QWidget). However QTreeWidget is a convenience class which uses a predefined tree model so it makes sense to solve the problem in the framework of model/view programming.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the main problem : I'm using QTreeWidget class as a main tree,
I have QTreeWidget in a widget in Maya (A 3D computer graphics application). The
I have a QTreeWidget which needs to be populated with a large sum of
I have to populate a QTreeWidget with items (or children of items) that may
Is it possible to have individual indentation of items in a QTreeWidget? In specific,
I use a QTreeWidget to display multicolumn items, and I want to have a
I can't seem to get any mouse clicks in a QTreeWidget. I have tried...
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
I have two columns in a QTreeWidget , one column represents a list of
I have a QTreeWidget that reads data from an XML file. If at any

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.