Qt模型-视图实践

效果如下:
这里写图片描述

文件列表:
这里写图片描述

spinboxdelegate.h 文件内容:

#ifndef SPINBOXDELEGATE_H
#define SPINBOXDELEGATE_H

#include <QItemDelegate>

class SpinBoxDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    explicit SpinBoxDelegate(QObject *parent = 0);

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;

signals:

public slots:

};

#endif // SPINBOXDELEGATE_H

spinboxdelegate.cpp内容:

#include "spinboxdelegate.h"
#include <QSpinBox>

SpinBoxDelegate::SpinBoxDelegate(QObject *parent) :
    QItemDelegate(parent)
{
}

QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSpinBox *editor = new QSpinBox(parent);
    editor->setMinimum(0);
    editor->setMaximum(100);
    return editor;
}

void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    int value = index.model()->data(index,Qt::EditRole).toInt();
    QSpinBox *spinBox = static_cast<QSpinBox *>(editor);
    spinBox->setValue(value);
}

void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QSpinBox *spinBox = static_cast<QSpinBox *> (editor);
    spinBox->interpretText();
    int value = spinBox->value();
    model->setData(index,value,Qt::EditRole);
}

void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

mainwindow.h内容:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui
{
    class MainWindow;
}

class QTableView;
class QItemSelection;
class QModelIndex;


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    QTableView *tabelView;
    QTableView *tabelView2;

    Ui::MainWindow *ui;

public slots:
    void getCurrentItemData();
    void toggleSelection();

    void updataSelection(const QItemSelection &selected, const QItemSelection &deselected);
    void changeCurrent(const QModelIndex &current, const QModelIndex &previous);
};

#endif // MAINWINDOW_H

mainwindow.cpp内容:

#include "mainwindow.h"
#include <QStandardItemModel>
#include <QTableView>
#include <QDebug>
#include "ui_mainwindow.h"
#include "spinboxdelegate.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QStandardItemModel *model = new QStandardItemModel(7,4,this);
    for(int row = 0; row < 7; ++row)
    {
        for(int column = 0; column < 4; ++column)
        {
            QStandardItem *item = new QStandardItem(QString("%1").arg(row*4+column));
            model->setItem(row,column,item);
        }
    }


    tabelView = new QTableView;
    tabelView->setModel(model);

    SpinBoxDelegate *delegate = new SpinBoxDelegate(this);
    tabelView->setItemDelegate(delegate);

    setCentralWidget(tabelView);


    //获取视图的项目选择模型
    QItemSelectionModel *selectionModel = tabelView->selectionModel();

    //定义左上角和右下角的索引,然后使用这两个索引创建选择
    QModelIndex topLeft;
    QModelIndex bottomRight;
    topLeft = model->index(1,1,QModelIndex());
    bottomRight = model->index(5,2,QModelIndex());
    QItemSelection selection(topLeft,bottomRight);

    //使用指定的选择模式来选择项目
    selectionModel->select(selection,QItemSelectionModel::Select);

    ui->toolBar->addAction(tr("当前项目"),this,SLOT(getCurrentItemData()));
    ui->toolBar->addAction(tr("切换选择"),this,SLOT(toggleSelection()));

    connect(selectionModel,SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(updataSelection(QItemSelection,QItemSelection)));
    connect(selectionModel,SIGNAL(currentChanged(QModelIndex,QModelIndex)),this,SLOT(changeCurrent(QModelIndex,QModelIndex)));


    tabelView2 = new QTableView;
    tabelView2->setWindowTitle("tableView2");
    tabelView2->resize(415,315);
    tabelView2->setModel(model);
    tabelView2->setSelectionModel(selectionModel);
    tabelView2->show();
}

void MainWindow::getCurrentItemData()
{
    qDebug()<<tr("当前项目的内容:")
           <<tabelView->selectionModel()->currentIndex().data().toString();
}

void MainWindow::toggleSelection()
{
    QModelIndex topLeft = tabelView->model()->index(0,0,QModelIndex());
    QModelIndex bottomRight = tabelView->model()->index(tabelView->model()->rowCount(QModelIndex())-1,
                                                        tabelView->model()->columnCount(QModelIndex())-1,
                                                        QModelIndex());
    QItemSelection currentSelection(topLeft,bottomRight);
    tabelView->selectionModel()->select(currentSelection,QItemSelectionModel::Toggle);
}

void MainWindow::updataSelection(const QItemSelection &selected, const QItemSelection &deselected)
{
    QModelIndex index;
    QModelIndexList list = selected.indexes();
    foreach (index, list) {
        QString text = QString("(%1,%2)").arg(index.row()).arg(index.column());
        tabelView->model()->setData(index,text);
    }
    list = deselected.indexes();
    foreach (index, list) {
        tabelView->model()->setData(index,"");
    }
}


void MainWindow::changeCurrent(const QModelIndex &current, const QModelIndex &previous)
{
    qDebug()<<tr("move(%1,%2)to(%3,%4)").arg(previous.row()).arg(previous.column())
              .arg(current.row()).arg(current.column());
}
MainWindow::~MainWindow()
{

}

main.cpp内容:

#include "mainwindow.h"
#include <QApplication>
#include <QTextCodec>

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

    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    MainWindow w;
    w.resize(425,315);
    w.show();

    return a.exec();
}

注意: 如果后添加的ui文件中没有toolBar,需要手动添加(在ui区域右键-添加toolBar)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值