QT 视图(view)模型(model)汇总

QStringListModel和QListView

UI界面

QT 视图(view)模型(model)汇总_第1张图片

widget头文件

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget {
    Q_OBJECT

public:
    Widget(QWidget* parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_checkBox_clicked(bool checked);

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_6_clicked();

    void on_pushButton_8_clicked(bool checked);

    void on_pushButton_9_clicked();

    void on_pushButton_10_clicked();

    void on_listView_clicked(const QModelIndex& index);

private:
    Ui::Widget* ui;
    QStringList m_strList;
    QStringListModel* m_model;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include >
Widget::Widget(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_strList << "北京"
              << "上海"
              << "山西"
              << "南京";
    m_model = new QStringListModel(this);
    m_model->setStringList(m_strList);
    ui->listView->setModel(m_model);
    ui->listView->setEditTriggers(QAbstractItemView::EditTrigger::DoubleClicked | QAbstractItemView::EditTrigger::SelectedClicked);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    m_model->setStringList(m_strList);
}

void Widget::on_pushButton_2_clicked()
{
    m_model->removeRows(0, m_model->rowCount()); //删除行
}

void Widget::on_checkBox_clicked(bool checked)
{
    if (checked) {
        ui->listView->setEditTriggers(QAbstractItemView::EditTrigger::DoubleClicked | QAbstractItemView::EditTrigger::SelectedClicked);

    } else {
        ui->listView->setEditTriggers(QAbstractItemView::EditTrigger::NoEditTriggers);
    }
}

void Widget::on_pushButton_3_clicked()
{
    qDebug() << "行数" << m_model->rowCount();
    m_model->insertRow(m_model->rowCount()); //插入了行
    qDebug() << "插入后,行数" << m_model->rowCount();

    QModelIndex index = m_model->index(m_model->rowCount() - 1);

    m_model->setData(index, "new item", Qt::DisplayRole);

    ui->listView->setCurrentIndex(index);
}

void Widget::on_pushButton_4_clicked()
{
    QModelIndex index = ui->listView->currentIndex();
    m_model->insertRow(index.row()); //可以插入行
    //    m_model->insertRows(index.row(), 0); //会把行给换掉

    m_model->setData(index, "insert item", Qt::DisplayRole);
}

void Widget::on_pushButton_5_clicked()
{
    QModelIndex index;
    int curRow = ui->listView->currentIndex().row();
    m_model->moveRow(index, curRow, index, curRow - 1);
}

void Widget::on_pushButton_6_clicked()
{
    QModelIndex index;
    int curRow = ui->listView->currentIndex().row();
    m_model->moveRow(index, curRow, index, curRow + 2); //下移需要加2 ,因为两个行号的号,程序会进行自动调整
}

void Widget::on_pushButton_8_clicked(bool checked)
{
    if (checked) {
        m_model->sort(0, Qt::SortOrder::AscendingOrder); //升序
    } else {
        m_model->sort(0, Qt::SortOrder::DescendingOrder); //降序
    }
}

void Widget::on_pushButton_9_clicked()
{
    ui->plainTextEdit->clear();
}

void Widget::on_pushButton_10_clicked()
{
    ui->plainTextEdit->clear();
    QStringList tempList = m_model->stringList();
    for (int i = 0; i < tempList.count(); i++) {
        ui->plainTextEdit->appendPlainText(tempList.at(i));
    }
}

void Widget::on_listView_clicked(const QModelIndex& index)
{
    QString str = QString::asprintf("模型索引: row %d,column = %d", index.row(), index.column());
    qDebug() << str;
    qDebug() << m_model->data(index, Qt::DisplayRole);
}

 ui_widget.h

/********************************************************************************
** Form generated from reading UI file 'widget.ui'
**
** Created by: Qt User Interface Compiler version 6.3.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:
    QVBoxLayout *verticalLayout_2;
    QSplitter *splitter;
    QGroupBox *groupBox;
    QGridLayout *gridLayout;
    QCheckBox *checkBox;
    QPushButton *pushButton_4;
    QPushButton *pushButton_3;
    QListView *listView;
    QPushButton *pushButton;
    QPushButton *pushButton_5;
    QPushButton *pushButton_2;
    QPushButton *pushButton_8;
    QPushButton *pushButton_7;
    QPushButton *pushButton_6;
    QGroupBox *groupBox_2;
    QVBoxLayout *verticalLayout;
    QPushButton *pushButton_9;
    QPushButton *pushButton_10;
    QPlainTextEdit *plainTextEdit;

    void setupUi(QWidget *Widget)
    {
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QString::fromUtf8("Widget"));
        Widget->resize(800, 600);
        verticalLayout_2 = new QVBoxLayout(Widget);
        verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
        splitter = new QSplitter(Widget);
        splitter->setObjectName(QString::fromUtf8("splitter"));
        splitter->setOrientation(Qt::Horizontal);
        groupBox = new QGroupBox(splitter);
        groupBox->setObjectName(QString::fromUtf8("groupBox"));
        gridLayout = new QGridLayout(groupBox);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        checkBox = new QCheckBox(groupBox);
        checkBox->setObjectName(QString::fromUtf8("checkBox"));
        checkBox->setChecked(true);
        checkBox->setTristate(false);

        gridLayout->addWidget(checkBox, 0, 2, 1, 1);

        pushButton_4 = new QPushButton(groupBox);
        pushButton_4->setObjectName(QString::fromUtf8("pushButton_4"));

        gridLayout->addWidget(pushButton_4, 1, 1, 1, 1);

        pushButton_3 = new QPushButton(groupBox);
        pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));

        gridLayout->addWidget(pushButton_3, 1, 0, 1, 1);

        listView = new QListView(groupBox);
        listView->setObjectName(QString::fromUtf8("listView"));

        gridLayout->addWidget(listView, 3, 0, 1, 3);

        pushButton = new QPushButton(groupBox);
        pushButton->setObjectName(QString::fromUtf8("pushButton"));

        gridLayout->addWidget(pushButton, 0, 0, 1, 1);

        pushButton_5 = new QPushButton(groupBox);
        pushButton_5->setObjectName(QString::fromUtf8("pushButton_5"));

        gridLayout->addWidget(pushButton_5, 2, 0, 1, 1);

        pushButton_2 = new QPushButton(groupBox);
        pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));

        gridLayout->addWidget(pushButton_2, 0, 1, 1, 1);

        pushButton_8 = new QPushButton(groupBox);
        pushButton_8->setObjectName(QString::fromUtf8("pushButton_8"));
        pushButton_8->setCheckable(true);
        pushButton_8->setChecked(false);

        gridLayout->addWidget(pushButton_8, 2, 2, 1, 1);

        pushButton_7 = new QPushButton(groupBox);
        pushButton_7->setObjectName(QString::fromUtf8("pushButton_7"));

        gridLayout->addWidget(pushButton_7, 1, 2, 1, 1);

        pushButton_6 = new QPushButton(groupBox);
        pushButton_6->setObjectName(QString::fromUtf8("pushButton_6"));

        gridLayout->addWidget(pushButton_6, 2, 1, 1, 1);

        splitter->addWidget(groupBox);
        groupBox_2 = new QGroupBox(splitter);
        groupBox_2->setObjectName(QString::fromUtf8("groupBox_2"));
        verticalLayout = new QVBoxLayout(groupBox_2);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        pushButton_9 = new QPushButton(groupBox_2);
        pushButton_9->setObjectName(QString::fromUtf8("pushButton_9"));

        verticalLayout->addWidget(pushButton_9);

        pushButton_10 = new QPushButton(groupBox_2);
        pushButton_10->setObjectName(QString::fromUtf8("pushButton_10"));

        verticalLayout->addWidget(pushButton_10);

        plainTextEdit = new QPlainTextEdit(groupBox_2);
        plainTextEdit->setObjectName(QString::fromUtf8("plainTextEdit"));

        verticalLayout->addWidget(plainTextEdit);

        splitter->addWidget(groupBox_2);

        verticalLayout_2->addWidget(splitter);


        retranslateUi(Widget);

        QMetaObject::connectSlotsByName(Widget);
    } // setupUi

    void retranslateUi(QWidget *Widget)
    {
        Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
        groupBox->setTitle(QCoreApplication::translate("Widget", "1", nullptr));
        checkBox->setText(QCoreApplication::translate("Widget", "\345\205\201\350\256\270\347\274\226\350\276\221", nullptr));
        pushButton_4->setText(QCoreApplication::translate("Widget", "\346\217\222\345\205\245\351\241\271", nullptr));
        pushButton_3->setText(QCoreApplication::translate("Widget", "\346\267\273\345\212\240\351\241\271", nullptr));
        pushButton->setText(QCoreApplication::translate("Widget", "\346\201\242\345\244\215\345\210\227\350\241\250", nullptr));
        pushButton_5->setText(QCoreApplication::translate("Widget", "\344\270\212\347\247\273", nullptr));
        pushButton_2->setText(QCoreApplication::translate("Widget", "\346\270\205\351\231\244\345\210\227\350\241\250", nullptr));
        pushButton_8->setText(QCoreApplication::translate("Widget", "\346\216\222\345\272\217", nullptr));
        pushButton_7->setText(QCoreApplication::translate("Widget", "\345\210\240\351\231\244\351\241\271", nullptr));
        pushButton_6->setText(QCoreApplication::translate("Widget", "\344\270\213\347\247\273", nullptr));
        groupBox_2->setTitle(QCoreApplication::translate("Widget", "2", nullptr));
        pushButton_9->setText(QCoreApplication::translate("Widget", "\346\270\205\347\251\272\346\226\207\346\234\254", nullptr));
        pushButton_10->setText(QCoreApplication::translate("Widget", "\346\230\276\347\244\272\346\225\260\346\215\256\346\250\241\345\236\213", nullptr));
    } // retranslateUi

};

namespace Ui {
    class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

你可能感兴趣的:(qt,开发语言)