Hello Qt -- QT unit view component

1, QListView list view

1. QListView component introduction

QListView list view, inherited from QAbstractItemView. QListView is a model-based list / icon view without displaying header and table box, which provides a more flexible way for Qt's model / view structure.

2. QListView component properties

QListView component property settings:

A. Name: the name in the source code corresponding to the component.

B. Font: set the font in the view.

C. batchSize: if layoutMode is set to Batched, this attribute saves the specifications of batch processing.

D. layoutModel: the layout mode of the project.

E. Modelcolumn: the class visible in the model. By default, it is set to 0 to indicate that the first column in the model is visible.

F. viewModel: saves the view model of the ListView component.

3. Common member functions of QListView component

QListView(QWidget *parent = 0)

Construct a ListView whose parent object is parent.

void currentChanged(const QModelIndex ¤t,const QModelIndex &previous) [virtual protected]

Locate current as the current project and previous as the previous project.

void dataChanged(const QModelIndex &topLeft,const QModelIndex &bottomRight) [virtual protected]

Change the items in the model from topLeft to bottomRight.

QModelIndex indexAt(const QPoint &p) const [virtual]

Returns the model index of the item at coordinate point p.

void rowsInserted(const QModelIndex &parent,int start,int end) [virtual protected]

Insert a new line. The parent of the new line is parent. All items from start to end.

QModelIndexList selectedIndexes() const[virtual protected]

Returns the model index of all selected non hidden items.

void setViewMode(ViewMode mode)

Set the display mode, including QListView::ListMode, QListView::IconMode, and the default ListMode

void setLayoutMode(LayoutMode mode)

Set the layout mode, QListView::SinglePass, QListView::Batched

virtual void QAbstractItemView::setModel(QAbstractItemModel * model)

Set the model associated with the view

4. QListView instance

Widget.h documents:

#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui/QWidget>
#include <QStringListModel>
#include <QListView>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>

class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(const QStringList &leaders, QWidget *parent = 0);
    ~Widget();
private:
    QStringListModel *model;
    QListView *listView;
    QDialogButtonBox *buttonBox;
private slots:
    void onDelete();
    void onInsert();
};

#endif // WIDGET_H

Widget.cpp file:

#include "widget.h"

Widget::Widget(const QStringList &leaders, QWidget *parent)
: QWidget(parent)
{
    model = new QStringListModel(this);
    model->setStringList(leaders);
    listView = new QListView;
    listView->setModel(model);
    listView->setEditTriggers(QAbstractItemView::AnyKeyPressed
                              | QAbstractItemView::DoubleClicked);
    buttonBox = new QDialogButtonBox;
    QPushButton *insertButton = buttonBox->addButton(
                tr("&Insert"),QDialogButtonBox::ActionRole);
    QPushButton *deleteButton = buttonBox->addButton(
                tr("&Delete"), QDialogButtonBox::ActionRole);
    connect(insertButton, SIGNAL(clicked()), this, SLOT(onInsert()));
    connect(deleteButton, SIGNAL(clicked()), this, SLOT(onDelete()));
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(listView);
    layout->addWidget(buttonBox);
    setLayout(layout);

    setWindowTitle(tr("QStringListModel"));
}

Widget::~Widget()
{

}

void Widget::onDelete()
{
    model->removeRows(listView->currentIndex().row(), 1);
}

void Widget::onInsert()
{
    int row = listView->currentIndex().row();
    model->insertRows(row, 1);
    QModelIndex index = model->index(row);
    listView->setCurrentIndex(index);
    listView->edit(index);
}

Main.cpp file:

#include <QtGui/QApplication>

#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QStringList list;
    list << QObject::tr("xianziyu")
         << QObject::tr("yangwangming")
         << QObject::tr("yinshixin")
         << QObject::tr("baichengshuang")
         << QObject::tr("zhangzurui");

    Widget w(list);

    w.show();

   
    return a.exec();
}

2, QTreeView tree view

1. Introduction to QTreeView component

QTreeView tree view, inherited from QAbstractItemView, is a model-based list / icon view and a part of Qt model / view framework.

2. QTreeView component properties

QTreeView component property setting options:

A. Name: the name in the source code corresponding to the component.

B. Font: set the font of all text in the component.

C. sortingEnable: whether items are sorted.

3. Common member functions of QTreeView component

QTreeView(QWidget *parent = 0)

