gpt4 book ai didi

qt - 更改 QRubberBand 的颜色

转载 作者:行者123 更新时间:2023-12-02 11:23:27 24 4
gpt4 key购买 nike

我正在尝试更改 QRubberBand 的颜色。我试过这个:

QRubberBand* rubberBand = new QRubberBand(QRubberBand::Rectangle, this->graphicsView);
QPalette palette;
palette.setBrush(QPalette::Foreground, QBrush(Qt::green));
palette.setBrush(QPalette::Base, QBrush(Qt::red));

rubberBand->setPalette(palette);

rubberBand->resize(30, 30);
rubberBand->show();

但是出现的橡皮筋是标准的黑色虚线橡皮筋。关于如何做到这一点有什么想法吗?

最佳答案

实际渲染取决于平台,但对于 Windows(可能是其他平台),您可以设置 QPalette::Highlight 颜色角色。这个例子有效:

#include <QtGui>

int main(int argc, char **argv) {
QApplication app(argc, argv);
QRubberBand band(QRubberBand::Rectangle);

QPalette pal;
pal.setBrush(QPalette::Highlight, QBrush(Qt::red));
band.setPalette(pal);

band.resize(30, 30);
band.show();
return app.exec();
}

如果您使用其他平台,请告诉我,我可以看看哪些内容适用于该平台。

但是,由于您引用的是图形 View ,我想知道您是否指的是所选项目周围出现的黑色虚线?这是完全不同的东西,并且根本不涉及 QRubberBand 。如果是这种情况,您可能需要更新问题。

顺便说一句,我查看了 QWindowsVistaStyle 的代码来找到答案。如果您位于不同的平台上并且可以访问源代码,则可以查找该平台的正确样式类。

更新 看起来您对 Ubuntu 的运气不好。它使用QCleanLooksStyle,它为橡皮筋调用QWindowsStyle。在那里,颜色是硬编码的:

QPixmap tiledPixmap(16, 16);
QPainter pixmapPainter(&tiledPixmap);
pixmapPainter.setPen(Qt::NoPen);
pixmapPainter.setBrush(Qt::Dense4Pattern);
pixmapPainter.setBackground(Qt::white);
pixmapPainter.setBackgroundMode(Qt::OpaqueMode);
pixmapPainter.drawRect(0, 0, tiledPixmap.width(), tiledPixmap.height());
... etc ...

此外,Qt Style Sheets Reference没有将 QRubberBand 列为遵循任何样式。如果您确实需要不同的颜色,唯一的选择可能是继承 QRubberBand 并实现 paintEvent。真糟糕。

关于qt - 更改 QRubberBand 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8140070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com