qt中QtreeWidget与QstackWidget关联的问题
过程:要做一个图书管理系统,主界面是类似于这样的
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QStackedWidget>
#include<QTreeWidget>
#include<QTreeWidgetItem>
#include<QString>
#include<QLabel>
#include<QGridLayout>
#include<QHBoxLayout>
#include<QStringList>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private:
QStackedWidget *stackWindow;
QHBoxLayout *mainLayout;
QTreeWidget *treeWidget;
QTreeWidgetItem *parentItem1;
QTreeWidgetItem *parentItem2;
QTreeWidgetItem *childItem1;
QTreeWidgetItem *childItem2;
QLabel *parentLable1;
QLabel *parentLable2;
QLabel *childLable1;
QLabel *childLable2;
public slots:
void change1(QTreeWidgetItem* item1,int count1);
};
#endif // WIDGET_H
#include "widget.h"
#include<QObject>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//connect(treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(change1(QTreeWidgetItem*,int)));
stackWindow =new QStackedWidget;
mainLayout =new QHBoxLayout;
treeWidget =new QTreeWidget(this);
parentItem1=new QTreeWidgetItem(treeWidget,QStringList("parent1"));
parentItem2=new QTreeWidgetItem(treeWidget,QStringList("parent2"));
childItem1=new QTreeWidgetItem(parentItem1,QStringList("child1"));
childItem2=new QTreeWidgetItem(parentItem2,QStringList("child2"));
parentLable1=new QLabel("parentWindow1");
parentLable2=new QLabel("parentWindow2");
childLable1=new QLabel("childLable1");
childLable2=new QLabel("childLable2");
stackWindow->addWidget(parentLable1);
stackWindow->addWidget(parentLable2);
stackWindow->addWidget(childLable1);
stackWindow->addWidget(childLable2);
mainLayout->addWidget(treeWidget);
mainLayout->addWidget(stackWindow);
// stackWindow->setCurrentIndex(2);
this->setLayout(mainLayout);
//QObject::connect(treeWidget,SIGNAL(itemPressed(QTreeWidgetItem*,int)),this,SLOT(change1(QTreeWidgetItem* ,int )));//注释1
QObject::connect(treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(change1(QTreeWidgetItem* ,int)));
}
Widget::~Widget()
{
}
void Widget::change1(QTreeWidgetItem *item1, int count1)
{
QTreeWidgetItem *item=item1;
if(item->text(count1)=="parent1")
{
stackWindow->setCurrentIndex(0);
}
else if(item->text(count1)=="parent2")
{
stackWindow->setCurrentIndex(1);
}
else if(item->text(count1)=="child1")
{
stackWindow->setCurrentIndex(2);
}
else if(item->text(count1)=="child2")
{
stackWindow->setCurrentIndex(3);
}
}