Results 1 to 3 of 3

Thread: Qt signal/slot auto connect problem in TREE

  1. #1
    Join Date
    Mar 2010
    Posts
    54
    Qt products
    Qt/Embedded
    Platforms
    Symbian S60

    Default Qt signal/slot auto connect problem in TREE

    Hi
    I have created an app , where I click a node of a tree, it would open a website.
    my problem is that , it should work only for a particular node, but currently it works for every node.
    the code for creating the tree is

    Qt Code:
    1. //creates a child of a root
    2. QTreeWidgetItem* SettingsTree::createItem(const QString &text, QTreeWidgetItem *parent, int index)
    3. {
    4. QTreeWidgetItem *after = 0;
    5. if (index != 0)
    6. after = childAt(parent, index - 1);
    7.  
    8. if (parent)
    9. item = new QTreeWidgetItem(parent, after);
    10. else
    11. item = new QTreeWidgetItem(this, after);
    12.  
    13. item->setText(0, text);
    14. return item;
    15. }
    16.  
    17.  
    18. //finds the child at particular index
    19. QTreeWidgetItem *SettingsTree::childAt(QTreeWidgetItem *parent, int index)
    20. {
    21. if (parent)
    22. return parent->child(index);
    23. else
    24. return topLevelItem(index);
    25. }
    26. //tree 1 created
    27. QTreeWidgetItem* SettingsTree::eBeamTREE()
    28. {
    29.  
    30. root->setText(0,"EBEAM GALLERY");
    31. for(int i=0;i<1;i++)
    32. {
    33.  
    34. createItem("Art & Design",root,0);
    35. createItem("Classroom Tools",root,1);
    36. createItem("Dynamic Content",root,2);
    37. createItem("Geography",root,3);
    38. createItem("Health",root,4);
    39. createItem("History & Social Studies",root,5);
    40. createItem("Literature & Language Arts",root,6);
    41. createItem("Mathematics",root,7);
    42. createItem("Music & Theatre",root,8);
    43. createItem("Recreation & Games",root,9);
    44. createItem("Science",root,10);
    45. createItem("Backgrounds",root,11);
    46. createItem("Tips & Tricks",root,12);
    47.  
    48. }
    49. return root;
    50. }
    51.  
    52. //tree 2
    53. QTreeWidgetItem* SettingsTree::eBeamMYGALLERY()
    54. {
    55.  
    56. root1->setText(0,"MY GALLERY");
    57. for(int i =0;i<1;i++)
    58. {
    59. createItem("My Gallery",root1,0);
    60. }
    61. return root1;
    62. }
    63.  
    64.  
    65. //tree3
    66. QTreeWidgetItem* SettingsTree::eBeamRESOURCETREE()
    67. {
    68. root2->setText(0,"RESOURCES");
    69. for(int i=0;i<1;i++)
    70. {
    71. child = createItem("eBeam Web Resources",root2,0);
    72.  
    73. }
    74. root2->setFlags(Qt::ItemIsSelectable);
    75. return child;
    76. }
    To copy to clipboard, switch view to plain text mode 

    Codeto connect to a website
    Qt Code:
    1. void MainWindow::openwebsite()
    2. {
    3. while(*it)
    4. {
    5. if((*it)->text(0)=="eBeam Web Resources")
    6. {
    7. QUrl url(QString("http://www.e-beam.com/education/web-resources.html"));
    8. bool flag = QDesktopServices::openUrl(url);
    9. if (flag == false)
    10. {
    11. qDebug()<<"Error Opening the website";
    12. }
    13. }
    14.  
    15. ++it;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt signal/slot auto connect problem in TREE

    maybe the Problem is at the string comparison.are you sure, that text(0) is another string for the other nodes? try the following code and debug it.

    Qt Code:
    1. QString test = (*it)->text(0);
    2. if(test==QString("eBeam Web Resources"))
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt signal/slot auto connect problem in TREE

    I would put the url in a custom role (Qt::UserRole or higher) and fetch it from the item. If an url is set, you open it. If not, you open nothing.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. Replies: 2
    Last Post: 15th September 2010, 00:54
  2. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 20:42
  3. problem connect signal - slot
    By jaca in forum Newbie
    Replies: 13
    Last Post: 9th March 2010, 19:38
  4. qt signal/slot auto-connect issue
    By parnedo in forum Qt Programming
    Replies: 9
    Last Post: 16th July 2009, 12:55
  5. A signal/slot connect isn't working.
    By Daimonie in forum Qt Programming
    Replies: 6
    Last Post: 15th February 2009, 22:55

Tags for this Thread

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.