onnect(tableWidget,SIGNAL(cellEntered(int,int)),this,SLOT(mycellEntered(int,int)));
在槽函数中可以实现改变第几行第几列的颜色!
用QTreeWidget也可以啊!QTableWidget就相当于QTreeWidget只有顶层,没有子节点而已!
treeWidget->setStyleSheet( "QTreeView::item:hover{background-color:rgb(0,255,0,50)}"
"QTreeView::item:selected{background-color:rgb(255,0,0,100)}");
hover表示鼠标滑动过时的颜色,selected表示选中时的颜色。
使用QTreeWidget岂不是更简单,使用QSS样式设置就行了,QTableWidget还得自己写事件!
//不解释,自己看。不保证完整,仅供思路参考
#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(list);
}
TableView view;
view.setModel(&model);
view.show();
return a.exec();
}
#ifndef IVIEW_H
#define IVIEW_H
class 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_OBJECT
private: 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的文档吧。
本回答被提问者采纳