Results 1 to 6 of 6

Thread: connect signal to a new QtableWidget

  1. #1
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: connect signal to a new QtableWidget

    I would like to connect a signal to new tables everytime the user clicks on any of their cells.

    The user creates the QWidgetTables as needed (with: table = new QTableWidget(this) ) and adds them to an existing QTabWidget

    I cannot figure out a way to issue the signal from the unknown QWidgetTable.

    how do i find "table" in the line below and where should I make the connection?

    connect(table,SIGNAL(itemClicked(QTableWidgetItem) ),this,SLOT(doSomething(QTableWidgetItem)));


    Added after 21 minutes:


    I can interact with the tables if I write the connection just after I create the tables in the same function. That is to say something like

    Qt Code:
    1. void myclass::createTables(void)
    2. {
    3. table = new QTableWidget(this);
    4. ...
    5. ..
    6. myTabWidget->addTab(table,"name");
    7. ...
    8. connect(table,SIGNAL(itemClicked(QTableWidgetItem) ),this,SLOT(doSomething(QTableWidgetItem)));
    9. }
    To copy to clipboard, switch view to plain text mode 

    However, I can interact with only the last table but not the earlier tables. How do I overcome this?

    Thanks
    Last edited by hass26; 7th March 2015 at 20:12.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: connect signal to a new QtableWidget

    Why do you need to find anything? You create the table then you connect it. Assuming "this" is the tab widget...
    Qt Code:
    1. QTableWidget *table = new QTableWidget(this);
    2. connect(table,SIGNAL(itemClicked(QTableWidgetItem) ),this,SLOT(doSomething(QTableWidgetItem)));
    To copy to clipboard, switch view to plain text mode 

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

    hass26 (7th March 2015)

  4. #3
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: connect signal to a new QtableWidget

    Yes I can do that, but I need to re-write the code as the table is accessible from many other functions.
    But I think that is the safest way. Thanks

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: connect signal to a new QtableWidget

    Assuming the variable "table" is a member variable then you are discarding the pointer value every time you create a new table widget. Since you only have on pointer you can only use it to access one table.

    If you are sharing slot with all the table widget connected to it then you can use the sender() function in the slot to work out which activated the slot. Something like
    Qt Code:
    1. void Class::slot(...)
    2. {
    3. QTableWidget *sender = qobject_cast<QTableWidget *>(sender);
    4. if (sender) {
    5. // do stuff
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    There may be a better design... but we do not know enough about what you are doing.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: connect signal to a new QtableWidget

    If you need access to the n-th table you create at any time after creation, put the tables into a numerically indexed container.

    For the connect() that would only make sense if you disconnect and reconnect later on, otherwise connect after creation is way cleaner.

    Cheers,
    _

  7. #6
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: connect signal to a new QtableWidget

    Eventually that is what I did. It is cleaner and works.

Similar Threads

  1. How to connect signal to signal in QWidget correctly
    By Mint87 in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2013, 00:06
  2. Connect signal/signal in Qt Designer
    By jlemaitre in forum Newbie
    Replies: 1
    Last Post: 22nd September 2010, 15:53
  3. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 20:42
  4. QObject::connect: No such signal
    By caseyong in forum Qt Programming
    Replies: 5
    Last Post: 19th February 2008, 07:23

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.