Results 1 to 20 of 42

Thread: QTreeWidget clicked signal

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    Default Re: QTreeWidget clicked signal

    Quote Originally Posted by mkkguru View Post
    do you know how we can use signals and slots in treewidget?
    Yes, I know, like in every other class.
    do you know below code?
    connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showPLC(QTreeWidgetItem *)));
    Yes, I know, you've pasted it before :]

    Ok, so please run this example and tell me if it is what you want, because I did what I understand you want but as I said, it is difficult to understand your needs.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. Widget(QWidget *parent = 0) : QWidget(parent) {
    8. treeWidget = new QTreeWidget(this);
    9. tabWidget = new QTabWidget(this);
    10. tabWidget->setTabsClosable(true);
    11. la->addWidget(treeWidget);
    12. la->addWidget(tabWidget);
    13. setLayout(la);
    14.  
    15. for (int i = 0; i < 2; ++i) {
    16. QTreeWidgetItem *p = new QTreeWidgetItem(treeWidget, QStringList() << QString("parent item %1").arg(i));
    17. for (int j = 0; j < 5; ++j) {
    18. QTreeWidgetItem *c = new QTreeWidgetItem(QStringList() << QString("child item %1%2").arg(i).arg(j));
    19. p->addChild(c);
    20. }
    21. }
    22. connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(slotItemClicked(QTreeWidgetItem*)));
    23. connect(tabWidget, SIGNAL(tabCloseRequested(int)), SLOT(slotCloseTab(int)));
    24. }
    25. private slots:
    26. void slotItemClicked(QTreeWidgetItem *item) {
    27. QWidget *w = itemToWidget.value(item, 0);
    28. if (w) {
    29. tabWidget->setCurrentWidget(w);
    30. return;
    31. }
    32. QLabel *label = new QLabel(item->text(0));
    33. itemToWidget.insert(item, label);
    34. widgetToItem.insert(label, item);
    35. label->setAlignment(Qt::AlignCenter);
    36. tabWidget->insertTab(0, label, item->text(0));
    37. tabWidget->setCurrentIndex(0);
    38. }
    39. void slotCloseTab(int index) {
    40. QWidget *w = tabWidget->widget(index);
    41. itemToWidget.remove(widgetToItem.value(w));
    42. widgetToItem.remove(w);
    43. tabWidget->removeTab(index);
    44. }
    45. private:
    46. QTabWidget *tabWidget;
    47. QTreeWidget *treeWidget;
    48. QMap<QTreeWidgetItem *, QWidget *> itemToWidget;
    49. QMap<QWidget *, QTreeWidgetItem *> widgetToItem;
    50. };
    51.  
    52. int main(int argc, char **argv)
    53. {
    54. QApplication a(argc, argv);
    55. Widget w;
    56. w.show();
    57. return a.exec();
    58. }
    59. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  2. #2
    Join Date
    Oct 2009
    Posts
    35
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11
    Thanks
    12

    Smile Re: QTreeWidget clicked signal

    Quote Originally Posted by faldżip View Post
    Yes, I know, like in every other class.

    Yes, I know, you've pasted it before :]

    Ok, so please run this example and tell me if it is what you want, because I did what I understand you want but as I said, it is difficult to understand your needs.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. Widget(QWidget *parent = 0) : QWidget(parent) {
    8. treeWidget = new QTreeWidget(this);
    9. tabWidget = new QTabWidget(this);
    10. tabWidget->setTabsClosable(true);
    11. la->addWidget(treeWidget);
    12. la->addWidget(tabWidget);
    13. setLayout(la);
    14.  
    15. for (int i = 0; i < 2; ++i) {
    16. QTreeWidgetItem *p = new QTreeWidgetItem(treeWidget, QStringList() << QString("parent item %1").arg(i));
    17. for (int j = 0; j < 5; ++j) {
    18. QTreeWidgetItem *c = new QTreeWidgetItem(QStringList() << QString("child item %1%2").arg(i).arg(j));
    19. p->addChild(c);
    20. }
    21. }
    22. connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(slotItemClicked(QTreeWidgetItem*)));
    23. connect(tabWidget, SIGNAL(tabCloseRequested(int)), SLOT(slotCloseTab(int)));
    24. }
    25. private slots:
    26. void slotItemClicked(QTreeWidgetItem *item) {
    27. QWidget *w = itemToWidget.value(item, 0);
    28. if (w) {
    29. tabWidget->setCurrentWidget(w);
    30. return;
    31. }
    32. QLabel *label = new QLabel(item->text(0));
    33. itemToWidget.insert(item, label);
    34. widgetToItem.insert(label, item);
    35. label->setAlignment(Qt::AlignCenter);
    36. tabWidget->insertTab(0, label, item->text(0));
    37. tabWidget->setCurrentIndex(0);
    38. }
    39. void slotCloseTab(int index) {
    40. QWidget *w = tabWidget->widget(index);
    41. itemToWidget.remove(widgetToItem.value(w));
    42. widgetToItem.remove(w);
    43. tabWidget->removeTab(index);
    44. }
    45. private:
    46. QTabWidget *tabWidget;
    47. QTreeWidget *treeWidget;
    48. QMap<QTreeWidgetItem *, QWidget *> itemToWidget;
    49. QMap<QWidget *, QTreeWidgetItem *> widgetToItem;
    50. };
    51.  
    52. int main(int argc, char **argv)
    53. {
    54. QApplication a(argc, argv);
    55. Widget w;
    56. w.show();
    57. return a.exec();
    58. }
    59. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Hi ,

    I really thanks ,its working for me ,thanks for the code...
    Last edited by mkkguru; 28th January 2010 at 10:24.

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    Default Re: QTreeWidget clicked signal

    I really hope you make some effort to understand this piece of code and not only copy-paste it to your application.
    And next time please try to describe your problem more clearly and attach image which could be helpful, because yours did not even contain any tab widget and we already know how tree widget looks like...
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. #4
    Join Date
    Oct 2009
    Posts
    35
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11
    Thanks
    12

    Default Re: QTreeWidget clicked signal

    Hi,

    iam attaching my project,u will now understand about my project, plz go through the below ..

    1)click on New menu ->new Project window has to open and ECLogic-LNX wil place in Treewidget.then
    2)click on ECLogic-LNX->ECLogic-LNX window has to open.
    3) click on PLC->PLC configrtn window has to open.
    4)click on CPU configuration ->cpu confi window '" " """""""""""""""""""""
    5)click on Program variable configuration->Program varbl config "" """"" """"
    6)click on IO configuration -> IO Config """"""""""""""""""""'
    7)click on BIN ->Bin """"""""""""""""""""""'

    But my problem iam getting in my project is when iam clicking on treewidget item all the windows are coming....
    plz help to get the solutions of above mention points..
    Attached Files Attached Files

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    Default Re: QTreeWidget clicked signal

    Man, read all answers to your posts once again, ok? It was already mentioned that this is wrong way:
    Qt Code:
    1. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showPLC(QTreeWidgetItem *)));
    2. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showpvc(QTreeWidgetItem *)));
    3. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showLDR()));
    4. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showIOC(QTreeWidgetItem*)));
    5. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showCHNL(QTreeWidgetItem *)));
    6. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showCPU(QTreeWidgetItem *)));
    7. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showSLT(QTreeWidgetItem *)));
    8. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showBIN(QTreeWidgetItem *)));
    9.  
    10. connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem* ,int)),this,SLOT(showItem(QTreeWidgetItem*)));
    To copy to clipboard, switch view to plain text mode 
    and read about Signals and slots in Qt Assistant and you will know why it is working wrong. There is even a picture showing how it is working.
    And the solution has already been given to you in previous posts.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

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

    mkkguru (28th January 2010)

  7. #6
    Join Date
    Oct 2009
    Posts
    35
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11
    Thanks
    12

    Default Re: QTreeWidget clicked signal

    Hi faldzip,

    I tried the previous post code ,which i used the single slot,there i written like below
    Qt Code:
    1. void ECLogic::showItem(QTreeWidgetItem* item )
    2. {
    3. if(item=="PLC")
    4. {
    5. PLCconfig=new plcconfiguration();
    6. main_tab_widget->insertTab(0,PLCconfig, "PLC");
    7. main_tab_widget->setCurrentIndex(0);
    8. PLCconfig->show();
    9. }
    10. else if(item==lnx)
    11. {
    12. ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
    13. ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
    14. ECLogic_Lnx->show();
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    but above condition is not working for me,can you please solve how to write that code......
    Last edited by wysota; 28th January 2010 at 20:14. Reason: Missing [code] tags

  8. #7
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    Default Re: QTreeWidget clicked signal

    What is the type of item and what is the type of "PLC"?
    And in your code lnx is a local variable in different method so how do you want to reference it in this method?
    I think you need to revise your C++ knowledge...
    Why can't you use QTreeWidgetItem methods like for example text() to get know what item did you clicked?
    You can event set your data with QTreeWidgetItem::setData() with Qt::UserRole while creating item to let you know what item it is.
    Qt Code:
    1. QTreeWidgetItem lnx = new QTreeWidgetItem(ui->treeWidget);
    2. lnx->setData(0, Qt::UserRole, "lnx");
    3.  
    4. // and check it:
    5. if (item->data().toString() == "lnx")
    To copy to clipboard, switch view to plain text mode 

    P.S. And use CODE tags for pasting code! Your code is already a mess in terms of formatting and without CODE tags it is even worse.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  9. The following user says thank you to faldzip for this useful post:

    mkkguru (28th January 2010)

  10. #8
    Join Date
    Oct 2009
    Posts
    35
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11
    Thanks
    12

    Default Re: QTreeWidget clicked signal

    Hi,
    I didnt mention any type to Item and PLC ,i just created as QTreewidgetItem *item and QTreeWidgetItem *plc ,please go through my attached project in previous post ,let me know what i done the mistakes in code,as iam new to Qt,this is the first project iam handling ,please feel free to answer to my post .
    thankq ..

  11. #9
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    Default Re: QTreeWidget clicked signal

    Quote Originally Posted by mkkguru View Post
    Hi,
    I didnt mention any type to Item and PLC ,i just created as QTreewidgetItem *item and QTreeWidgetItem *plc
    I mean C++ type... To make you things easier I can say that in this code:
    Qt Code:
    1. void ECLogic::showItem(QTreeWidgetItem* item )
    2. {
    3. if(item=="PLC")
    4. {
    5. PLCconfig=new plcconfiguration();
    6. main_tab_widget->insertTab(0,PLCconfig, "PLC");
    7. main_tab_widget->setCurrentIndex(0);
    8. PLCconfig->show();
    9. }
    10. else if(item==lnx)
    11. {
    12. ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
    13. ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
    14. ECLogic_Lnx->show();
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    item's type is QTreeWidgetItem* and "PLC" is const char *, so both are pointers. Do you want to compare two memory addresses of two completely different things? I don't think so...
    And again:
    i just created as QTreewidgetItem *item and QTreeWidgetItem *plc
    You have created them in another method as local variables so they not exist where you want to reference them. They are dead :]

    Your problem now is pure C++ issue. Please revise your C++ basics, like local variables, scopes, referencing pointers and so on.

    And your project your slot showItem looks:
    Qt Code:
    1. void ECLogic::showItem(QTreeWidgetItem* item )
    2. {
    3.  
    4. ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
    5. ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
    6. ECLogic_Lnx->show();
    7.  
    8. item << "PLC" << "CPU CONFIGURATION" << "PROGRAM VARIABLE CONFIGURATION" << "IO CONFIGURATION"
    9. << "BIN" << "SLOT" <<"LADDER PROGRAM";
    10.  
    11. if(item=="PLC")
    12. {
    13. PLCconfig=new plcconfiguration();
    14. main_tab_widget->insertTab(0,PLCconfig, "PLC");
    15. main_tab_widget->setCurrentIndex(0);
    16. PLCconfig->show();
    17. }
    18. else if(item==lnx)
    19. {
    20. ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
    21. ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
    22. ECLogic_Lnx->show();
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 
    Which does not make sense at all.

    EDIT:
    Oops now I found that there is another lnx variable which is class member - that would be better, but when you are creating new QTreeWidgetItem you are assigning it to the local variable with the same name (lnx) so your member variable lnx is always unassigned and referencing it will cause segmentation fault.
    Last edited by faldzip; 28th January 2010 at 16:04.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  12. The following user says thank you to faldzip for this useful post:

    mkkguru (30th January 2010)

  13. #10
    Join Date
    Oct 2009
    Posts
    35
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11
    Thanks
    12

    Default Re: QTreeWidget clicked signal

    Quote Originally Posted by faldżip View Post
    I mean C++ type... To make you things easier I can say that in this code:
    Qt Code:
    1. void ECLogic::showItem(QTreeWidgetItem* item )
    2. {
    3. if(item=="PLC")
    4. {
    5. PLCconfig=new plcconfiguration();
    6. main_tab_widget->insertTab(0,PLCconfig, "PLC");
    7. main_tab_widget->setCurrentIndex(0);
    8. PLCconfig->show();
    9. }
    10. else if(item==lnx)
    11. {
    12. ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
    13. ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
    14. ECLogic_Lnx->show();
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    item's type is QTreeWidgetItem* and "PLC" is const char *, so both are pointers. Do you want to compare two memory addresses of two completely different things? I don't think so...
    And again:

    You have created them in another method as local variables so they not exist where you want to reference them. They are dead :]

    Your problem now is pure C++ issue. Please revise your C++ basics, like local variables, scopes, referencing pointers and so on.

    And your project your slot showItem looks:
    Qt Code:
    1. void ECLogic::showItem(QTreeWidgetItem* item )
    2. {
    3.  
    4. ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
    5. ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
    6. ECLogic_Lnx->show();
    7.  
    8. item << "PLC" << "CPU CONFIGURATION" << "PROGRAM VARIABLE CONFIGURATION" << "IO CONFIGURATION"
    9. << "BIN" << "SLOT" <<"LADDER PROGRAM";
    10.  
    11. if(item=="PLC")
    12. {
    13. PLCconfig=new plcconfiguration();
    14. main_tab_widget->insertTab(0,PLCconfig, "PLC");
    15. main_tab_widget->setCurrentIndex(0);
    16. PLCconfig->show();
    17. }
    18. else if(item==lnx)
    19. {
    20. ECLogic_Lnx=new eclogic_lnx(main_tab_widget);
    21. ECLogic_Lnx->setWindowTitle(QString("ECLOGIC-LNX"));
    22. ECLogic_Lnx->show();
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 
    Which does not make sense at all.

    EDIT:
    Oops now I found that there is another lnx variable which is class member - that would be better, but when you are creating new QTreeWidgetItem you are assigning it to the local variable with the same name (lnx) so your member variable lnx is always unassigned and referencing it will cause segmentation fault.

    Hi,
    Thankq.

  14. #11
    Join Date
    Oct 2009
    Posts
    35
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11
    Thanks
    12

    Red face Re: QTreeWidget clicked signal

    Hi All,
    i got my requirement from below code ,thanks all.

    Qt Code:
    1. void xxx::xxx()
    2. {
    3. ]if (item->data(0,Qt::UserRole) == "PLC")
    4. {
    5. PLCconfig=new plcconfiguration();
    6. scrollArea1=new QScrollArea;
    7. scrollArea1->setWidget(PLCconfig);
    8. main_tab_widget->insertTab(0,scrollArea1, "PLC");
    9. itemToWidget.insert(item,scrollArea1);
    10. widgetToItem.insert(scrollArea1,item);
    11. main_tab_widget->setCurrentIndex(0);
    12. PLCconfig->show();
    13. }
    To copy to clipboard, switch view to plain text mode 

    As i need to have many modifications in my project ,plz welcome my threads in near future.....
    thanks .
    Last edited by wysota; 30th January 2010 at 13:32. Reason: Missing [code] tags

  15. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QTreeWidget clicked signal

    Quote Originally Posted by faldżip View Post
    P.S. And use CODE tags for pasting code! Your code is already a mess in terms of formatting and without CODE tags it is even worse.
    You can "reward" the author with negative reputation if you feel he is not behaving well by clicking the star icon under his post.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. The following user says thank you to wysota for this useful post:

    mkkguru (30th January 2010)

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.