Construct a TreeView whose parent object is parent.

void collapse(const QModena &index) [slot]

Collapses items whose model index is index

void collapseAll() [slot]

Collapse all items

int columnAt(int x) const

Returns the column at the x coordinate.

void columnCountChanged(int oldCount,int newCount) [protected slot]

Notify the number of columns in the tree view to change from oldCount to newCount.

void currentChanged(const QModelIndex ¤t,const QModelIndex &previous)[virtual protected]

Set current as the current project and previous as the previous current project

void dataChanged(const QModelIndex &topLeft,const QModelIndex &bottomRight)[virtual]

Change the items in the model from topLeft to bottomRight.

void drawBranches(QPainter *painter,const QRect &rect,const QModelIndex &index) const

On the same line of the item index, draw the rect rectangular branch specified with painter.

void drawRow(QPainter *painter,const QStyleOptionViewItem &option,
            const QModelIndex &index) const [virtual protected]

Draw a new line with painter. The new line contains the items whose model index is index. option is how to display the items.

void QTreeView::drawTree(QPainter *painter,const QRegion& ion)const [protected]

Using painter to draw the tree in the region

void expand(const QModelIndex &index) [slot]

Expand the item whose model index is index.

void expandAll() [slot]

Expand all projects

void expandToDepth(int depth) [slot]

Expand the item in the tree view to depth

QHeaderView *header() const

The tree view of the header is returned

QModelIndex indexAbove(const QModelIndex &index) const

Returns the previous index of the model index

 QModelIndex indexAt(const QPoint &point) const [virtual]

Returns the model index of the item at point

QModelIndex indexBelow(const QModelIndex &index) const

Returns the next index of the model index

bool isExpanded(const QModelIndex &index) const

If the item at the index of the model index is expanded, return true; otherwise, return false

void rowsInserted(const QModelIndex &parent,int start,int end) [virtual protected]

Insert a new line whose parent is parent, including all items from start to end.

void rowsRemoved(const QModelIndex &parent,int start,int end) [protected slot]

Delete the line whose parent is parent, including all items from start to end

void selectAll() [virtual]

Set all items to be selected

QModelIndexList selectedIndexes() const [virtual protected]

Returns the model index of all selected and non hidden items

void setHeader(QHeaderView *header)

Set the title of the TreeView to header

void sortByColumn(int column,QT::SortOrder order)

Sort column s by order

4. QTreeView instance

#include <QtGui>
#include <QString>
#include <QTextCodec>
#include <QSplitter>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QSplitter *splitter = new QSplitter;
    QDirModel *model = new QDirModel;

    //Create data from default directory
    QTreeView *tree = new QTreeView(splitter);
    tree->setModel(model);
    tree->setRootIndex(model->index("d:\\"));
    QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));   //Chinese display
    QTextCodec *codec = QTextCodec::codecForLocale();
    QString s = codec->toUnicode("catalogue");

    splitter->setWindowTitle(s);
    splitter->show();

   
    return a.exec();
}

3, QTableView table view

1. Introduction to QTableView component

QTableView table view is a table view implementation of model / view structure, which is used to display model items. QTableView provides the standard table provided by QTable class. It is a part of Qt's model / view framework. It is implemented by the interface defined by QAbstractItemView class to enable it to display the data provided by the model derived from QAbstractItemModel class. QTableView can use a custom data model to display content.

2. QTableView component properties

QTableView component property setting options:

A. Name: the name of the control in the corresponding source code

B. Font: set the font inside the table

C. Corner button enabled: is the button in the upper left corner useful

D. gridStyle: format of table

E. showGrid: whether to display the grid. If the value is true, it will be displayed; otherwise, it will not be displayed

F. sortingEnabled: whether to sort items

3. QTableView component common member functions

QTableView(QWidget *parent = 0)

Construct a TableView whose parent object is parent

 void clearSpans()

Deletes the span of all rows and columns in the TableView

 int columnAt(int x) const

Returns the column at coordinate x, or - 1 if there is no item at coordinate

 int columnSpan(int row,intcolumn) const

Returns the row span at row and column

void currentChanged(const QModelIndex& t,const QModelIndex &previous)[virtual protected]

Specify current as the current project and previous as the previous project

QHeaderView *horizontalHeader() const

Returns the horizontal title of the TableView

QModelIndex indexAt(const QPoint *pos) const [virtual]

