QTableWidget选择颜色

发布于 2024-10-31 05:40:20 字数 383 浏览 0 评论 0原文

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

我已经尝试过这个:

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

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

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

预先感谢, 问候 马蒂亚斯

I want selected Cells to have a different background color. By default there is only a thin underline in the selected cell.

I've tried this:

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

but it only changes the Color that is shown while the pointer is on a Cell. After the pointer is away, wether I select the cell by table->selectRow(selRow) there is only the underline. Probably it looks differen on other plattforms.

There are a lot of thread with equal topic, but most answer is using the Stylesheet above. Nothing worked, only the "moseover Color" changes.

Thanks in advance, Regards
Matthias

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独难免 2024-11-07 05:40:20
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->setItemDelegateForColumn(2, new BackgroundDelegate(this));

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();
    }
  }
};

then
table->setItemDelegateForColumn(2, new BackgroundDelegate(this));

宛菡 2024-11-07 05:40:20

这就是我所做的。

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);

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

这适用于选定的单元格,就像选定的行一样。如果您想要“鼠标悬停”的内容,您可能必须在样式表中使用“悬停”。希望这能给你带来想法。

This is what I did.

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);

The selection color does the one item that is selected while the item focus will color the rest of the items that should be highlighted.

This works for the selected cells like having a selected row. If you want something for "mouse over" you may have to use "hover" in the style sheet. Hope this can give you ideas.

活泼老夫 2024-11-07 05:40:20
table->setStyleSheet("QTableView:item:selected {background-color: #XXYYZZ; color: #FFFFFF}\n"
                     "QTableView:item:selected:focus {background-color: #3399FF;}")

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

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

table->setStyleSheet("QTableView:item:selected {background-color: #XXYYZZ; color: #FFFFFF}\n"
                     "QTableView:item:selected:focus {background-color: #3399FF;}")

Unfortunately there doesn't seem to be a "nofocus" property, so you just have to set the colour for all selected items, then override the focused colour back to the default. #3399FF is what a colour picker revealed the default highlight background colour was for my setup, so I used that. You can substitute with whichever colour you like.

The color: #FFFFFF sets the text colour to something custom when the selection loses focus. It's white for me when I have focus, so I just keep it as white when it loses focus. You can use whatever colour you like, or remove that part to use the default.

江南月 2024-11-07 05:40:20

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

查看 QAbstractItemView::setItemDelegate() 方法和 QItemDelegate 类。您需要重写QItemDelegate::paint()方法。 Paint 方法采用 QStyleOptionViewItem 结构 - 您可以使用它来确定是否选择了要求您绘制的项目。

QItemDelegate::paint 的 Qt 文档具有完全执行此操作的示例代码。

You need to use a custom delegate to paint selected cells as you want them.

Have a look at the QAbstractItemView::setItemDelegate() method and the QItemDelegate class. You'll need to override the QItemDelegate::paint() method. The paint method takes a QStyleOptionViewItem structure - you can use this to determine if the item you're being asked to paint is selected.

The Qt docs for QItemDelegate::paint have sample code that does exactly that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文