选择单元格时,更改QTableView中图标的颜色突出显示

时间:2013-12-20 11:52:03

标签: c++ user-interface qtableview

在QTableView中选择单元格时,其中的图标会显示蓝色高亮显示,如何控制此高亮显示的颜色或将其禁用?

我尝试设置QPalette :: Highlight但它没有用。

编辑:

好的,所以我知道如何更改背景颜色和文字颜色以及颜色突出显示,但不知道如何更改图标。如果我返回一个图标作为单元格的装饰,则在选择单元格时会给它一个浅蓝色突出显示。如何删除它?

3 个答案:

答案 0 :(得分:3)

您可以使用样式表来定义元素的颜色。 QTableView中所选项目的名称为selection-background-color。因此,更改此元素的颜色,您将选择您喜欢的背景颜色。

#include <QtWidgets/QApplication>
#include <QtWidgets/QTableView>
#include <QStandardItemModel>

int main(int argc, char* argv[]) {
    QApplication app(argc, argv);

    QTableView *table = new QTableView();
    QStandardItemModel *model = new QStandardItemModel(2,2);

    table->setModel(model);
    table->setStyleSheet("selection-background-color: red");

    table->show();

    return app.exec();
}

看看它在图片中的样子:

enter image description here

答案 1 :(得分:1)

我发现了解决此问题的方法,但与此相关的是一些费用。

根本上,它在Qt代码的深处调用 d := json.NewDecoder(strings.NewReader(st)) var m map[string]interface{} err = d.Decode(&m) if err != nil { log.Fatal(err) } fmt.Println(m) 并通过QIcon::paint()作为图标模式,因此问题在于,图标像素图在所需分辨率下的“选定”形式是一个由Qt自动生成。

我通过将图标的QIcon::Selected形式设置为与Selected模式相同来解决此问题:

Normal

sampleWithNonSelectedIconInTable

缺点是这样做会花费更多时间,可能会花费更多的内存来存储它,而浪费时间来生成我们扔掉的选定图标。

在我的情况下,我使用的是 // Make the "Selected" version of the icon look the same as "Normal". for (const auto& size : icon.availableSizes()) { icon.addPixmap(icon.pixmap(size, QIcon::Normal, QIcon::Off), QIcon::Selected, QIcon::Off); icon.addPixmap(icon.pixmap(size, QIcon::Normal, QIcon::On), QIcon::Selected, QIcon::On); } ,但不幸的是,这并不能让您更紧密地影响图标的呈现方式,而不必完全重新实现样式中的QStyledItemDelegate呈现方式。

请考虑一下,如果您使用代理样式,则不难改写QStyle::CE_ItemViewItem的呈现方式以不使用选定的图标,因此这也是一个选择。

答案 2 :(得分:0)

使用Qt中的标准样式更改此行为是完全不可能的。您需要实现自己的特定样式才能解决此问题。

相关问题