日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python tablewidget 颜色_QT中,QTableView鼠标移动到item上时该item所在行的背景颜色变成其他颜色,这要怎么实现...

發布時間:2023/12/4 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python tablewidget 颜色_QT中,QTableView鼠标移动到item上时该item所在行的背景颜色变成其他颜色,这要怎么实现... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

展開全部

//不解釋,自己看。不保證完整,僅供思路參考

#include?

#include?"TableView.h"

#include?

#include?

int?main(int?argc,?char?*argv[])

{

QApplication?a(argc,?argv);

QStandardItemModel?model;

for?(?int?col?=?0;?col?

{

QList?list;

for?(?int?row?=?0;?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?#include?

#include?

#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?

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(model());

for?(?int?col?=?0;?col?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(model());

for?(?int?col?=?0;?col?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?

#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?

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?&=?~32313133353236313431303231363533e78988e69d8331333332626134QStyle::State_MouseOver;

QStyledItemDelegate::paint(painter,?o,?index);

}

總結

以上是生活随笔為你收集整理的python tablewidget 颜色_QT中,QTableView鼠标移动到item上时该item所在行的背景颜色变成其他颜色,这要怎么实现...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。