Results 1 to 11 of 11

Thread: Designer UI widget in qtablewidget / setCellWidget

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

    Default Designer UI widget in qtablewidget / setCellWidget

    I may be a lughead, but I can not seem to figure out the proper direction to take with what I am trying to accomplish. Basically I have a QTableWidget with some data in cells as follows:

    ----------------------
    ID | name | phone #|
    ----------------------
    |*tool* | e-mail |
    ----------------------
    ID | name | phone #|
    ----------------------
    |*tool* | e-mail |
    ----------------------
    repeat....

    I have the table widget working properly to display my data how I want, but in the *tool* cells I would like to put some buttons/icons that someone could click to spawn a popup about the person shown in the table.

    I created a widget UI file from designer, and I have read docs about setCellWidget(). I am still at a loss however about how to use the UI file (my small toolbar) with setCellWidget(), and then connecting slots to the signals that the toolbar generates.

    I have made lots of dialog popups, and my main window with UI files and designer, just not quite sure how to place it within many cells in a table.

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

    Default Re: Designer UI widget in qtablewidget / setCellWidget

    So far here is what I have:

    My Header to use the (tools_contacts.h):
    Qt Code:
    1. #ifndef TOOLS_CONTACTS_H
    2. #define TOOLS_CONTACTS_H
    3.  
    4. //user interface items:
    5. #include "ui_tools_contacts.h"
    6.  
    7. //Qt functions:
    8. #include <QString>
    9.  
    10. class toolsContacts : public QWidget, private Ui::toolsContacts {
    11.  
    12. Q_OBJECT
    13.  
    14. public:
    15. //functions:
    16. toolsContacts(QWidget *parent = 0);
    17.  
    18. private slots:
    19.  
    20. private:
    21.  
    22. };
    23.  
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    the tools_contacts.cpp:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "tools_contacts.h"
    4.  
    5. //setup interface of main window:
    6. toolsContacts::toolsContacts(QWidget *parent) : QWidget(parent) {
    7.  
    8. setupUi(this);
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    The test code to invoke it from my main window:
    Qt Code:
    1. toolsContacts contactListTool(this);
    2. ui.tableWidget->setCellWidget(1, 1, contactListTool);
    To copy to clipboard, switch view to plain text mode 

    I think I am close, however when I compile I get the following errors:

    This I believe is from an issue somewhere within my class to use the UI file:
    In file included from tools_contacts.h:5,
    from calculatorform.h:15,
    from calculatorform.cpp:3:
    ui_tools_contacts.h:25: error: expected unqualified-id before "delete"
    ui_tools_contacts.h:25: error: abstract declarator `QToolButton*' used as declaration
    ui_tools_contacts.h:25: error: expected `;' before "delete"
    ui_tools_contacts.h: In member function `void Ui_toolsContacts::setupUi(QWidget*)':
    ui_tools_contacts.h:42: error: expected primary-expression before '=' token
    ui_tools_contacts.h:43: error: expected primary-expression before '->' token
    ui_tools_contacts.h:44: error: expected primary-expression before '->' token
    ui_tools_contacts.h:45: error: expected primary-expression before '->' token
    ui_tools_contacts.h: In member function `void Ui_toolsContacts::retranslateUi(QWidget*)':
    ui_tools_contacts.h:57: error: expected primary-expression before '->' token
    This comes from me not being able to tell the setCellWidget function that my toolsContacts is a widget:


    error: no matching function for call to `QTableWidget::setCellWidget(int, int, toolsContacts&)'

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

    Default Re: Designer UI widget in qtablewidget / setCellWidget

    Well I have solved part of the issue now:

    the errors within the ui_tools_contacts.h file were caused by me naming a button within the .ui file "delete" once I renamed it to "trash" then those errors went away. So I guess I am still stuck on how to actually place the widget in my table, i still get this error:


    calculatorform.cpp: In member function `void ProgramMain::showContacts()':
    calculatorform.cpp:1683: error: no matching function for call to `QTableWidget::setCellWidget(int, int, toolsContacts&)'
    ../../include/QtGui/../../src/gui/itemviews/qtablewidget.h:249: note: candidates are: void QTableWidget::setCellWidget(int, int, QWidget*)

    C:\Qt\4.3.0\Kdevelop\housing>

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

    Default Re: Designer UI widget in qtablewidget / setCellWidget

    The cell widget should be allocated on the heap:
    Qt Code:
    1. toolsContacts* contactListTool = new toolsContacts(this);
    2. ui.tableWidget->setCellWidget(1, 1, contactListTool);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    tpf80 (4th July 2007)

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

    Default Re: Designer UI widget in qtablewidget / setCellWidget

    your awesome, this made the toolbar show up in the cell just like I wanted it to.

    Do I access the signals in the contactListTool by contactListTool.signal()? 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.
    Last edited by tpf80; 4th July 2007 at 09:08.

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

    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

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

    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.

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

    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

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

    tpf80 (6th July 2007)

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

    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!

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

    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!

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

    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

  14. 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, 15:47
  2. Center a widget in a cell on a QTableWidget
    By roleroz in forum Qt Programming
    Replies: 16
    Last Post: 5th March 2009, 14:47
  3. QT4: Custom widget for QT Designer
    By Michiel in forum Qt Tools
    Replies: 4
    Last Post: 4th May 2006, 13:35
  4. Replies: 4
    Last Post: 1st March 2006, 23:11
  5. Replies: 4
    Last Post: 6th February 2006, 14: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.