Results 1 to 14 of 14

Thread: Question with QTreeWidgetItem

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

    Default Question with QTreeWidgetItem

    I have a TreeView class with QTreeWidgetItem's set as check boxes with the following 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 

    I try to send a signal with the following code:

    Qt Code:
    1. connect(cities, SIGNAL(itemClicked(QTreeWidgetItem *,int)), this, setChecked);
    To copy to clipboard, switch view to plain text mode 

    When I compile the code, the following errors come up:

    Qt Code:
    1. treeview.cpp:56: error: no matching function for call to ‘TreeView::connect(QTreeWidgetItem*&, const char*, TreeView* const, <unresolved overloaded function type>)’
    2.  
    3. qobject.h:209: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
    4.  
    5. qobject.h:314: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
    To copy to clipboard, switch view to plain text mode 

    I'm not sure but I believe the reason for this is that there are no signals for QTreeWidgetItem. Please confirm if this is the reason, or if it's not, to show me a possible signal for it that will make it work. Thank you in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question with QTreeWidgetItem

    The correct syntax is
    Qt Code:
    1. connect(cities, SIGNAL(itemClicked(QTreeWidgetItem *,int)), this, SLOT(setChecked());
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

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

    Default Re: Question with QTreeWidgetItem

    Oh sorry about that, I have corrected it:

    Qt Code:
    1. connect(cities, SIGNAL(itemClicked(QTreeWidgetItem *,int)), this, SLOT(setChecked()));
    To copy to clipboard, switch view to plain text mode 

    But still recieved the same errors. Any ideas?

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question with QTreeWidgetItem

    Have you declared setChecked slot in you TreeView class?
    A camel can go 14 days without drink,
    I can't!!!

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

    Default Re: Question with QTreeWidgetItem

    Yes I have. Here is all of the code that I have pertaining to it:

    in treeview.h

    Qt Code:
    1. public slots:
    2. virtual void setChecked();
    To copy to clipboard, switch view to plain text mode 

    in treeview.cpp

    Qt Code:
    1. connect(cities, SIGNAL(itemClicked(QTreeWidgetItem *,int)), this, SLOT(setChecked()));
    2.  
    3. void TreeView::setChecked()
    4. {
    5. if(Qt::Checked)
    6. {
    7. std::cout << "Checked\n" << std::endl;
    8. //citiesChild->setCheckState(0, Qt::Checked);
    9. }
    10. else
    11. {
    12. std::cout << "Unchecked\n" << std::endl;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 


    Added after 5 minutes:


    Ok, I see that I accidentally made it a "public" slot instead of a "private" slot. But now I am recieving the error

    Qt Code:
    1. :: error: collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 

    The code works without all of the code that I have displayed above, so the problem is within it.

    Ok, I corrected that problem, but am now recieving the same errors as before:

    Qt Code:
    1. treeview.cpp:56: error: no matching function for call to ‘TreeView::connect(QTreeWidgetItem*&, const char*, TreeView* const, const char*)’
    2.  
    3. /opt/qtsdk-2010.04/qt/include/QtCore/qobject.h:209: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
    4.  
    5. /opt/qtsdk-2010.04/qt/include/QtCore/qobject.h:314: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
    To copy to clipboard, switch view to plain text mode 

    any ideas?
    Last edited by denumbaone; 1st March 2011 at 22:22.

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question with QTreeWidgetItem

    The itemClicked signal is emitted from QTreeView!!
    The correct use is
    Qt Code:
    1. connect(treeView, SIGNAL(itemClicked(QTreeWidgetItem *,int)), this, SLOT(setChecked()));
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

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

    Default Re: Question with QTreeWidgetItem

    What i'm trying to do with this program is when the "cities" is toggled, it sends a signal. What signal can I use to make this possible?

  8. #8
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question with QTreeWidgetItem

    QTreeWidgetItem doesn't emit signals itself.

    You can catch signal emitted from QTreeWidget and write slot like this
    Qt Code:
    1. void TreeView::setChecked(QTreeWidgetItem *item)
    2. {
    3. if (item == this->cities) {
    4. if (Qt::Checked == item->checkState(0))
    5. qDebug ("Checked");
    6. else
    7. qDebug ("Unchecked");
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  9. #9
    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.

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

    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

  11. #11
    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?

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

    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

  13. #13
    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?

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.