#include<QPainter>
#include<QMouseEvent>
#include <QHeaderView>
#include"MyTableView.h"
#include <QStyledItemDelegate>
#include <QMessageBox>
#ifndef NDEBUG
#include<QDebug>
#endif
MyTableView::MyTableView(QWidget * parent)
:QTableView(parent)
{
delegate=new MyItemDelegate;
pushbuttondelegate = new PushbuttonDelegate(this);
model=new MyStandardItemModel;
model->setRowCount(6);
model->setColumnCount(8);
this->setStyleSheet("QTableView::item:selected{color:black;background:rgb(255,247,205);}");
this->setModel(model);
QItemSelectionModel *selectionModel=new QItemSelectionModel(model);
this->setSelectionModel(selectionModel);
connect(selectionModel,SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(selectionChanged(QItemSelection,QItemSelection)));
this->setItemDelegateForColumn(0, delegate);
this->setItemDelegateForColumn(1, pushbuttondelegate);
this->resizeColumnsToContents();
this->resizeRowsToContents();
this->verticalHeader()->setVisible(false);
this->setShowGrid(true);
this->setSelectionBehavior(QAbstractItemView::SelectRows);
this->setEditTriggers(QAbstractItemView::NoEditTriggers);
this->setSelectionMode(QAbstractItemView::ExtendedSelection);
this->setMouseTracking(true);
this->setFocusPolicy(Qt::NoFocus); //去除选中虚线框
for(int i = 0 ; i < 8; i++)
{
this->setColumnWidth(i,120);
}
this->resize(675,250);
}
void MyTableView::selectionChanged(const QItemSelection &selected,const QItemSelection &deselected)
{
QTableView::selectionChanged(selected,deselected);
delegate->selectedlist.clear();
QItemSelectionModel *selections = selectionModel();
delegate->selectedlist = selections->selectedIndexes();
}
void MyTableView::rowClicked(const QModelIndex &index)
{
int column = index.column();
if(column == 1)
QMessageBox::information(NULL,"提示",QString::number(column));
}
MyItemDelegate::MyItemDelegate(QObject * parent)
:QStyledItemDelegate(parent)
{
favouritePixmap=QPixmap(":/favourite.png");
notFavouritePixmap=QPixmap(":/no-favourite.png");
}
void MyItemDelegate::paint(QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index) const
{
QStyledItemDelegate::paint(painter, option, index);
QPixmap star;
for(int i=0; i<selectedlist.size(); i++)
{
if(selectedlist.at(i).row() == index.row())
{
star=favouritePixmap;
break;
}
}
if(star.isNull())
star=notFavouritePixmap;
int height=star.height();
QRect rect=option.rect;
int x= 4;
int y=rect.y()+rect.height()/2-height/2;
painter->drawPixmap(x,y,star);
QString str_index = QString::number(index.row()+1);
QRect textrect(23,y+2,50,20);
painter->drawText(textrect,str_index);
}
bool MyItemDelegate::editorEvent(QEvent * event,
QAbstractItemModel * model,
const QStyleOptionViewItem & /*option*/,
const QModelIndex & index)
{
return false;
}
PushbuttonDelegate::PushbuttonDelegate(QWidget * parent)
:QStyledItemDelegate(parent),
m_PushButton(new QPushButton())
{
m_parent = (MyTableView *)parent;
b_clicked = false;
currentBtn = 0;
}
PushbuttonDelegate::~PushbuttonDelegate()
{
}
void PushbuttonDelegate::paint(QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index) const
{
QStyleOptionViewItem viewOption(option);
initStyleOption(&viewOption, index);
if (option.state.testFlag(QStyle::State_HasFocus))
viewOption.state = viewOption.state ^ QStyle::State_HasFocus;
QStyledItemDelegate::paint(painter, viewOption, index);
QStyleOptionButton *opt = m_btns.value(index);
if(!opt)
{
opt = new QStyleOptionButton();
}
opt->state |= QStyle::State_Enabled;
QString str_text = "信息未处理";
opt->text = str_text;
QFont font = m_parent->font();
QFontMetrics fontmetr(font);
int m_width= fontmetr.width(str_text);
QRect rectbtn = QRect(option.rect.x()+(option.rect.width()-m_width)/2, option.rect.y()+8, m_width, 15);
opt->rect = rectbtn;
(const_cast<PushbuttonDelegate*>(this))->m_btns.insert(index, opt);
QWidget *pWidget = m_PushButton.data();
QApplication::style()->drawControl(QStyle::CE_PushButton, opt, painter, pWidget);
}
bool PushbuttonDelegate::editorEvent(QEvent * event,
QAbstractItemModel * model,
const QStyleOptionViewItem & /*option*/,
const QModelIndex & index)
{
QMouseEvent *mouse_event = static_cast<QMouseEvent*>(event);
// 还原鼠标样式
QApplication::restoreOverrideCursor();
switch (event->type())
{
case QEvent::MouseMove:
{
if(index.isValid())
{
QStyleOptionButton* movetBtn = m_btns.value(index);
if(movetBtn->rect.contains(mouse_event->pos()))
{
QApplication::setOverrideCursor(Qt::PointingHandCursor);
}
}
break;
}
case QEvent::MouseButtonPress:
{
if(mouse_event->button() == Qt::LeftButton && !b_clicked)
{
currentBtn = m_btns.value(index);
if(currentBtn->rect.contains(mouse_event->pos()))
b_clicked = true;
}
break;
}
case QEvent::MouseButtonRelease:
{
if(mouse_event->button() == Qt::LeftButton && b_clicked)
{
QStyleOptionButton *newBtn = m_btns.value(index);
if(newBtn == currentBtn && newBtn->rect.contains(mouse_event->pos()))
{
QMessageBox::information(NULL,"提示","按钮已响应");
}
b_clicked = false;
currentBtn = 0;
}
break;
}
default:
break;
}
return false;
}
QVariant MyStandardItemModel::data(
const QModelIndex & index,
int role) const
{
int column=index.column();
if(role==Qt::DisplayRole && column>1)
return column+1;
else if(role==Qt::DisplayRole && column<=1)
return "";
return QStandardItemModel::data(index,role);
}
QVariant MyStandardItemModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
if(section==0 && orientation==Qt::Horizontal)
{
if(role==Qt::DisplayRole)
return "序号";
}
return QStandardItemModel::headerData(section,orientation,role);
}
- 1
- 2
前往页