Results 1 to 14 of 14

Thread: Question with QTreeWidgetItem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question with QTreeWidgetItem

    I still can't figure out how to do something like that; I've never had to do anything like that to manipulate a signal. Can you show me a way that you can catch a signal emitted from QTreeWidget with this code:

    Qt Code:
    1. QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
    2. cities->setText(0, tr("Places"));
    3. cities->setCheckState(0, Qt::Checked);
    To copy to clipboard, switch view to plain text mode 

    Whenever the checkbox for cities is toggled, I want to send a signal. Thank you very much for your help.

  2. #2
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 10 Times in 9 Posts

    Default Re: Question with QTreeWidgetItem

    After you create your QTreeWidgetItem you need to add it to a selectable QTreeWidget

    Qt Code:
    1. mTreeWidget->setSelectionMode(QAbstractItemView::MultiSelection);
    2. ...
    3. mTreeWidget->addTopLevelItem(cities);
    To copy to clipboard, switch view to plain text mode 

    Whenever any item in the tree is selected the tree emits an itemClicked signal which you connect to like

    Qt Code:
    1. QObject::connect(mTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(onClicked(QTreeWidgetItem*, int)));
    2.  
    3. onClicked(QTreeWidgetItem* item, int column)
    4. {
    5. //Do whatever you want to the item here
    6. }
    To copy to clipboard, switch view to plain text mode 

    You actually shouldn't need to set you objects checked state manually the tree will do that automatically

  3. #3
    Join Date
    Feb 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question with QTreeWidgetItem

    I've tried the code that you have provided. It compiles but it doesn't seem to jump to the slot. I have provided my code below.

    in treeview.h:

    Qt Code:
    1. private slots:
    2. void setChecked(QTreeWidgetItem *, int);
    To copy to clipboard, switch view to plain text mode 

    in treeview.cpp:

    Qt Code:
    1. QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
    2. cities->setText(0, tr("Places"));
    3. cities->setCheckState(0, Qt::Checked);
    4.  
    5. mTreeWidget->setSelectionMode(QAbstractItemView::MultiSelection);
    6. mTreeWidget->addTopLevelItem(cities);
    7.  
    8. QObject::connect(mTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(setChecked(QTreeWidgetItem*,int)));
    9.  
    10. void TreeView::setChecked(QTreeWidgetItem *item, int)
    11. {
    12. qDebug ("Cities Clicked");
    13. cout << "Cities Clicked" << endl;
    14. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, I've added a simple cout and qDebug to signify if the slot has been reached and it does not. Any suggestions?

  4. #4
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 10 Times in 9 Posts

    Default Re: Question with QTreeWidgetItem

    So your tree displays properly with the cities item in it and when you click the item in the gui the signal is not being triggered?

    Double check you are not getting any error messages other then that it's hard to debug your program with only code snippets

  5. #5
    Join Date
    Feb 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question with QTreeWidgetItem

    Here is my code for the treeview. Everything compiles, and I only get a warning for an unused parameter "item."

    treeview.h

    Qt Code:
    1. #ifndef TREEVIEW_H
    2. #define TREEVIEW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class QAction;
    7. class QCheckBox;
    8.  
    9. namespace Ui {
    10. class TreeView;
    11. }
    12.  
    13. class TreeView : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit TreeView(QWidget *parent = 0);
    19. ~TreeView();
    20.  
    21. private slots:
    22. void setChecked(QTreeWidgetItem *, int);
    23.  
    24. private:
    25. Ui::TreeView *ui;
    26. void setupTreeItems();
    27. // void manageTreeItems();
    28.  
    29. TreeView * treeView;
    30. //TreeView * check;
    31.  
    32. QAction *ascendingAction;
    33. QTreeWidget *treeWidget;
    34. QTreeWidget *mTreeWidget;
    35. };
    36.  
    37. #endif // TREEVIEW_H
    To copy to clipboard, switch view to plain text mode 

    treeview.cpp

    Qt Code:
    1. #include "treeview.h"
    2. //#include "ui_treeview.h"
    3. //#include "dialog.h"
    4.  
    5. #include <iostream>
    6. #include <QTreeWidget>
    7. #include <QInputDialog>
    8. #include <QCheckBox>
    9. #include <QStandardItem>
    10. #include <Qt>
    11. #include <iostream>
    12.  
    13. using namespace std;
    14.  
    15. TreeView::TreeView(QWidget *parent) :
    16. QMainWindow(parent)
    17. //ui(new Ui::TreeView)
    18. {
    19. //ui->setupUi(this);
    20.  
    21. treeWidget = new QTreeWidget(/*this*/);
    22. mTreeWidget = new QTreeWidget;
    23.  
    24. treeWidget->setColumnCount(1);
    25.  
    26. QStringList headers;
    27. headers << tr("Legend");
    28. treeWidget->setHeaderLabels(headers);
    29.  
    30. setupTreeItems();
    31.  
    32. setCentralWidget(treeWidget);
    33. setWindowTitle(tr("Tree Widget"));
    34. }
    35.  
    36. TreeView::~TreeView()
    37. {
    38. //delete ui;
    39. }
    40.  
    41. void TreeView::setupTreeItems()
    42. {
    43.  
    44.  
    45. QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
    46. cities->setText(0, tr("Places"));
    47. cities->setCheckState(0, Qt::Checked);
    48.  
    49. QTreeWidgetItem *citiesChild = new QTreeWidgetItem();
    50. cities->insertChild(0, citiesChild);
    51. citiesChild->setText(0, tr("USA Cities"));
    52. citiesChild->setCheckState(0, Qt::Checked);
    53.  
    54. mTreeWidget->setSelectionMode(QAbstractItemView::MultiSelection);
    55. mTreeWidget->addTopLevelItem(cities);
    56.  
    57. QObject::connect(mTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(setChecked(QTreeWidgetItem*,int)));
    58. }
    59.  
    60. void TreeView::setChecked(QTreeWidgetItem *item, int)
    61. {
    62. cout << "Cities Clicked" << endl;
    63. qDebug("Cities Clicked");
    64. }
    To copy to clipboard, switch view to plain text mode 

    Sorry for the lengthy post, I'm just really having trouble with this problem. When I do anything with the checkbox, it doesn't seem to jump to the slot. Do you have any suggestions?

  6. #6
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 10 Times in 9 Posts

    Default Re: Question with QTreeWidgetItem

    Ok I think one of the big problems you have is in the code you listed you declare 3 QTreeWidgets, one in the ui file, then one named "treewidget" and one named "mTreeWidget". The tree widget you actually see when you run the program is the one you declared in the ui file so you need to only use that one in your code.

    Qt Code:
    1. #ifndef TREEVIEW_H
    2. #define TREEVIEW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class QAction;
    7. class QCheckBox;
    8.  
    9. #include "ui_treeview.h"
    10.  
    11. class TreeView : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit TreeView(QWidget *parent = 0);
    17. virtual ~TreeView(); //In general always make destructors virtual
    18.  
    19. private slots:
    20. void setChecked(QTreeWidgetItem *, int);
    21.  
    22. private:
    23. Ui::TreeView ui;
    24. void setupTreeItems();
    25. QTreeWidget* getTreeWidget(){ return ui.whatever_the_widgets_name_is_in_the_ui_file; }
    26. QAction *ascendingAction;
    27. };
    28.  
    29. #endif // TREEVIEW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "treeview.h"
    2.  
    3. //#include "dialog.h"
    4.  
    5. #include <QDebug>
    6. #include <QTreeWidget>
    7. #include <QInputDialog>
    8. #include <QCheckBox>
    9. #include <QStandardItem>
    10. #include <Qt>
    11.  
    12. TreeView::TreeView(QWidget *parent) :
    13. QMainWindow(parent)
    14. {
    15. ui.setupUi(this);
    16.  
    17. //can be done in ui file
    18. getTreeWidget()->setSelectionMode(QAbstractItemView::MultiSelection);
    19.  
    20. //Connecting the tree widgets slot only needs to be done once, not for every item
    21. QObject::connect(getTreeWidget(), SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(setChecked(QTreeWidgetItem*,int)));
    22.  
    23. QStringList headers;
    24. qDebug() << "Legend: " << headers;
    25. getTreeWidget()->setHeaderLabels(headers);
    26.  
    27. setupTreeItems();
    28. }
    29.  
    30. TreeView::~TreeView()
    31. {
    32. }
    33.  
    34. void TreeView::setupTreeItems()
    35. {
    36. QTreeWidgetItem *cities = new QTreeWidgetItem(getTreeWidget(), QTreeWidgetItem::UserType + 1);
    37. cities->setText(0, tr("Places"));
    38.  
    39. QTreeWidgetItem *citiesChild = new QTreeWidgetItem();
    40. cities->addChild(citiesChild);
    41. citiesChild->setText(0, tr("USA Cities"));
    42.  
    43. getTreeWidget()->addTopLevelItem(cities);
    44. }
    45.  
    46. void TreeView::setChecked(QTreeWidgetItem *item, int)
    47. {
    48. if(item->type() == QTreeWidgetItem::UserType + 1)
    49. {
    50. qDebug() << "cities clicked";
    51. }
    52. else
    53. {
    54. qDebug() << "cities child clicked";
    55. }
    56. }
    To copy to clipboard, switch view to plain text mode 

    I hope that helps.

Similar Threads

  1. Replies: 5
    Last Post: 23rd September 2010, 13:58
  2. Question about QTreeWidgetItem
    By perny in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2010, 16:39
  3. Question about QTreeWidgetItem expand
    By santosh.kumar in forum Qt Programming
    Replies: 0
    Last Post: 30th April 2008, 05:32
  4. QTreeWidgetItem
    By Sarma in forum Qt Programming
    Replies: 1
    Last Post: 6th April 2006, 19:52
  5. QTreeWidgetItem
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 5th March 2006, 15:07

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.