需求:拖动矩形
效果:
#ifndef RECTDELEGATE_H
#define RECTDELEGATE_H
#include <QItemDelegate>
class RectDelegate :public QItemDelegate
{
Q_OBJECT
public:
RectDelegate(QObject *parent = 0);
private:
void paint(QPainter* painter,const QStyleOptionViewItem& option,const QModelIndex& index) const;
};
#endif // RECTDELEGATE_H
#include "rectdelegate.h"
#include <QPainter>
RectDelegate::RectDelegate(QObject *parent) : QItemDelegate(parent)
{
}
void RectDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
Q_UNUSED(option);
QPoint point = index.data().toPoint();
painter->setBrush(QBrush(QColor(120,120,120)));
painter->drawRect(point.x(), option.rect.y(), point.y(), option.rect.height());
}
#ifndef MYVIEW_H
#define MYVIEW_H
#include <QTableView>
class QStandardItemModel;
class MyView : public QTableView
{
Q_OBJECT
public:
explicit MyView(QWidget *parent = 0);
private:
void setRect(int row, int column, const QPoint &point);
bool pointLeftCheck(const QPoint &pos, const QPoint &point);
bool pointRightCheck(const QPoint &pos, const QPoint &point);
protected:
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private:
static const int offset = 2;
int m_Index;
bool m_LeftDown;
bool m_RightDown;
QStandardItemModel *m_Model;
};
#endif // MYVIEW_H
#include "myview.h"
#include "rectdelegate.h"
#include <QStandardItemModel>
#include <QTableView>
#include <QMouseEvent>
#include <QHeaderView>
MyView::MyView(QWidget *parent) : QTableView(parent)
{
m_LeftDown = false;
m_RightDown = false;
resize(500,500);
RectDelegate *ck = new RectDelegate();
this->setItemDelegateForColumn(1,ck);
m_Model = new QStandardItemModel();
m_Model->setRowCount(2);
m_Model->setColumnCount(2);
this->setModel(m_Model);
this->verticalHeader()->setDefaultSectionSize(20);
this->setEditTriggers(QAbstractItemView::NoEditTriggers);
//! test
setRect(0,1,QPoint(100,80));
}
void MyView::setRect(int row, int column, const QPoint &point)
{
QStandardItem *item = new QStandardItem();
item->setData(point,Qt::DisplayRole);
m_Model->setItem( row, column, item );
}
bool MyView::pointLeftCheck(const QPoint &pos, const QPoint &point)
{
return pos.x() > point.x() - offset && pos.x() < point.x() + offset;
}
bool MyView::pointRightCheck(const QPoint &pos, const QPoint &point)
{
int width = point.x() + point.y();
return pos.x() > width - offset && pos.x() < width + offset;
}
void MyView::mousePressEvent(QMouseEvent *event)
{
QTableView::mousePressEvent(event);
m_Index = currentIndex().row();
QPoint && point = currentIndex().data().toPoint();
QPoint && pos = event->pos();
if(pointLeftCheck(pos, point)){
m_LeftDown = true;
setCursor(Qt::SizeHorCursor);
return;
}else if(pointRightCheck(pos, point)){
m_RightDown = true;
setCursor(Qt::SizeHorCursor);
return;
}
//!......
}
void MyView::mouseReleaseEvent(QMouseEvent *event)
{
QTableView::mouseReleaseEvent(event);
setCursor(Qt::ArrowCursor);
m_LeftDown = false;
m_RightDown = false;
}
void MyView::mouseMoveEvent(QMouseEvent *event)
{
QTableView::mouseMoveEvent(event);
if(m_Index != currentIndex().row()) return;
QModelIndex && index = currentIndex();
QPoint && point = currentIndex().data().toPoint();
if(m_LeftDown){
point.setY(point.x() + point.y() - event->pos().x());
point.setX(event->pos().x());
}else if(m_RightDown){
point.setY(event->pos().x() - point.x());
}
setRect(index.row(), index.column(), point);
}
#include "myview.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyView view;
view.show();
return a.exec();
}