Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
QTreeWidget备用行颜色设置_Qt_Qt4 - Fatal编程技术网

QTreeWidget备用行颜色设置

QTreeWidget备用行颜色设置,qt,qt4,Qt,Qt4,我想将颜色设置为我使用的树边中的备用行 setAlternatingRowColors(1); QPalette p = palette(); p.setColor( QPalette::AlternateBase, QColor(226, 237, 253) ); setPalette(p); 但在这里,每次单击后,颜色设置为已设置行下方的行,或者颜色设置在行之间切换。我希望将其设置为特定行的常量。表示第一行,如果第二行正在设置颜色,则单击后,颜色集将转到第三行。我希望它只在第二排我建议使用

我想将颜色设置为我使用的树边中的备用行

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

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

我建议使用模型来完成这项工作,并返回模型中背景的适当颜色。当
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;
    }
}