|
转自这里:http://zhidao.baidu.com/link?url=b9K-NxkXmEsIItEQFO17V1M9zB1MpywHa3gqsR7Xs1mMADjDOVwyoNgcKL1URdgU03JxI3Tc1QRCI0Ep00hee_
//不解释,自己看。不保证完整,仅供思路参考#include <QtGui/QApplication>#include "TableView.h"#include <QStandardItem>#include <QStandardItemModel> int main(int argc, char *argv[]){ QApplication a(argc, argv); QStandardItemModel model; for ( int col = 0; col < 3; col++ ) { QList<QStandardItem*> list; for ( int row = 0; row < 5; row++ ) { list.append(new QStandardItem); } model.appendColumn(l<img
id="selectsearch-icon" src="http://img.baidu.com/img/iknow/qb/select-search.png" alt="搜索" class="selectsearch-hide">ist); } TableView view; view.setModel(&model); view.show(); return a.exec();}#ifndef IVIEW_H#define IVIEW_Hclass IView {public: virtual void setMouseOver(const int) =0;};#endif // IVIEW_H #ifndef TABLEVIEW_H#define TABLEVIEW_H#include <QTableView>#include <QMouseEvent>#include <QStandardItem> #include "IView.h"#include "Delegate.h"class TableView : public QTableView, public IView { Q_OBJECTprivate: int currHovered; void mouseMoveEvent(QMouseEvent *event); void disableMouseOver();public: TableView(QWidget *parent = 0); void setMouseOver(const int);};#endif // TABLEVIEW_H #include "TableView.h"#include <QDebug>TableView::TableView(QWidget *parent) : QTableView(parent), currHovered(-1){ Delegate *delegate = new Delegate; delegate->setView(this); setItemDelegate(delegate); setMouseTracking(true);} void TableView::setMouseOver(const int row){ if ( row == currHovered) return; QStandardItemModel *_model = static_cast<QStandardItemModel*>(model()); for ( int col = 0; col < _model->columnCount(); col++ ) { QStandardItem *item = _model->item(row, col); item->setBackground(QBrush(QColor("red"))); } if ( currHovered != -1 ) { disableMouseOver(); } currHovered = row;} void TableView::disableMouseOver(){ QStandardItemModel *_model = static_cast<QStandardItemModel*>(model()); for ( int col = 0; col < _model->columnCount(); col++ ) { QStandardItem *item = _model->item(currHovered, col); item->setBackground(QBrush(QColor("white"))); }} void TableView::mouseMoveEvent(QMouseEvent *event){ // TODO: you need know when mouse are not in table rect // then you need disable over QTableView::mouseMoveEvent(event);} #ifndef DELEGATE_H#define DELEGATE_H #include <QStyledItemDelegate>#include "IView.h"class Delegate : public QStyledItemDelegate {private: IView *view;public: void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; void setView(IView *view) { this->view = view; }};#endif // DELEGATE_H #include "Delegate.h"#include <QDebug>void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ QStyleOptionViewItemV4 o = option; initStyleOption(&o, index); if ( o.state & QStyle::State_MouseOver ) { view->setMouseOver(index.row()); } o.state &= ~QStyle::State_MouseOver; QStyledItemDelegate::paint(painter, o, index);} |
追问
你这类似的思路我之前有想过,但是我不知道当离开那table的范围要怎么回复原来的颜色,就是你代码中的TableView::mouseMoveEvent(QMouseEvent *event)函数,判断是否在table里面,这个你有相关代码吗,能否请教下,感激不尽
回答
用table的矩形范围和event中的鼠标坐标进行判断,就得到鼠标是否位于table控件内了。 看看QTableView和QMouseEvent的文档吧。
-
提问者评价
-
谢谢!
本文详细介绍了C++中TableView组件的实现方式,包括其构造函数、模型设置、视图展示及鼠标交互功能。通过实例代码展示了如何创建TableView组件,并实现对表格数据的动态更新与鼠标悬浮效果。
7535

被折叠的 条评论
为什么被折叠?