Returns the model index of the item at the point pos

int rowAt(int y)const

Returns the row at coordinate y, or - 1 if there is no item at coordinate

 int rowSpan(int row,int column) const

Returns the column span at row, column, and column

void selectcolumn(int column) [slot]

Set column as selected

void selectRow(int row) [slot]

Set row as selected

QModelIndexList selectedIndexes() const [virtual protected]

Returns the model index of all selected and non hidden items

void setHorizontalHeader(QHeaderView *header)

Set the horizontal title of TableView to header

void setSpan(int row,int column,int rowSpanCount,int columnCount)

Set row span to rowSpanCount and column span to columnSpanCount at row and column

void setVerticalHeader(QHeaderView *header)

Set the vertical title of TableView to header

void showColumn(int column)[slot]

Show column column

void showRow(int row) [slot]

Show row

QHeaderView *verticalHeader() const

Returns the vertical title of the TableView

4. QTableView instance

#include <QtGui/QApplication>
#include <QTableView>
#include <QStandardItemModel>
#include <QHeaderView>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QStandardItemModel *model = new QStandardItemModel();

    model->setColumnCount(2);
    model->setHeaderData(0,Qt::Horizontal,QString::fromLocal8Bit("Card number"));
    model->setHeaderData(1,Qt::Horizontal,QString::fromLocal8Bit("full name"));
    QTableView *tableview = new QTableView();
    tableview->setModel(model);
    tableview->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
    tableview->horizontalHeader()->setResizeMode(0,QHeaderView::Fixed);
    tableview->horizontalHeader()->setResizeMode(1,QHeaderView::Fixed);
    tableview->setColumnWidth(0, 100);
    tableview->setColumnWidth(1, 100);
    for(int i = 0; i < 100; i++)
    {
        model->setItem(i,0,new QStandardItem("220161101"));
        //Set character color
        model->item(i,0)->setForeground(QBrush(QColor(255, 0, 0)));
        //Set character position
         model->item(i,0)->setTextAlignment(Qt::AlignCenter);
         model->setItem(i,1,new QStandardItem(QString::fromLocal8Bit("ha-ha")))
     }

    tableview->show();
    return a.exec();
}

4, QColumnView column view

1. Introduction to qccolumnview component

Qccolumnview column view provides a column view implementation of model / view. It is a part of the model / view framework and provides multi-level views (a menu will appear next to each option to display all its sub items).

2. QColumnView component properties

QColumnView component property setting options:

A. Name: the name in the corresponding source code of the component

B. Font: set the font inside the table

3. QColumnView component common member functions

QColumnView(QWidget *parent = 0)

Construct a ColumnView whose parent object is parent

QAbstractItemView * createColumn(const QModelIndex *index)[virtual protected]

Index is the root model index of the view and returns the new view

void currentChanged(const QModelIndex&t,const QModelIndex &previous)[virtual protected]

Specify current as the current project and previous as the previous current project

QModelIndex indexAt(cosnt QPoint &point) const [virtual]

Returns the index model of the item at the point pos

QWidget *previewWidget() const

Returns the preview component, or 0 if none

void rowsInserted(const QModelIndex &parent,int start,int end) [virtual protected]

Insert a new line whose parent is parent, including all items from start to end

void selectAll() [virtual]

Set all items in this ColumnView as selected

void setPreviewWidget(QWidget *widget)

Set the widget as the preview component of the columnView.

4. Qccolumnview instance

#include <QtGui/QApplication>
#include <QMainWindow>
#include <QColumnView>
#include <QStandardItemModel>
#include <QStandardItem>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QMainWindow win;
    QColumnView *cview = new QColumnView(&win);
    win.setCentralWidget(cview);
    QStandardItemModel model;

    for (int groupnum = 0; groupnum < 3 ; ++groupnum)
    {
        QStandardItem *group = new QStandardItem(QString("Group %1").arg(groupnum));
        for (int personnum = 0; personnum < 5 ; ++personnum)
        {
            QStandardItem *child = new QStandardItem(QString("Person %1 (group %2)").arg(personnum).arg(groupnum));
            group->appendRow(child);
        }
        model.appendRow(group);
    }
    cview->setModel(&model);
    win.show();
    return app.exec();
}

Keywords: Qt

Added by WLW on Thu, 03 Mar 2022 20:37:16 +0200