Results 1 to 2 of 2

Thread: Populating a QtableWidget using a list of custom widgets

  1. #1
    Join Date
    Jul 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Populating a QtableWidget using a list of custom widgets

    Good morning

    I have a problem with population of a qtablewidget using custom widgets stored in a list.

    the code for create the widgets is:

    Qt Code:
    1. xdate=[None]*3200
    2. n_projects=100
    3. for i in range(0,n_projects):
    4. label="Hello"
    5. xdate[i]=QtGui.QLabel()
    6. xdate[i].setText(label)
    7. self.ui.TW1.setCellWidget(13,i,xdate[i])
    To copy to clipboard, switch view to plain text mode 
    Then I could see the custom widgets without problem. Then in the program I want to change the text of the qlabels and reinserting using the following code:

    Qt Code:
    1. for i in range(0,n_projects):
    2. xdate[i].setText("Option"+str(i))
    3. self.ui.TW1.setCellWidget(13,i,xdate[i])
    To copy to clipboard, switch view to plain text mode 
    and now the widgets doesn't appear in the table.

    Why is happen this?

    Many thanks for your help

    German

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Populating a QtableWidget using a list of custom widgets

    Adding a lot of custom widget to a table widget is not the best solution. So better check if you could use a model or at least subclass QTableWidgetItem and alter the paint method to fit your needs. (or use a custom delegate);

    the problem why your code is not working, is that setCellWidget replaces the old widget, so your first widget is deleted and then Qt tries to set the new one which actually was deleted. So to alter your text use something like
    python Code:
    1. self.ui.TW1.cellWidget(13, i)->setText("Option"+str(i))
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lykurg for this useful post:

    Toro (5th August 2010)

Similar Threads

  1. Replies: 1
    Last Post: 18th July 2012, 09:59
  2. How to build a list of custom widgets
    By curreli in forum Newbie
    Replies: 6
    Last Post: 28th July 2010, 12:55
  3. Replies: 3
    Last Post: 8th December 2009, 10:49
  4. Replies: 0
    Last Post: 15th May 2009, 15:38
  5. List of Widgets
    By linuxdev in forum Qt Tools
    Replies: 3
    Last Post: 30th April 2009, 14:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.