Results 1 to 11 of 11

Thread: Designer UI widget in qtablewidget / setCellWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Designer UI widget in qtablewidget / setCellWidget

    Quote Originally Posted by tpf80 View Post
    your awesome, this made the toolbar show up in the cell just like I wanted it to.
    Great

    Do I access the signals in the contactListTool by contactListTool.signal()?
    Sorry, what do you mean? You connect cell widget signals like any other signals:
    Qt Code:
    1. toolsContacts* contactListTool = new toolsContacts(this);
    2. ui.tableWidget->setCellWidget(1, 1, contactListTool);
    3. connect(contactListTool, SIGNAL(someSignal()), ....);
    To copy to clipboard, switch view to plain text mode 

    Also, is there a way to have the object names be variable? since I would not be able to know how many of these ui's would be shown, due to an unknown table size until runtime.
    You can later access any particular cell widget via QTableWidget::cellWidget(). Is this what you mean?
    J-P Nurmi

  2. #2
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 4 Times in 4 Posts

    Default Re: Designer UI widget in qtablewidget / setCellWidget

    You can later access any particular cell widget via QTableWidget::cellWidget(). Is this what you mean?
    Kindof, But when I create them, they have to have a different name correct? so say I have a loop that creates table rows, and in each row adds the widget. How could I give them a variable name? Perhaps this is more of a general c++ question than a Qt specific one.

    For instance in php I could have ${$something}, and if $something was "cat" then id have a variable $cat, but if $something was "dog" then id have a variable $dog. In this way, I could have a variable with a variable name. A use would be like ${"variable_" . $index} and a for loop would create variables each named by the index (but not an array).

    Assuming that the widgets can't have the same name, and I wouldn't know how many of them I would have until the table is filled with data, it would make sense to use this kind of logic when I create them.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Designer UI widget in qtablewidget / setCellWidget

    You don't need variables with variable names:
    Qt Code:
    1. // how to create and set a cell widget in each cell:
    2. for (int row = 0; row < ui.tableWidget->rowCount(); ++row)
    3. {
    4. for (int col = 0; col < ui.tableWidget->columnCount(); ++col)
    5. {
    6. toolsContacts* contactListTool = new toolsContacts(ui.tableWidget);
    7. ui.tableWidget->setCellWidget(row, col, contactListTool);
    8. }
    9. }
    10.  
    11. // how to access a cell widget in particular cell:
    12. toolsContacts* contactListTool = dynamic_cast<toolsContacts*>(ui.tableWidget->cellWidget(2, 3)); // row, col
    13. if (contactListTool)
    14. {
    15. // the cell contains a cell widget and it's of type toolsContacts
    16. contactListTool->doSomething();
    17. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    tpf80 (6th July 2007)

  5. #4
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 4 Times in 4 Posts

    Smile Re: Designer UI widget in qtablewidget / setCellWidget

    Thanks a lot for all your help, I now understand a lot better how this all fits together!

  6. #5
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 4 Times in 4 Posts

    Default Re: Designer UI widget in qtablewidget / setCellWidget

    one thing I forgot to ask about, but I want to make sure I use good programming techniques.

    The table can be refreshed to have new rows in it, and since there will not be too many and I want the latest info to be there, every time I grab new data, I first use:

    Qt Code:
    1. tableWidget->setRowCount(0)
    To copy to clipboard, switch view to plain text mode 

    to clear the table (which is working nicely for this task), then I add new rows with the new data. Since the table could be refreshed often, my main concern is memory leaks in this case.

    When I clear the table, do the toolsContacts widgets contained within the cells automatically get deleted too? Or are they still in memory, requiring me to make a function to clean them up as part of the process of clearing the table? Also, if there are any slots connected to them, do I need to "disconnect" them before clearing the table, or are their connections removed when the widgets are deleted?

    Thanks a lot for all your help!

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Designer UI widget in qtablewidget / setCellWidget

    Yes, setting row count to 0 makes items and cell widgets to get properly deleted. Does the row count vary between updates? If not, QTableWidget::clear() would be more natural choice.

    And yes, QObject disconnects associated signal slot connections upon destruction. See QObject::~QObject() docs for more details.
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    tpf80 (9th July 2007)

Similar Threads

  1. Can't drop a widget in designer - solution
    By heathbar82 in forum Qt Tools
    Replies: 2
    Last Post: 24th July 2012, 16:47
  2. Center a widget in a cell on a QTableWidget
    By roleroz in forum Qt Programming
    Replies: 16
    Last Post: 5th March 2009, 15:47
  3. QT4: Custom widget for QT Designer
    By Michiel in forum Qt Tools
    Replies: 4
    Last Post: 4th May 2006, 14:35
  4. Replies: 4
    Last Post: 2nd March 2006, 00:11
  5. Replies: 4
    Last Post: 6th February 2006, 15:30

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
  •  
Qt is a trademark of The Qt Company.