备注:1.去掉网格
2.去掉选中的焦点色{蓝条}和选中单元格的虚框
3.添加选中和非选中时的背景(图片)
4.添加的控件,非Hover时为“假”控件,Hover时为“真”控件
#ifndef QXTABLEVIEW_H
#define QXTABLEVIEW_H
#include <QTableView>
#include <QtGui>
class QxTableView : public QTableView
{
Q_OBJECT
public:
QxTableView(QWidget *parent = 0);
~QxTableView();
public:
void setModel(QAbstractItemModel *model);
protected:
virtual void mouseMoveEvent ( QMouseEvent * event );
private:
QModelIndex getSourceIndex(const QPoint &pos);
QSortFilterProxyModel* m_model;
int rowId;
};
#endif // QXTABLEVIEW_H
#include "qxtableview.h"
#include <Qtgui>
#define PROGRESSBARHEIGHT 20
#define rowCount 1000
QxTableView::QxTableView(QWidget *parent)
: QTableView(parent)
{
setMouseTracking(true);
m_model = new QSortFilterProxyModel();
rowId = -1;
}
QxTableView::~QxTableView()
{
}
void QxTableView::mouseMoveEvent ( QMouseEvent * event )
{
QAbstractItemView::mouseMoveEvent(event);
QModelIndex index = getSourceIndex(event->pos());
if (rowId != index.row()){
if ((rowId >= 0) && (rowId != index.row())){
closePersistentEditor(model()->index(rowId, 3, QModelIndex()));
}
openPersistentEditor( model()->index(index.row(), 3, QModelIndex()));
rowId = index.row();
}
}
void QxTableView::setModel(QAbstractItemModel *model)
{
QTableView::setModel(model);
m_model = (QSortFilterProxyModel*)model;
}
QModelIndex QxTableView::getSourceIndex(const QPoint &pos)
{
QModelIndex modelIndex;
modelIndex = indexAt(pos);
return m_model->mapToSource(modelIndex);
}
#ifndef TRACKDELEGATE_H
#define TRACKDELEGATE_H
#include <QItemDelegate>
#include <QtGui>
#include "floatingprogress.h"
class TrackDelegate : public QItemDelegate
{
Q_OBJECT
public:
TrackDelegate(QObject *parent = 0);
~TrackDelegate();
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index)const;
void setFloatingProgress(FloatingProgress* floating);
void setRealFloatingProgress(FloatingProgress* floating);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
private:
QString m_RowBackGroundImagePath;
FloatingProgress* m_floating;
FloatingProgress* m_realFloating;
};
#endif // TRACKDELEGATE_H
#include "trackdelegate.h"
TrackDelegate::TrackDelegate(QObject *parent)
: QItemDelegate(parent)
{
m_floating = NULL;
}
TrackDelegate::~TrackDelegate()
{
}
void TrackDelegate::setFloatingProgress(FloatingProgress* floating)
{
m_floating = floating;
}
void TrackDelegate::setRealFloatingProgress(FloatingProgress* floating)
{
m_realFloating = floating;
}
QWidget* TrackDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
FloatingProgress* floating = new FloatingProgress(parent);
return floating;
}
void TrackDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem viewOption(option);
QString backgroundImage;
if (viewOption.state & QStyle::State_Selected)
{
backgroundImage = "image2.png";
}else{
backgroundImage = "image.png";
}
if (viewOption.state & QStyle::State_Selected)
viewOption.state = viewOption.state ^ QStyle::State_Selected;
if (viewOption.state & QStyle::State_HasFocus)
viewOption.state = viewOption.state ^ QStyle::State_HasFocus;
QPixmap pixmap(backgroundImage);
pixmap.scaled(option.rect.width(),option.rect.height());
QBrush brush(pixmap);
painter->save();
painter->fillRect(option.rect, brush);
painter->restore();
viewOption.rect = QRect(option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height());
switch (index.column())
{
case 0:
{
QVariant var= index.model()->data(index,Qt::DisplayRole);
QString strStempJpgPath = var.toString();
QPixmap pixmap(strStempJpgPath);
pixmap = pixmap.scaled(option.rect.width(),option.rect.height(),Qt::KeepAspectRatio);
painter->drawPixmap(option.rect.x(),option.rect.y(),pixmap);
break;
}
case 3:
{
m_floating->setBackGroundImage(backgroundImage);
m_floating->resize(option.rect.width(), option.rect.height());
QPixmap pixmap(m_floating->size());
m_floating->render(&pixmap);
pixmap = pixmap.scaled(option.rect.width(),option.rect.height()/*,Qt::KeepAspectRatio*/);
painter->drawPixmap(option.rect.x(),option.rect.y(),pixmap);
break;
}
default:
QItemDelegate::paint(painter, viewOption,index);
break;
}
}