1.List View:清单视图
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QListView>
//字符串列表模型
#include <QStringListModel>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
private:
QListView * listview1;
private slots:
void SlotClickedFunc(const QModelIndex &index);
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
resize(450,250);
//第一步:创建QListView类对象
listview1 = new QListView(this);
//设置控件的显示位置和大小
listview1->setGeometry(20,20,140,160);
//第二步:创建数据显示至清单视图
QStringList qlist;//创建数据显示至清单视图
qlist.append("运动类:篮球,足球");
qlist.append("娱乐类:电影,音乐");
qlist.append("游戏类:五子棋,象棋");
qlist.append("旅游类:国内,国外");
//第三步:利用数据列表创建数据显示模型进行实现
QStringListModel *listmode = new QStringListModel(qlist);
//
listview1->setModel(listmode);
connect(listview1,SIGNAL(clicked(const QModelIndex)),this,SLOT(SlotClickedFunc(const QModelIndex )));
}
Widget::~Widget()
{
delete ui;
}
void Widget::SlotClickedFunc(const QModelIndex &index)
{
QMessageBox::information(NULL,"兴趣爱好","你选择的类型为:\n"+index.data().toString());
}
2.Tree View:树视图
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStandardItemModel>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
public:
void InitTreeViewFUNC();//初始化树控件视图
QStandardItemModel *sItemModel;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
InitTreeViewFUNC();//在构造函数调用树视图控件
}
MainWindow::~MainWindow()
{
delete ui;
}
//初始化树控件视图
void MainWindow::InitTreeViewFUNC()
{
//1.构造Model
sItemModel = new QStandardItemModel(ui->treeView);
sItemModel->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("编号")<<QStringLiteral("初中部 | 高中部 "));
//创建一级节点,将它加入到sItemModel
QList<QStandardItem*> item11;
QStandardItem * itme1 = new QStandardItem(QString::number(1));
QStandardItem * itme2 = new QStandardItem("初中部");
item11.append(itme1);
item11.append(itme2);
sItemModel->appendRow(item11);//第一个一级节点
//二级节点,添加到第一个一级节点
QList<QStandardItem*> item112;
QStandardItem * itme1121 = new QStandardItem(QString::number(2));
QStandardItem * itme1122 = new QStandardItem("一年级");
item112.append(itme1121);
item112.append(itme1122);
itme1->appendRow(item112);
//三级节点,添加到第一个二级节点
QList<QStandardItem*> item1231;
QStandardItem * item12311 = new QStandardItem(QString::number(3));
QStandardItem * item12312 = new QStandardItem("一班");
item1231.append(item12311);
item1231.append(item12312);
itme1121->appendRow(item1231);
QList<QStandardItem*> item1232;
QStandardItem * item12321 = new QStandardItem(QString::number(3));
QStandardItem * item12322 = new QStandardItem("二班");
item1232.append(item12321);
item1232.append(item12322);
itme1121->appendRow(item1232);
QList<QStandardItem*> item1233;
QStandardItem * item12331 = new QStandardItem(QString::number(3));
QStandardItem * item12332 = new QStandardItem("三班");
item1233.append(item12331);
item1233.append(item12332);
itme1121->appendRow(item1233);
//第二个一级节点
QList<QStandardItem*> item12;
QStandardItem * itme3= new QStandardItem(QString::number(2));
QStandardItem * itme4 = new QStandardItem("高中部");
item12.append(itme3);
item12.append(itme4);
sItemModel->appendRow(item12);
//2.给QTreeView控件应用model;
ui->treeView->setModel(sItemModel);
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
public:
void InitTableViewFunc();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStandardItemModel>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
InitTableViewFunc();//调用自定义函数
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::InitTableViewFunc()
{
//1:添加表头,准备数据模型
QStandardItemModel *stuMode = new QStandardItemModel();
stuMode->setHorizontalHeaderItem(0,new QStandardItem(QObject::tr("学号")));
stuMode->setHorizontalHeaderItem(0,new QStandardItem(QObject::tr("姓名")));
stuMode->setHorizontalHeaderItem(0,new QStandardItem(QObject::tr("性别")));
stuMode->setHorizontalHeaderItem(0,new QStandardItem(QObject::tr("分数")));
//通过API函数将数据模型绑定到QTableView
ui->tableView->setModel(stuMode);
//设备表格列的宽度
ui->tableView->setColumnWidth(0,120);
//2.添加数据信息
stuMode->setItem(0,0,new QStandardItem("2022001"));
stuMode->setItem(0,1,new QStandardItem("张三"));
stuMode->setItem(0,2,new QStandardItem("男"));
stuMode->setItem(0,3,new QStandardItem("714"));
stuMode->setItem(1,0,new QStandardItem("2022002"));
stuMode->setItem(1,1,new QStandardItem("小花"));
stuMode->setItem(1,2,new QStandardItem("男"));
stuMode->setItem(1,3,new QStandardItem("720"));
stuMode->setItem(2,0,new QStandardItem("2022003"));
stuMode->setItem(2,1,new QStandardItem("小妮"));
stuMode->setItem(2,2,new QStandardItem("男"));
stuMode->setItem(2,3,new QStandardItem("657"));
//禁止编辑
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
//排序操作
stuMode->sort(3,Qt::DescendingOrder);
}
二、Qt 项目部件(ltem Widgets)
1.List Widget:I清单控件。
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QListWidget>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QListWidgetItem *qitem =new QListWidgetItem("沁园春.雪--作者:毛主席");
ui->listWidget->addItem(qitem);
qitem->setTextAlignment(Qt::AlignCenter |Qt:: AlignCenter );
QStringList slist;
slist<<"1.北国风光,千里冰封,万里雪飘";
slist<<"2.望长城内外,惟余莽莽,顿失滔滔";
ui->listWidget->addItems(slist);
}
Widget::~Widget()
{
delete ui;
}
2.Tree Widget: 树形控件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTreeWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//.c
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//1.添加第一级节点
QTreeWidgetItem * topItem1 = new QTreeWidgetItem(ui->treeWidget);
topItem1->setText(0,"清华大学");
topItem1->setCheckState(0,Qt::Checked);
ui->treeWidget->addTopLevelItem(topItem1);
//隐藏表头
ui->treeWidget->setHeaderHidden(true);
//展开节点
ui->treeWidget->expandAll();
//2.二级节点添加到一级节点的topItem1;
QTreeWidgetItem * topItem11 = new QTreeWidgetItem(topItem1);
topItem11->setText(0,"清华大学建筑学院");
topItem11->setCheckState(0,Qt::Checked);
QTreeWidgetItem * topItem12 = new QTreeWidgetItem(topItem1);
topItem12->setText(0,"清华大学土木学院");
topItem12->setCheckState(0,Qt::Checked);
QTreeWidgetItem * topItem13 = new QTreeWidgetItem(topItem1);
topItem13->setText(0,"清华大学信息科学学院");
topItem13->setCheckState(0,Qt::Checked);
QTreeWidgetItem * topItem14 = new QTreeWidgetItem(topItem1);
topItem14->setText(0,"清华大学马克思学院");
topItem14->setCheckState(0,Qt::Checked);
//3.添加第一级节点
QTreeWidgetItem * topItem2 = new QTreeWidgetItem(ui->treeWidget);
topItem2->setText(0,"北京大学");
topItem2->setCheckState(0,Qt::Checked);
ui->treeWidget->addTopLevelItem(topItem2);
QTreeWidgetItem * topItem21 = new QTreeWidgetItem(topItem2);
topItem21->setText(0,"北京大学马克思学院");
topItem21->setCheckState(0,Qt::Checked);
QTreeWidgetItem * topItem22 = new QTreeWidgetItem(topItem2);
topItem22->setText(0,"北京大学马克思学院");
topItem22->setCheckState(0,Qt::Checked);
QTreeWidgetItem * topItem23 = new QTreeWidgetItem(topItem2);
topItem23->setText(0,"北京大学马克思学院");
topItem23->setCheckState(0,Qt::Checked);
QTreeWidgetItem * topItem24 = new QTreeWidgetItem(topItem2);
topItem24->setText(0,"北京大学马克思学院");
topItem24->setCheckState(0,Qt::Checked);
QTreeWidgetItem * topItem25 = new QTreeWidgetItem(topItem2);
topItem25->setText(0,"北京大学马克思学院");
topItem25->setCheckState(0,Qt::Checked);
//topItem1->setText(0,"清华大学");
//topItem1->setText(0,"清华大学");
}
Widget::~Widget()
{
delete ui;
}
3.Table Widget: 表控件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//.c
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置不表格的行数和列数
ui->tableWidget->setRowCount(3);
ui->tableWidget->setColumnCount(2);
//设置水平表头
QStringList slist;
slist <<"学号"<< "高考分数";
ui->tableWidget->setHorizontalHeaderLabels(slist);
QList<QString> strno;
strno <<"202201"<<"202202"<<"202203";
QList<QString> strscore;
strscore <<"708"<<"712"<<"690";
//通过循环为表格赋值
for(int i = 0;i < 3;i++)
{
int iCol = 0;
QTableWidgetItem *pitem = new QTableWidgetItem(strno.at(i));
//添加到对应的控件
ui->tableWidget->setItem(i,iCol++,pitem);
ui->tableWidget->setItem(i,iCol++, new QTableWidgetItem(strscore.at(i)));
}
}
Widget::~Widget()
{
delete ui;
}