如何访问委托的paint()函数中的另一个索引?
How to access another index inside paint() function of delegate?
当鼠标悬停在表格的单元格上时,我需要为该单元格的整行创建效果。这意味着我需要访问另一个索引。
在这种情况下,我做了一个 for 循环,从表的第一列运行到最后一列并为其设置效果。但它不起作用。当然,因为命令drawText没有任何输入参数作为索引。在这种情况下,如何设置另一个索引的效果?
另一种解决方案也受到欢迎。谢谢!
void TableDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
qDebug() << index.row() << index.column();
TableDataRow::Type type = static_cast<TableDataRow::Type>( index.data( Qt::UserRole ).toInt() );
QString text = index.data( Qt::DisplayRole ).toString();
int row = index.row();
if ( option.state & QStyle::State_MouseOver )
{
if ( type == TableDataRow::Type::Data )
{
painter->fillRect( QRect( 0, option.rect.topLeft().y(), option.rect.width()*numberOfColumns, option.rect.height() ), QColor( 249, 126, 18 ) );
for ( int i = 0; i < numberOfColumns; i++ )
{
QModelIndex indexOfRow = index.sibling( row, i );
painter->setPen( Qt::white );
painter->drawText( option.rect, Qt::AlignVCenter | Qt::TextWordWrap, text );
}
}
}
}
我知道如果鼠标在行中的任何项目上,您希望创建更改行文本颜色的效果,我认为为此没有必要使用委托,只需启用mouseTracking
并覆盖mouseMoveEvent
方法。
#include <QApplication>
#include <QMouseEvent>
#include <QStandardItemModel>
#include <QTableView>
class TableView: public QTableView{
public:
TableView(QWidget *parent = nullptr):
QTableView(parent)
{
setMouseTracking(true);
}
protected:
void mouseMoveEvent(QMouseEvent *event)
{
QModelIndex ix = indexAt(event->pos());
if(mRow != ix.row()){
changeRowColor(mRow);
if(ix.isValid())
changeRowColor(ix.row(), Qt::green, Qt::blue);
mRow = ix.row();
}
QTableView::mouseMoveEvent(event);
}
void leaveEvent(QEvent *event)
{
changeRowColor(mRow);
QTableView::leaveEvent(event);
}
private:
void changeRowColor(int row, const QColor & textColor=Qt::black, const QColor &backgroundColor=Qt::white){
if(!model())
return;
for(int i=0; i< model()->columnCount(); i++){
model()->setData(model()->index(row, i), textColor, Qt::ForegroundRole);
model()->setData(model()->index(row, i), backgroundColor, Qt::BackgroundRole);
}
}
int mRow = -1;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TableView w;
QStandardItemModel model(5, 5);
for(int i=0; i < model.rowCount(); i++){
for(int j=0; j < model.columnCount(); j++){
model.setItem(i, j, new QStandardItem(QString("%1-%2").arg(i).arg(j)));
}
}
w.setModel(&model);
w.show();
return a.exec();
}
更新:
由于您已使用QAbstractTableModel
创建了自己的模型,因此必须实现setData()
和data()
方法来处理Qt::ForegroundRole
和Qt::BackgroundRole
角色。
在我的示例中,每个项目都有以下结构:
struct Item{
QString text="";
QBrush textColor=Qt::black;
QBrush bgColor=Qt::white;
};
然后模型必须将数据保存在QList<QList<Item>> m_items;
中,假设上述方法应如下所示:
QVariant TableModel::data(const QModelIndex &index, int role) const{
if (!index.isValid())
return QVariant();
const Item & item = m_items[index.row()][index.column()];
if (role == Qt::DisplayRole)
return item.text;
else if (role == Qt::ForegroundRole) {
return item.textColor;
}
else if (role == Qt::BackgroundRole) {
return item.bgColor;
}
else
return QVariant();
}
bool TableModel::setData(const QModelIndex &index,
const QVariant &value, int role)
{
if (!index.isValid())
return false;
Item & item = m_items[index.row()][index.column()];
if(role == Qt::EditRole || role == Qt::DisplayRole){
item.text = value.toString();
}
else if (role == Qt::ForegroundRole) {
if(value.canConvert<QBrush>())
item.textColor = value.value<QBrush>();
}
else if (role == Qt::BackgroundRole) {
if(value.canConvert<QBrush>())
item.bgColor = value.value<QBrush>();
}
else
return false;
emit dataChanged(index, index);
return true;
}
完整的示例可以在以下链接中找到
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将指针从一个void函数传递到另一个C++
- 如何从另一个文件继承私有成员变量和公共函数
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 如何在C++中将函数发送到另一个进程
- 如何在qt中从另一个类调用函数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 将 N-arg 函数包装到另一个函数中
- 为什么我的模板化函数需要从一个迭代器转换到另一个迭代器?
- C++重载函数,一个采用基类的参数,另一个采用派生类的参数
- C++从另一个函数退出函数
- 为什么我不能在主函数之外定义一个类的对象(它继承了另一个类)?
- 在一个类中使用另一个类中的函数,而不在 c++ 中使用继承
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?