Results 1 to 20 of 42

Thread: QTreeWidget clicked signal

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget clicked signal

    I guess you are messing with the tab bar and tab widget.
    PLCconfig->show();
    I guess that should be tab_3->show();
    or main_tab_widget->show();

    I guess you are not showing the parent widget and somehow messing with tab widget.

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

    mkkguru (27th January 2010)

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

    Default Re: QTreeWidget clicked signal

    Quote Originally Posted by aamer4yu View Post
    I guess you are messing with the tab bar and tab widget.
    PLCconfig->show();
    I guess that should be tab_3->show();
    or main_tab_widget->show();

    I guess you are not showing the parent widget and somehow messing with tab widget.
    PLCconfig,ECLogic_Lnx are different ui names iam calling that ui's in tab bar and placing in main_tab_widget.,so me facing problem in writing the connections to treewidget,where items like plc,...etc has to click,when we click on plc (QTreewidgetItem) the PLCconfig(ui) has to open,then when we click on ECLOGIC-lnx(QTrreewidgetItem)the ECLogic-LNX(ui)has to open,.............

    please kindly go through the code and image(treewidget) which i posted and plz give me the solution.

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

    Default Re: QTreeWidget clicked signal

    It is hard to understand what you are saying but your code where you add new tab is wrong. For what do you need new QTabBar?
    It should be like this:
    Qt Code:
    1. main_tab_widget->insertTab(0, new plcconfiguration(main_tab_widget), "PLC");
    2. main_tab_widget->setCurrentIndex(0);
    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.

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

    Default Re: QTreeWidget clicked signal

    Quote Originally Posted by faldżip View Post
    It is hard to understand what you are saying but your code where you add new tab is wrong. For what do you need new QTabBar?
    It should be like this:
    Qt Code:
    1. main_tab_widget->insertTab(0, new plcconfiguration(main_tab_widget), "PLC");
    2. main_tab_widget->setCurrentIndex(0);
    To copy to clipboard, switch view to plain text mode 
    ok thankq,

    i tried ,i got the required solution but the connections to the treewidget not getting ,as iam attaching the treewidget image,when iam clicking the items in that the particular window(Gui )has to open
    Attached Images Attached Images

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

    Default Re: QTreeWidget clicked signal

    Can you finally explain what is a "connection to treewidget"?
    You said that now your code is working but it is not working... So can you decide if it is or it is not?
    I don't see how your picture can help us understand your problem?
    Maybe you can find someone to help you explain your problem in english or use some google translator to help you.
    And use CODE tags when pasting code.
    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.

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

    mkkguru (28th January 2010)

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

    Default Re: QTreeWidget clicked signal

    Quote Originally Posted by faldżip View Post
    Can you finally explain what is a "connection to treewidget"?
    You said that now your code is working but it is not working... So can you decide if it is or it is not?
    I don't see how your picture can help us understand your problem?
    Maybe you can find someone to help you explain your problem in english or use some google translator to help you.
    And use CODE tags when pasting code.
    do you know how we can use signals and slots in treewidget?

    do you know below code?

    connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)),this,SLOT(showPLC(QTreeWidgetItem *)));

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

    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.

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

    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.

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

    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.

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

    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

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

    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.

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

    mkkguru (28th January 2010)

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

    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

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

    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.

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

    mkkguru (28th 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.