2. [文件] test.zip ~ 23KB 下载(647)
3. [文件] main.cpp ~ 273B 下载(119)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * @file main.cpp * @brief * @author xiangxw * @version 0.0 * @date 2011年04月30日 */ #include<QApplication> #include"MyTableView.h" int main( int argc, char *argv[]) { QApplication app(argc,argv); MyTableView table; table.show(); return app.exec(); } |
4. [文件] MyTableView.h ~ 1KB 下载(84)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | /** * @file MyTableView.h * @brief * @author xiangxw * @version 0.0 * @date 2011年04月30日 */ #ifndef MYTABLEVIEW_H #define MYTABLEVIEW_H #include<QTableView> #include<QItemDelegate> #include<QStandardItemModel> #include<QPixmap> class MyTableView; class MyItemDelegate; class MyStandardItemModel; class MyTableView: public QTableView { public : MyTableView(QWidget * parent=0); virtual ~ MyTableView(){} protected : void mouseMoveEvent(QMouseEvent * event); private : MyItemDelegate * delegate; MyStandardItemModel * model; }; class MyItemDelegate: public QItemDelegate { public : MyItemDelegate(QObject * parent=0); virtual ~ MyItemDelegate(){} void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const ; bool editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index); private : QPixmap favouritePixmap; QPixmap notFavouritePixmap; }; class MyStandardItemModel: public QStandardItemModel { public : MyStandardItemModel(QObject * parent=0) :QStandardItemModel(parent){} virtual ~ MyStandardItemModel(){} QVariant data( const QModelIndex & index, int role=Qt::DisplayRole) const ; QVariant headerData( int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const ; }; #endif |
5. [文件] MyTableView.cpp ~ 3KB 下载(107)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | /** * @file MyTableView.cpp * @brief * @author xiangxw * @version 0.0 * @date 2011年04月30日 */ #include<QPainter> #include<QMouseEvent> #include"MyTableView.h" #ifndef NDEBUG #include<QDebug> #endif MyTableView::MyTableView(QWidget * parent) :QTableView(parent) { delegate= new MyItemDelegate; model= new MyStandardItemModel; model->setRowCount(6); model->setColumnCount(8); this ->setModel(model); this ->setItemDelegate(delegate); this ->resizeColumnsToContents(); this ->resizeRowsToContents(); this ->setEditTriggers(QAbstractItemView::NoEditTriggers); this ->setSelectionBehavior(QAbstractItemView::SelectRows); this ->setMouseTracking( true ); //important } void MyTableView::mouseMoveEvent(QMouseEvent * event) { int column= this ->columnAt(event->x()); int row= this ->rowAt(event->y()); if (column==0 && row!=-1){ this ->setCursor(Qt::PointingHandCursor); } else { this ->setCursor(Qt::ArrowCursor); } } MyItemDelegate::MyItemDelegate(QObject * parent) :QItemDelegate(parent) { favouritePixmap=QPixmap( ":/favourite.png" ); notFavouritePixmap=QPixmap( ":/no-favourite.png" ); } void MyItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const { if (index.column()!=0){ QItemDelegate::paint(painter,option,index); return ; } const QAbstractItemModel * model=index.model(); QVariant var=model->data(index,Qt::CheckStateRole); if (var.isNull()) var= false ; const QPixmap & star=var.toBool()? favouritePixmap:notFavouritePixmap; int width=star.width(); int height=star.height(); QRect rect=option.rect; int x=rect.x()+rect.width()/2-width/2; int y=rect.y()+rect.height()/2-height/2; painter->drawPixmap(x,y,star); } bool MyItemDelegate::editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & /*option*/ , const QModelIndex & index) { if (event->type()==QEvent::MouseButtonPress && index.column()==0){ QVariant var=model->data(index,Qt::CheckStateRole); bool isFavourite=var.toBool(); if (var.isValid()) isFavourite=isFavourite? false : true ; else isFavourite= true ; model->setData(index,isFavourite,Qt::CheckStateRole); return true ; //I have handled the event } return false ; } QVariant MyStandardItemModel::data( const QModelIndex & index, int role) const { int column=index.column(); if (role==Qt::DisplayRole && column!=0) return column+1; if (role==Qt::ToolTipRole && column==0) return tr( "love" ); 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::DecorationRole) return QIcon( ":/favourite.png" ); if (role==Qt::DisplayRole) return "" ; if (role==Qt::ToolTipRole) return tr( "love" ); } return QStandardItemModel::headerData(section,orientation,role); } |