QTreeWidget signals and slots not being triggered
-
I'm trying to make use of the QTreeWidget as follows and slots associated with the code below are not called and the return-value for connect(method) is zero. Could readers help me understand my mistakes?
Header File
@
#include <QMainWindow>
#include <QModelIndex>
#include <QTreeWidget>class test:public QMainWindow
{
Q_OBJECTpublic:
test(QWidget* parent = 0)
{
treeWidget = new QTreeWidget(0);
treeWidget->setWindowModality(Qt::ApplicationModal);
treeWidget->setWindowTitle("Select Well/Logs");
treeWidget->setSelectionMode(QAbstractItemView::NoSelection); //QAbstractItemView::MultiSelection
QStringList headerLabels;
headerLabels << "Field" << "Inline" << "CrossLine";treeWidget->setColumnCount(headerLabels.size()); treeWidget->setHeaderLabels(headerLabels); treeWidget->setAllColumnsShowFocus(true); treeWidget->resize(QSize(300,400)); treeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); treeWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); bool temp = connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(currItemClicked(QTreeWidgetItem*, int))); printf("value of temp for itemClicked = %d\n", temp); temp = connect(treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(currItemClicked(QTreeWidgetItem*, int))); printf("value of temp for itemChanged = %d\n", temp); setupTreeItems(); temp = connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(dblClick(QModelIndex))); printf("value of temp for dblClick = %d\n", temp);
}
QTreeWidget* getTreeWidget()const { return treeWidget;}
void currItemClicked(QTreeWidgetItem* item, int col);
void setTreeItems(const std::multimap<std::string, std::string>& mapData) {}private:
void setupTreeItems();QTreeWidget* treeWidget;
};@
Source file (test.cpp)
@
#include <test.h>
#include <QApplication>
#include <QDialog>
#include <QTreeWidget>void test::setupTreeItems()
{
QStringList header;
header << "One" << "Two" << "Three";foreach(QString nms, header)
{
QTreeWidgetItem* field_ele = new QTreeWidgetItem(treeWidget, QStringList(QString(nms)));
field_ele->setCheckState(0, Qt::Unchecked);
field_ele->setFlags(Qt::ItemIsEnabled|Qt::ItemIsTristate|Qt::ItemIsUserCheckable);
}
treeWidget->resizeColumnToContents(0);}
void test::currItemClicked(QTreeWidgetItem *item, int col)
{
printf("itemp->text = %s"n", qPrintable(item->text(0)));
}int main(int argc, char** argv)
{
QApplication app(argc, argv);
QDialog *parent = new QDialog();
parent->setVisible(false);test* testTreeWidget = new test(parent); QTreeWidget* treeWidget = testTreeWidget->getTreeWidget(); parent->show(); treeWidget->show(); //If parent->show() is uncommented, then I can modify //select/unselect qtreewidget items. parent->exec(); return 0;
}
@Qt-4.8.4(opensource-CentOS)
-
Use qDebug() instead of printf. Could be that you are simply not seeing the output that is being generated. In line 13. put your QMainWindow as parent to get a nice hierarchy:
@
treeWidget = new QTreeWidget(this);// Then in main() you don't need to get and show your treewidget;
// it will be done automatically when you show main window.
// Also, you don't need that strange QDialog as parent of QMainWindow...
// this is all obsolete, as far as I can tell
@Finally, to make it work:
In line 39. you need to mark your slots as slots:
@
public slots:
void currItemClicked(QTreeWidgetItem* item, int col);
void setTreeItems(const std::multimap<std::string, std::string>& mapData) {}
@