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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

QTableView中修改某个单元格或者行或者列内容颜色

發布時間:2025/7/14 c/c++ 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QTableView中修改某个单元格或者行或者列内容颜色 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

QTableView的單元格內容實現還是繼承了TableViewModel類的data(const?QModelIndex?&index,?int?role)?const函數,那個設置顏色的問題也就在這個里面實現了。

1、設置某個單元格顏色 1 QVariant TableViewModel::data(const QModelIndex &index, int role) const 2 { 3 if (!index.isValid()) 4 return QVariant(); 5 if (index.row() >= fEntries.size() || index.row() < 0) 6 return QVariant(); 7 if(role == Qt::DisplayRole) { 8 const Entry& entry = fEntries.at(index.row()); 9 const QString& key = getColumnId(index.column()); 10 return entry.value(key); 11 } 12 if(role == Qt::BackgroundRole) 13 { 14 if((1 == index.column())&(fEntries[index.row()].value("LandType") == QString::fromLocal8Bit("登陸失敗"))) 15 { 16 return QVariant(Qt::GlobalColor(Qt::red)); 17 } 18 else if(((1 == index.column())&(fEntries[index.row()].value("LandType") == QString::fromLocal8Bit("登陸成功")))) 19 { 20 return QVariant(Qt::GlobalColor(Qt::green)); 21 } 22 } 23 return QVariant(); 24 }

?

我這個上面其實是有兩種狀態,根據里面的內容來顯示顏色的變化,單元格的鎖定時(index.column()和index.row()). 既然能鎖定某個單個元格,那個鎖定某一行或者一列也很簡單。 2、設置某行顏色 1 QVariant TableViewModel::data(const QModelIndex &index, int role) const 2 { 3 if (!index.isValid()) 4 return QVariant(); 5 if (index.row() >= fEntries.size() || index.row() < 0) 6 return QVariant(); 7 if(role == Qt::DisplayRole) { 8 const Entry& entry = fEntries.at(index.row()); 9 const QString& key = getColumnId(index.column()); 10 return entry.value(key); 11 } 12 if(role == Qt::BackgroundRole) 13 { 14 if(1 == index.row()) 15 { 16 return QVariant(Qt::GlobalColor(Qt::red)); 17 } 18 } 19 return QVariant(); 20 }

?

3、設置某列顏色 1 QVariant TableViewModel::data(const QModelIndex &index, int role) const 2 { 3 if (!index.isValid()) 4 return QVariant(); 5 if (index.row() >= fEntries.size() || index.row() < 0) 6 return QVariant(); 7 if(role == Qt::DisplayRole) { 8 const Entry& entry = fEntries.at(index.row()); 9 const QString& key = getColumnId(index.column()); 10 return entry.value(key); 11 } 12 if(role == Qt::BackgroundRole) 13 { 14 if(1 == index.column()) 15 { 16 return QVariant(Qt::GlobalColor(Qt::red)); 17 } 18 } 19 return QVariant(); 20 }

?

轉載于:https://www.cnblogs.com/felix-wang/p/6248170.html

總結

以上是生活随笔為你收集整理的QTableView中修改某个单元格或者行或者列内容颜色的全部內容,希望文章能夠幫你解決所遇到的問題。

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