I have a QPushButton
that I am styling with a border-image through a stylesheet. However, the image quality is poor because the image isn't being drawn using antialiasing.
Is there any way to enable antialiasing simply without subclassing QPushButton
and giving it a new painter? I don't really want to do that because I'm using the style sheet extensively and would have to create a bunch of QProperties to emulate the existing pseudo-state functionality.
You can normally set the Anti-aliasing flag, on a QPainter object using the setRenderHints method. This is normally done by Subclassing the widget and overriding the paintEvent.
According to the Qt Docs for QPainter:
... When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent() ...
Without subclassing, you will be limited to intercepting the paintEvent using an Event Filter and setting the flag yourself.
You will need to create a class that overrides the event handler for your object. This class will be installed using QObject::installEventFilter
. This class will need to filter the events to handle the specific ones that you care about (QPaintEvent). Then it will need to create a QPainter object that takes in the originating object(using the second constructor) as its device as shown in this qtforum post. This works because QWidget inherits from QPaintDevice.
...
myView::handlePaintEvent(QObject *obj, QEvent *eve)
{
QPainter painter(static_cast<QWidget *>(obj));
}
...
From here you should be able to set the proper Render Hints to what you need them to be.
This same event filter class can be installed on numerous objects so the same functionality can be added very quickly and without subclassing any other widget.