2

When a QPainter is created, it has some default render hints. Some widgets override them when painting themselves. Is it possible to override these defaults and disable the per-widget overrides for the entire application?

I'd like to override the defaults as follows, and make all widget classes follow these:

painter->setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing, false);
painter->setRenderHints(QPainter::TextAntialiasing , true);

Is it possible?

UPDATE:

Short answer: not possible without changing Qt source code.

synacker
  • 1,722
  • 13
  • 32
  • You can subclass `QPainter` and put that code in the constructor – gengisdave Aug 31 '15 at 13:31
  • 1. This not possible, because not exists object of QPainter in constructor. 2. I need to redefine for all widgets these flags. – synacker Aug 31 '15 at 13:35
  • I meant in the subclassed `QPainter` constructor – gengisdave Aug 31 '15 at 13:41
  • Given that these hints only really make any difference for a couple of widgets, the global change isn't really necessary. This is needed for your own widgets, and for `QGraphicsView`, and that's pretty much it. – Kuba hasn't forgotten Monica Aug 31 '15 at 19:21
  • @KubaOber I work with QTextBrowser, for example. These hints really change line and images imagination. – synacker Sep 01 '15 at 06:06
  • As luck would have it, the render hints that you desire **are in fact the defaults**, and all widgets follow these defaults. Surely you want something else, then? Please edit your question to let us know what set of render hints you really need. – Kuba hasn't forgotten Monica Sep 01 '15 at 13:57
  • @KubaOber in QTextBrowser these hits redefined, In drawBorders method setted antialising hit, for example. I want reject this hits in entire qt application. – synacker Sep 01 '15 at 14:02
  • @KubaOber I just hope that exist way for disabling antialiasing hits globally. In this case setting flag with antialising will do nothing. – synacker Sep 01 '15 at 14:11
  • Yeah, there's no way of doing it using a public API. Using private APIs it's doable but messy. I'd suggest modifying `QPainter` source code, that would probably be the most straightforward way. – Kuba hasn't forgotten Monica Sep 01 '15 at 14:12

2 Answers2

1

Unfortunately, Qt doesn't implement any public way of doing this.

There are two issues:

  1. The default render hint - QPainter::TextAntialiasing is set in QPainter::begin(QPaintDevice*). This is exactly what you wanted according to your question, but

  2. The widgets are free to override these defaults. And many of them do. There is no way to disable that, without inserting a shim paint engine (or similar) that would intercept these and ignore them.

The simplest way to change it is to modify the QPainter::setRenderHint and QPainter::setRenderHints to disable the overrides on certain widget types, and rebuild Qt. In any professional setting you'll be using your own build of Qt anyway, so that shouldn't be an issue.

There probably is a way of hooking it using Qt's private headers, most likely by offering a shim paint engine and swapping it out on the backing store, without modifying Qt itself, but it'll be messy and not worth it.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • In your case also exist problem, that widget can redefine render hits flags in paintEvent such as, for example, QTextBrowser. – synacker Sep 01 '15 at 13:59
  • Sorry, I mean QTextDocumentLayout - https://github.com/Vitallium/qt5/blob/master/qtbase/src/gui/text/qtextdocumentlayout.cpp#L844 – synacker Sep 01 '15 at 14:07
  • @Milovidow Oops, sorry, I was grepping the wrong thing (the plural `setRenderHints` instead of `setRenderHint`). As you know from the code, there is now way to override these without delving into Qt's internals... – Kuba hasn't forgotten Monica Sep 01 '15 at 14:09
-2

You can subclass QPainter with:

class MyQPainter: public QWidget
{
    Q_OBJECT;
    public:
        MyQPainter(QWidget *parent = 0);
        QPainter painter;
}

and:

MyQPainter::MyQPainter(QWidget *parent)
    : QWidget(parent)
{
    painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing, false);
    painter.setRenderHints(QPainter::TextAntialiasing , true);
}

now, you can declare MyQPainter *pPainter = new MyQPainter();

gengisdave
  • 1,969
  • 5
  • 21
  • 22