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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:19:42+00:00 2026-06-05T17:19:42+00:00

I have a QTreeWidget which needs to be populated with a large sum of

  • 0

I have a QTreeWidget which needs to be populated with a large sum of information. So that I can style it and set it up the way I really wanted, I decided I’d create a QWidget which was styled and dressed all pretty-like. I would then populate the TreeWidget with generic TreeWidgetItems and then use setItemWidget to stick the custom QWidgets in the tree. This works when the QWidgets are called inside the main PyQt thread, but since there is a vast sum of information, I’d like to create and populate the QWidgets in the thread, and then emit them later on to be added in the main thread once they’re all filled out. However, when I do this, the QWidgets appear not to be getting their parents set properly as they all open in their own little window. Below is some sample code recreating this issue:

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

class ItemWidget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        btn = QPushButton(self)

class populateWidgets(QThread):
    def __init__(self):
        QThread.__init__(self)

    def run(self):
        widget = ItemWidget()
        for x in range(5):
            self.emit(SIGNAL("widget"), widget)

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

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

        self.pop = populateWidgets()
        self.connect(self.pop, SIGNAL("widget"), self.addItems)
        self.pop.start()

        itemWidget = QTreeWidgetItem()
        itemWidget.setText(0, "This Works")
        self.tree.addTopLevelItem(itemWidget)
        self.tree.setItemWidget(itemWidget, 1, ItemWidget(self))        

    def addItems(self, widget):
        itemWidget = QTreeWidgetItem()
        itemWidget.setText(0, "These Do Not")
        self.tree.addTopLevelItem(itemWidget)
        self.tree.setItemWidget(itemWidget, 1, widget)

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

As you can see, doing it inside MyMainWindow is fine, but somehow things go awry once it gets processed in the thread and returns. Is this possible to do? If so, how do I properly parent the ItemWidget class inside the QTreeWidgetItem? Thanks in advance.

  • 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-05T17:19:44+00:00Added an answer on June 5, 2026 at 5:19 pm

    AFAICT Qt doesn’t support the creation of QWidgets in a thread other than the thread where the QApplication object was instantiated (i.e. usually the main() thread). Here are some posts on the subject with responses from Qt developers:

    http://www.qtcentre.org/archive/index.php/t-27012.html

    http://www.archivum.info/qt-interest@trolltech.com/2009-07/00506/Re-(Qt-interest)-QObject-moveToThread-Widgets-cannot-be-moved-to-a-new-thread.html

    http://www.archivum.info/qt-interest@trolltech.com/2009-07/00055/Re-(Qt-interest)-QObject-moveToThread-Widgets-cannot-be-moved-to-a-new-thread.html

    http://www.archivum.info/qt-interest@trolltech.com/2009-07/00712/Re-(Qt-interest)-QObject-moveToThread-Widgets-cannot-be-moved-toa-new-thread.html

    (if it were possible, the way to do it would be to call moveToThread() on the QWidgets from within the main thread to move them to the main thread — but apparently that technique doesn’t work reliably, to the extent that QtCore has a check for people trying to do that prints a warning to stdout to tell them not to do it)

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

Sidebar

Related Questions

I have a QTreeWidget that I insert items in, and the user can select
I have to populate a QTreeWidget with items (or children of items) that may
I can't seem to get any mouse clicks in a QTreeWidget. I have tried...
I have a QTreeWidget that reads data from an XML file. If at any
I'm writing an application that has a QTreeWidget that is populated by parsing an
Have an app that can use tts to read text messages. It can also
I have a QTreeWidget which uses the QAbstractItemView.ExtendedSelection property, allowing users to shift click
I have a Qt application that contains a QTreeWidget. I want to replace one
have a small javscript that users can include into their sites like so: <script
I have a custom QAbstractItemModel used to display information in a QTreeWidget. However, individual

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.