QTableView add Control row BackGround image

备注: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;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值