简体   繁体   English

更改QRubberBand的颜色

[英]Change the color of a QRubberBand

I am trying to change the color of a QRubberBand. 我正在尝试更改QRubberBand的颜色。 I tried this: 我尝试了这个:

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

but the rubberband that appears is the standard black dotted one. 但是出现的橡皮筋是标准的黑色虚线。 Any ideas on how to do this? 有关如何执行此操作的任何想法?

The actual rendering depends on the platform, but for Windows (perhaps others), you set the QPalette::Highlight color role. 实际的渲染取决于平台,但是对于Windows(也许还有其他),您可以设置QPalette::Highlight颜色角色。 This example works: 此示例有效:

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

If you are on a different platform, let me know and I can see what works for that platform. 如果您使用的是其他平台,请告诉我,我会看到适用于该平台的内容。

However , since you refer to a graphics view, I'm wondering if you mean the black dotted line that appears around selected items? 但是 ,由于您引用的是图形视图,所以我想知道您是说出现在所选项目周围的黑色虚线吗? That is something completely different, and doesn't involve the QRubberBand at all. 那是完全不同的东西,根本不涉及QRubberBand If that is the case, you might need to update the question. 在这种情况下,您可能需要更新问题。

BTW, I looked at the code for QWindowsVistaStyle to track down the answer. 顺便说一句,我看了QWindowsVistaStyle的代码以QWindowsVistaStyle答案。 If you are on a different platform and have access to the source, you can look in the correct style class for that platform. 如果您在其他平台上并且可以访问源代码,则可以在该平台上查找正确的样式类。

UPDATE Looks like you are out of luck with Ubuntu. 更新看起来您对Ubuntu感到不走运。 That uses QCleanLooksStyle , which calls QWindowsStyle for a rubber band. 它使用QCleanLooksStyle ,它将QWindowsStyle用作橡皮筋。 In there, the colors are hard-coded: 在那里,颜色是硬编码的:

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 ...

Also, the Qt Style Sheets Reference doesn't list QRubberBand as obeying any styles. 另外,《 Qt样式表参考 》未将QRubberBand列为遵循任何样式。 If you really need a different color, your only option might be to subclass QRubberBand and implement paintEvent . 如果您确实需要其他颜色,则唯一的选择是QRubberBand并实现paintEvent Bummer. 笨蛋

The simplest way is using QGraphicsColorEffect, as follows: 最简单的方法是使用QGraphicsColorEffect,如下所示:

QGraphicsColorizeEffect *e = new QGraphicsColorizeEffect(rubberBand_);
e->setColor(QColor("red"));
rubberBand_->setGraphicsEffect(e);

However, QGraphicsEffects are deprecated in Qt 4.8 with the reason "flawed design". 但是,Qt 4.8不推荐使用QGraphicsEffects,其原因是“设计有缺陷”。 Color and dropshadow effects are used extensively in my projects to render non-widget items, menus and fonts. 颜色和阴影效果在我的项目中被广泛使用,以渲染非小部件项目,菜单和字体。 I hope Nokia would provide equivalent solutions before deprecate the entire feature. 我希望诺基亚在淘汰整个功能之前会提供等效的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM