Question with QTreeWidgetItem
I have a TreeView class with QTreeWidgetItem's set as check boxes with the following code:
Code:
cities->setText(0, tr("Places"));
cities->setCheckState(0, Qt::Checked);
I try to send a signal with the following code:
Code:
connect(cities,
SIGNAL(itemClicked
(QTreeWidgetItem *,
int)),
this, setChecked
);
When I compile the code, the following errors come up:
Code:
treeview.
cpp:56: error
: no matching function
for call to ‘TreeView
::connect(QTreeWidgetItem*&,
const char*, TreeView
* const, <unresolved overloaded function type>
)’
qobject.
h:209: candidates are
: static bool QObject::connect(const QObject*,
const char*,
const QObject*,
const char*, Qt
::ConnectionType)
qobject.
h:314: bool QObject::connect(const QObject*,
const char*,
const char*, Qt
::ConnectionType) const
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.
Re: Question with QTreeWidgetItem
The correct syntax is
Code:
connect(cities,
SIGNAL(itemClicked
(QTreeWidgetItem *,
int)),
this,
SLOT(setChecked
());
Re: Question with QTreeWidgetItem
Oh sorry about that, I have corrected it:
Code:
connect(cities,
SIGNAL(itemClicked
(QTreeWidgetItem *,
int)),
this,
SLOT(setChecked
()));
But still recieved the same errors. Any ideas?
Re: Question with QTreeWidgetItem
Have you declared setChecked slot in you TreeView class?
Re: Question with QTreeWidgetItem
Yes I have. Here is all of the code that I have pertaining to it:
in treeview.h
Code:
public slots:
virtual void setChecked();
in treeview.cpp
Code:
connect(cities,
SIGNAL(itemClicked
(QTreeWidgetItem *,
int)),
this,
SLOT(setChecked
()));
void TreeView::setChecked()
{
if(Qt::Checked)
{
std::cout << "Checked\n" << std::endl;
//citiesChild->setCheckState(0, Qt::Checked);
}
else
{
std::cout << "Unchecked\n" << std::endl;
}
}
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
Code:
:: error: collect2: ld returned 1 exit status
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:
Code:
treeview.
cpp:56: error
: no matching function
for call to ‘TreeView
::connect(QTreeWidgetItem*&,
const char*, TreeView
* const,
const char*)’
/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)
/opt
/qtsdk
-2010.04/qt
/include
/QtCore
/qobject.
h:314: bool QObject::connect(const QObject*,
const char*,
const char*, Qt
::ConnectionType) const
any ideas?
Re: Question with QTreeWidgetItem
The itemClicked signal is emitted from QTreeView!!
The correct use is
Code:
connect(treeView,
SIGNAL(itemClicked
(QTreeWidgetItem *,
int)),
this,
SLOT(setChecked
()));
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?
Re: Question with QTreeWidgetItem
QTreeWidgetItem doesn't emit signals itself.
You can catch signal emitted from QTreeWidget and write slot like this
Code:
{
if (item == this->cities) {
if (Qt::Checked == item->checkState(0))
qDebug ("Checked");
else
qDebug ("Unchecked");
}
}
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:
Code:
cities->setText(0, tr("Places"));
cities->setCheckState(0, Qt::Checked);
Whenever the checkbox for cities is toggled, I want to send a signal. Thank you very much for your help.
Re: Question with QTreeWidgetItem
After you create your QTreeWidgetItem you need to add it to a selectable QTreeWidget
Code:
...
mTreeWidget->addTopLevelItem(cities);
Whenever any item in the tree is selected the tree emits an itemClicked signal which you connect to like
Code:
{
//Do whatever you want to the item here
}
You actually shouldn't need to set you objects checked state manually the tree will do that automatically
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:
in treeview.cpp:
Code:
cities->setText(0, tr("Places"));
cities->setCheckState(0, Qt::Checked);
mTreeWidget->addTopLevelItem(cities);
{
qDebug ("Cities Clicked");
cout << "Cities Clicked" << endl;
}
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?
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
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
Code:
#ifndef TREEVIEW_H
#define TREEVIEW_H
#include <QMainWindow>
namespace Ui {
class TreeView;
}
{
Q_OBJECT
public:
explicit TreeView
(QWidget *parent
= 0);
~TreeView();
private slots:
private:
Ui::TreeView *ui;
void setupTreeItems();
// void manageTreeItems();
TreeView * treeView;
//TreeView * check;
};
#endif // TREEVIEW_H
treeview.cpp
Code:
#include "treeview.h"
//#include "ui_treeview.h"
//#include "dialog.h"
#include <iostream>
#include <QTreeWidget>
#include <QInputDialog>
#include <QCheckBox>
#include <QStandardItem>
#include <Qt>
#include <iostream>
using namespace std;
TreeView
::TreeView(QWidget *parent
) : //ui(new Ui::TreeView)
{
//ui->setupUi(this);
treeWidget->setColumnCount(1);
headers << tr("Legend");
treeWidget->setHeaderLabels(headers);
setupTreeItems();
setCentralWidget(treeWidget);
setWindowTitle(tr("Tree Widget"));
}
TreeView::~TreeView()
{
//delete ui;
}
void TreeView::setupTreeItems()
{
cities->setText(0, tr("Places"));
cities->setCheckState(0, Qt::Checked);
cities->insertChild(0, citiesChild);
citiesChild->setText(0, tr("USA Cities"));
citiesChild->setCheckState(0, Qt::Checked);
mTreeWidget->addTopLevelItem(cities);
}
{
cout << "Cities Clicked" << endl;
qDebug("Cities Clicked");
}
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?
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.
Code:
#ifndef TREEVIEW_H
#define TREEVIEW_H
#include <QMainWindow>
#include "ui_treeview.h"
{
Q_OBJECT
public:
explicit TreeView
(QWidget *parent
= 0);
virtual ~TreeView(); //In general always make destructors virtual
private slots:
private:
Ui::TreeView ui;
void setupTreeItems();
QTreeWidget* getTreeWidget
(){ return ui.
whatever_the_widgets_name_is_in_the_ui_file;
} };
#endif // TREEVIEW_H
Code:
#include "treeview.h"
//#include "dialog.h"
#include <QDebug>
#include <QTreeWidget>
#include <QInputDialog>
#include <QCheckBox>
#include <QStandardItem>
#include <Qt>
TreeView
::TreeView(QWidget *parent
) :{
ui.setupUi(this);
//can be done in ui file
//Connecting the tree widgets slot only needs to be done once, not for every item
qDebug() << "Legend: " << headers;
getTreeWidget()->setHeaderLabels(headers);
setupTreeItems();
}
TreeView::~TreeView()
{
}
void TreeView::setupTreeItems()
{
cities->setText(0, tr("Places"));
cities->addChild(citiesChild);
citiesChild->setText(0, tr("USA Cities"));
getTreeWidget()->addTopLevelItem(cities);
}
{
{
qDebug() << "cities clicked";
}
else
{
qDebug() << "cities child clicked";
}
}
I hope that helps.