Results 1 to 13 of 13

Thread: Problem emiting signal in QTreeWidgetItem's subclass

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: Problem emiting signal in QTreeWidgetItem's subclass

    Perhaps you could even use the built-in signal QTreeWidget::itemClicked(QTreeWidget* item, int column) and simply cast the passed item:
    Qt Code:
    1. connect(treeWidget, SIGNAL(itemClicked(QTreeWidget*, int)), receiver, SLOT(doSomething(QTreeWidget*, int)));
    2.  
    3. void Receiver::doSomething(QTreeWidget* item, int column)
    4. {
    5. MyTreeWidgetItem* myItem = dynamic_cast<MyTreeWidgetItem*>(item);
    6. if (myItem)
    7. {
    8. // yes, it's a "MyTreeWidgetItem", do something with it's "element"
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Actually you could store the element information as QTreeWidgetItem's user data, without the need of subclassing QTreeWidgetItem.
    J-P Nurmi

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

    Shawn (4th September 2007)

  3. #2
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem emiting signal in QTreeWidgetItem's subclass

    Quote Originally Posted by jpn View Post
    Perhaps you could even use the built-in signal QTreeWidget::itemClicked(QTreeWidget* item, int column) and simply cast the passed item:
    Qt Code:
    1. connect(treeWidget, SIGNAL(itemClicked(QTreeWidget*, int)), receiver, SLOT(doSomething(QTreeWidget*, int)));
    2.  
    3. void Receiver::doSomething(QTreeWidget* item, int column)
    4. {
    5. MyTreeWidgetItem* myItem = dynamic_cast<MyTreeWidgetItem*>(item);
    6. if (myItem)
    7. {
    8. // yes, it's a "MyTreeWidgetItem", do something with it's "element"
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Actually you could store the element information as QTreeWidgetItem's user data, without the need of subclassing QTreeWidgetItem.
    I did what you said,
    Qt Code:
    1. QObject::connect(ui.treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(testslot(QTreeWidgetItem*, int)));
    2. void test::testslot(QTreeWidgetItem* item, int column)
    3. {
    4. QString str = item->text(column);
    5. QMessageBox::information(0, "testslot", str, QMessageBox::Ok);
    6. }
    To copy to clipboard, switch view to plain text mode 
    The strange thing is that the message box appears many times when I click on any item. 22 times on my laptop and 38 or 39 times on my workstation...

  4. #3
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem emiting signal in QTreeWidgetItem's subclass

    Quote Originally Posted by Shawn View Post
    The strange thing is that the message box appears many times when I click on any item. 22 times on my laptop and 38 or 39 times on my workstation...
    I test it using a local static variable, and found that the exact times the testslot has been called is 22. Why it behaves like this ?

  5. #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: Problem emiting signal in QTreeWidgetItem's subclass

    Do you call QObject::connect() in a loop?
    J-P Nurmi

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

    Shawn (4th September 2007)

  7. #5
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem emiting signal in QTreeWidgetItem's subclass

    Sorry, stupid mistake

    I connected in a recursion function...

    After replace it to the constractor, it works well

    Thanks very much for your help!

  8. #6
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem emiting signal in QTreeWidgetItem's subclass

    Quote Originally Posted by jpn View Post
    Perhaps you could even use the built-in signal QTreeWidget::itemClicked(QTreeWidget* item, int column) and simply cast the passed item:
    Qt Code:
    1. connect(treeWidget, SIGNAL(itemClicked(QTreeWidget*, int)), receiver, SLOT(doSomething(QTreeWidget*, int)));
    2.  
    3. void Receiver::doSomething(QTreeWidget* item, int column)
    4. {
    5. MyTreeWidgetItem* myItem = dynamic_cast<MyTreeWidgetItem*>(item);
    6. if (myItem)
    7. {
    8. // yes, it's a "MyTreeWidgetItem", do something with it's "element"
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Actually you could store the element information as QTreeWidgetItem's user data, without the need of subclassing QTreeWidgetItem.
    The program works now, but I still doesn't understand your code. Especially for the dynamic_cast function, how can it get the "element" information only by MyTreeWidgetItem's parent class ?

    If you have time, can you explain it a little bit? If you can recommend some metarial that I can study this by myself, I will appreciate it as well.

  9. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem emiting signal in QTreeWidgetItem's subclass

    Go to this address and download Thinking in C++( Bruce Eckel) and read the chapter on polymorphism: http://www.mindview.net/Books/DownloadSites.

    Regards

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

    Shawn (4th September 2007)

Similar Threads

  1. Replies: 3
    Last Post: 15th April 2007, 19:16

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.