QTableWidget设置代理使得选中行字体颜色保持不变并且失去焦点后(也就是QTableWidget失去焦点或子类TableWidget)底色不变

这个问题困扰了我很久,一直不理解QT的MVC设计思路,后来经过一天学习才知道,所有个性化的设置都是通过代理类QStyledItemDelegate来进行改变,譬如选中效果,字体颜色大小等等,看起来比MFC当然要复杂多了,但是别忘了QT是跨平台的,能做到这样也是非常厉害了,不得不佩服QT作者的匠心独特

上关键代码:

MyTableWidget*childTable = new MyTableWidget;//MyTableWidget继承自QTableWidget

然后

childTable->setItemDelegate(new ItemDelegate(childTable));

//ItemDelegate类:

class ItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    ItemDelegate::ItemDelegate(QObject* parent)
        : QStyledItemDelegate(parent)
    {

    }
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        QStyleOptionViewItem  viewOption(option);
        //高亮显示与普通显示时的前景色一致(即选中行和为选中时候的文字颜色一样)  
        viewOption.palette.setColor(QPalette::HighlightedText, index.data(Qt::ForegroundRole).value<QColor>());
        QStyledItemDelegate::paint(painter, viewOption, index);
    }
};

搞定,原来这么简单,非常适合股票软件,另外附网上一个设计例子,可以改变列背景色,这对于股票行情非常有用,当价格变动时可以改变列的背景色以高亮突出显示一秒提示股民

 

 

ItemDelegate::ItemDelegate(int type, QObject *parent)
    : QStyledItemDelegate(parent),
      m_type(type)
{
 
}
 
void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(0 == m_type) //header tablewidget
    {
        int rowIndex = index.row();//行号
        int colIndex = index.column();//列号
        if (rowIndex == 0 || rowIndex == 1)//前两行作为header
        {
            //背景
            QColor color;
 
            if (rowIndex == 0 && (colIndex == 0 || //老师ID
                                  colIndex == 1 || //老师姓名
                                  colIndex == 2 || //课程类型
                                  colIndex == 9))  //操作
            {
                color.setRgb(231, 238, 251);
            }
            else if ((rowIndex == 0 && colIndex == 3) || //8月20日
                     (rowIndex == 1 && (colIndex == 3 || //续报率
                                        colIndex == 4 || //新学员续报率
                                        colIndex == 5 || //续报增长人数
                                        colIndex == 6))) //续报增长率
            {
                color.setRgb(214, 228, 253);
            }
            else if ((rowIndex == 0 && colIndex == 7) || //8月19日
                     (rowIndex == 1 && (colIndex == 7 || //续报率
                                        colIndex == 8))) //新学员续报率
            {
                color.setRgb(203, 221, 255);
            }
 
            //绘制背景
            painter->setPen(color);
            painter->setBrush(QBrush(color));
            painter->drawRect(option.rect);
 
            //右侧spacer
            if ((rowIndex == 0 && (colIndex == 0 || colIndex == 1) )) {
                int startX = option.rect.right();
                int startY = option.rect.y() + (option.rect.height() - 40) / 2;
                int endX = startX;
                int endY = startY + 40;
                QLinearGradient linearGradient(startX, startY, endX, endY);
                linearGradient.setColorAt(0, QColor(164, 188, 240, 0));
                linearGradient.setColorAt(0.5, QColor(164, 188, 240, 255));
                linearGradient.setColorAt(1, QColor(164, 188, 240, 0));
                painter->setBrush(linearGradient);
                painter->drawRect(option.rect.right()- 2, startY, 2, 40);
 
            }
            else if (rowIndex == 1 && (colIndex == 3 ||
                                       colIndex == 4 ||
                                       colIndex == 5 ||
                                       colIndex == 7 )) {
 
                int startX = option.rect.right();
                int startY = option.rect.y() + (option.rect.height() - 28) / 2;
                int endX = startX;
                int endY = startY + 28;
                QLinearGradient linearGradient(startX, startY, endX, endY);
                linearGradient.setColorAt(0, QColor(164, 188, 240, 0));
                linearGradient.setColorAt(0.5, QColor(164, 188, 240, 255));
                linearGradient.setColorAt(1, QColor(164, 188, 240, 0));
                painter->setBrush(linearGradient);
                painter->drawRect(option.rect.right()- 2, startY, 2, 28);        }
 
            //字体
            painter->setPen(QColor(51, 51, 51));
            QTextOption op;
            op.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
 
            QFont font;
            font.setFamily("Microsoft YaHei");
            font.setPixelSize(14);
            font.setBold(true);
            painter->setFont(font);
 
            QRect rect;
            if (rowIndex == 0 && colIndex == 9){//"操作" 左对齐
                rect = QRect(option.rect.x(), option.rect.y(), 100, option.rect.height());
            }
            else {//其他的居中
                rect = option.rect;
            }
 
            painter->drawText(rect, index.data(Qt::DisplayRole).toString(), op);
 
        }
 
    }
    else {  //content table widget
        //文字
        painter->setPen(QColor(255, 0, 0));
        QTextOption op;
        op.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
 
        QFont font;
        font.setFamily("Microsoft YaHei");
        font.setPixelSize(12);
        painter->setFont(font);
 
        if( option.state & QStyle::State_Selected ) {
            painter->fillRect( option.rect, QBrush(QColor(239,244,255)));
        }
 
        painter->drawText(option.rect, index.data(Qt::DisplayRole).toString(), op);
 
        //底边框
        painter->setPen(QColor(248,251,255));
        painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
 
    }
}
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值