效果如下:
文件列表:
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 ¤t, 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 ¤t, 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)。