qt项目视图
list view:清单视图
tree view :树视图
table view:表视图
column view:列视图
undo view:撤销命令视图
list view 用例
在list view 中加上选择项,标签给选择项加上点击跳出messagebox
#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* list_view;
private slots:
void slotClicked(const QModelIndex &idx) ;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
list_view = new QListView(this);
list_view->setGeometry(20,20,100,200);
QStringList qlist;
qlist.append("旅游");
qlist.append("运动");
qlist.append("娱乐");
QStringListModel *mod = new QStringListModel(qlist);
list_view->setModel(mod);
connect(list_view , SIGNAL(clicked(const QModelIndex)), this, SLOT(slotClicked(const QModelIndex &)));
}
Widget::~Widget()
{
delete ui;
}
void Widget::slotClicked(const QModelIndex &idx){
QMessageBox::information(NULL, "兴趣爱好", idx.data().toString());
}
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;
private:
void initTreeView() ;
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);
initTreeView();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initTreeView(){
sItemModel = new QStandardItemModel(ui->treeView);
sItemModel->setHorizontalHeaderLabels(QStringList() << QStringLiteral("编号") << QStringLiteral("学校"));
QList<QStandardItem*> item11;
QStandardItem *it1 = new QStandardItem(QString::number(1));
QStandardItem *it2 = new QStandardItem("北大");
item11.append(it1);
item11.append(it2);
sItemModel->appendRow(item11);
ui->treeView->setModel(sItemModel);
}