【问题标题】:QTreeWidget alternate row color settingQTreeWidget 交替行颜色设置
【发布时间】:2011-08-09 16:03:23
【问题描述】:

我想在我使用的树小部件中将颜色设置为交替行

setAlternatingRowColors(1);
QPalette p = palette();
p.setColor( QPalette::AlternateBase, QColor(226, 237, 253) );
setPalette(p);

但在每次单击后,颜色设置为已设置行下方的行,或者颜色设置在行之间切换。我希望将其设置为特定行的常量。表示首先如果第二行设置颜色,然后单击颜色设置将转到第三行。我希望它只在第二行

【问题讨论】:

    标签: qt qt4


    【解决方案1】:

    我建议使用模型来执行此操作,并为模型中的背景返回适当的颜色。当data(const QModelIndex& index, int role) 被称为视图的模型对象(或在您的情况下为QTreeWidget)时,role 的值之一将是Qt::BackgroundRole。像下面这样的东西会做你想做的事:

    QVariant SomeModel::data(const QModelIndex& index, int role)
    {
        switch(role)
        {
        // other role handling code here. below is the except for handling BackgroundRole
        case Qt::BackgroundRole:
            if (0 == index.row() % 2)
                return QColor(226, 237, 253);
            else
                return Qt::white;
        break;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-16
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2013-11-06
      • 2012-07-17
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多