过程:要做一个图书管理系统,主界面是类似于这样的

左边是类似于树形空间的东西,当点击左边的左边的窗体的时候,右边的窗口也会跟着切换。
为了实现这个功能,必须要有两个控件,QTreeWidget和QstackWidget;
以下是具体代码
1.Widge.h头文件

#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

2.Widget.cpp具体的实现

#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);
}

}

注释1:
这两个连接中的槽函数的参数只能给出类型,而不能给出形参名;
这两个连接必须写在窗口初始化之后
 
转自 https://blog.csdn.net/u013377068/article/details/78405158

qt中QtreeWidget与QstackWidget关联的问题的更多相关文章

  1. Qt中的常用容器类(解释比较全面,有插图)

    在Qt库中为我们提供了一系列的基于模板的容器类.这些类可以被用来存储特定类型的项.例如,如果你需要一个大小可以变得QString数组,那么可以使用QVector<QString>. 这些容 ...

  2. 第38课 Qt中的事件处理(上)

    1. GUI程序原理回顾 (1)图形界面应用程序的消息处理模型 (2)思考:操作系统发送的消息如何转变为Qt信号 2. Qt中的事件处理 (1)Qt平台将系统产生的消息转换为Qt事件 ①Qt事件是一个 ...

  3. QT中的SOCKET编程(QT-2.3.2)

    转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/ QT中的SOCKET编程 2008-10-07 23:13 ...

  4. Qt中,当QDockWidget的父窗口是一个不可以拖动的QTabWidget的时候实现拖动的方法

    之前在做有关QDockWidget的内容时候遇到了瓶颈,那就是窗口弹出来之后拖动不了,也不可以放大和缩小,若是弹出来之后设置成了window的flags,也不可以拖动,而且也不是需要的效果. 1.弹出 ...

  5. Qt中事件处理的方法(三种处理方法,四种覆盖event函数,notify函数,event过滤,事件处理器。然后继续传递给父窗口。可观察QWidget::event的源码,它是虚拟保护函数,可改写)

    一.Qt中事件处理的方式   1.事件处理模式一 首先是事件源产生事件,最后是事件处理器对这些事件进行处理.然而也许大家会问, Qt中有这么多类的事件,我们怎么样比较简便的处理每个事件呢?设想,如果是 ...

  6. QT中QMainWindow、QWidget、QDialog

    QT中QMainWindow.QWidget.QDialog 简述 在分享所有基础知识之前,很有必要在这里介绍下常用的窗口-QWidget.QDialog.QMainWindow. 熟悉Qt的同学都应 ...

  7. Qt 中的事件处理(一)

    1.图形界面应用程序的消息处理模型 特点: 基于操作系统才能运行 GUI应用程序提供的功能必须由用户触发 用户操作界面时操作系统是第一个感知的 系统内核的消息通过事件处理转变成QT的信号 2. Qt中 ...

  8. Qt中常用知识点

    1:QRegExp 正则表达式 QRegExp regExp("[a-zA-Z][1-9][0-9]{0,2}"); xxx->setValidator(new QRegEx ...

  9. QT中自定义系统托盘的实现—c++语言为例

    将要介绍的是:QT中自定义系统托盘(systemtray)的一个Demo,希望能帮需要的读者快速上手. 前提假设是诸位已经知道QT中的signals .slot以及资源文件,所以关于这些不会再累述. ...

随机推荐

  1. LigerUi折叠与展开

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  2. contos mysql 删除

    yum remove mysql mysql-server mysql-libs compat-mysql51rm -rf /var/lib/mysqlrm /etc/my.cnf查看是否还有mysq ...

  3. 【LeetCode每天一题】Rotate Image(旋转矩阵)

    You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise). N ...

  4. 百度云同同步盘 mac版

    百度云同步盘

  5. 再次 WebAssembly 技术探讨

    上次说到你可以将C代码编译成web调用的js文件,当时,很兴奋.哈哈,我也误以为是系统级别的C编程呢! 哎,今天,告诉你一个残酷的事实是,只是C语言级别,不是系统级别.因为WebAssembly目标是 ...

  6. Css预处理器---Less(二)

    三.Less语法 (1)变量 //less代码 @nice-blue : #5B83AD; @light-blue : @nice-blue + #111; #header { color : @li ...

  7. 假如java类里的成员变量是自身的对象

    假如java类里的成员变量是自身的对象,则新建该类对象时内存中怎么分配空间,我感觉似乎死循环了. 不过我想的肯定是错的,因为很多类的成员变量是自身对象,并且绝对无错,举个例子: Class A{ pr ...

  8. html5-增强的表单

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  9. 【2017-04-25】winform公共控件、菜单和工具栏

    一.公共控件 公共控件很多的属性很多都相似,这些是大部分都相同的: +布局 - AutoSize:自动适应控件上文字内容- Location:位置- Margin:控件间的间距- Size:控件大小 ...

  10. c++学习笔记(二)-指针

    1. 指向数组的指针 int balance[5] = { 1000, 2, 3, 17, 50 }; int *ptr; ptr = balance; //ptr是指向数组balance的指针 // ...