【问题标题】:QTableWidget selection ColorQTableWidget 选择颜色
【发布时间】:2011-04-07 21:11:33
【问题描述】:

我希望选定的单元格具有不同的背景颜色。默认情况下,所选单元格中只有一条细下划线。

我试过这个:

table->setStyleSheet("QTableView {selection-background-color: #0000FF; selection-color: #00FF00;}

但它只会更改指针位于单元格上时显示的颜色。指针离开后,我是否通过table->selectRow(selRow) 选择单元格,只有下划线。可能它在其他平台上看起来有所不同。

有很多相同主题的线程,但大多数答案是使用上面的样式表。没有任何效果,只有“moseover 颜色”发生了变化。

提前致谢,问候 马蒂亚斯

【问题讨论】:

    标签: qt qtablewidget


    【解决方案1】:
    class BackgroundDelegate : public QStyledItemDelegate {
    public:
      explicit BackgroundDelegate(QObject *parent = 0)
          : QStyledItemDelegate(parent){}
      void paint(QPainter *painter, const QStyleOptionViewItem &option,
                 const QModelIndex &index) const {
        // Fill the background before calling the base class paint
        // otherwise selected cells would have a white background
        QVariant background = index.data(Qt::BackgroundRole);
        if (background.canConvert<QBrush>())
            painter->fillRect(option.rect, background.value<QBrush>());
        // the comment below makes selection transparent
        //QStyledItemDelegate::paint(painter, option, index);
        // To draw a border on selected cells
        if(option.state & QStyle::State_Selected) {
            painter->save();
            QPen pen(Qt::black, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
            int w = pen.width()/2;
            painter->setPen(pen);
            painter->drawRect(option.rect.adjusted(w,w,-w,-w));
            painter->restore();
        }
      }
    };
    

    那么 table-&gt;setItemDelegateForColumn(2, new BackgroundDelegate(this));

    【讨论】:

      【解决方案2】:

      这就是我所做的。

      stylesheet =  "QTableView{selection-background-color: " + highlight + ";"
      stylesheet +=     "selection-color: white; show-decoration-selected: 10}\n"
      stylesheet += "QTableView::item:focus{border: 1px solid yellow;"
      stylesheet +=     "background-color:"+highlight+"}"
      
      table->setStyleSheet(stylesheet);
      

      选择颜色为选中的一个项目着色,而项目焦点将为应突出显示的其余项目着色。

      这适用于选定的单元格,例如具有选定的行。如果你想要“鼠标悬停”的东西,你可能必须在样式表中使用“悬停”。希望这能给你一些想法。

      【讨论】:

        【解决方案3】:
        table->setStyleSheet("QTableView:item:selected {background-color: #XXYYZZ; color: #FFFFFF}\n"
                             "QTableView:item:selected:focus {background-color: #3399FF;}")
        

        不幸的是,似乎没有“nofocus”属性,因此您只需为所有选定项目设置颜色,然后将焦点颜色覆盖回默认值。 #3399FF 是颜色选择器显示的默认突出显示背景颜色用于我的设置,所以我使用了它。你可以换成你喜欢的颜色。

        color: #FFFFFF在选择失去焦点时将文本颜色设置为自定义。当我有焦点时它对我来说是白色的,所以当它失去焦点时我只是保持它是白色的。您可以使用任何您喜欢的颜色,或者删除该部分以使用默认颜色。

        【讨论】:

        • 项目的双冒号而不是冒号
        【解决方案4】:

        您需要使用自定义委托来根据需要绘制选定的单元格。

        看看QAbstractItemView::setItemDelegate() 方法和QItemDelegate 类。您需要覆盖 QItemDelegate::paint() 方法。 paint 方法采用QStyleOptionViewItem 结构 - 您可以使用它来确定您被要求绘制的项目是否被选中。

        QItemDelegate::paint 的 Qt 文档有可以做到这一点的示例代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-16
          • 2013-11-14
          相关资源
          最近更新 更多