Qt-interest Archive, April 2007
Transparency problem
Message 1 in thread
Hi folks,
I've got a bit of a problem regarding subclassing QSlider and QComboBox.
The subclassed widgets will draw some new predefined graphics with some
transparent parts. But it doesn't work very well, there is severe
graphical distortion, especially when resizing. The behaviour is not
identical between platforms, mac nearly works, linux, not so much,
didn't test on windows lately, think it was about the same as mac.
I have created a small sample which demonstrates the basics of what I'm
trying to do, resizing the window produces some nice psychedelic effects.
Am I doing something totally wrong or is there possibly a bug lurking here?
Oh, this is with qt 4.2.3, going to test with the current snapshot.
---- test.cpp ----
#include <QtGui>
class Slider : public QSlider
{
QPixmap *bg;
public:
Slider() {
bg=NULL;
}
protected:
void paintEvent(QPaintEvent *ev) {
if (bg)
delete bg;
bg = new QPixmap(ev->rect().size());
QPainter b(bg);
b.fillRect(ev->rect(),QBrush(QColor(0,0,0,0))); // transparent,
paints wierd things
// b.fillRect(ev->rect(),QBrush(QColor(0,0,0,255))); // no
transparency, works
QPainter p(this);
p.drawPixmap(0,0,*bg);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow *t = new QMainWindow();
Slider *slider = new Slider();
t->setCentralWidget(slider);
t->show();
return app.exec();
}
---- test.cpp ----
Regards,
Robert
--
[ signature omitted ]
Message 2 in thread
Hello again,
Tested with yesterdays snapshot of qt4.3 with the same result.
I've attached a revised test that subclasses a QWidget instead, (and
implemented it a bit more c++ish, it didn't get that much longer).
The result is the same, when filling the image with transparency enabled
(or not filling at all) there are a lot of junk painted.
Please, does anyone have any idea what I am doing wrong?
Regards,
Robert
Robert Jonsson wrote:
> Hi folks,
>
> I've got a bit of a problem regarding subclassing QSlider and QComboBox.
> The subclassed widgets will draw some new predefined graphics with some
> transparent parts. But it doesn't work very well, there is severe
> graphical distortion, especially when resizing. The behaviour is not
> identical between platforms, mac nearly works, linux, not so much,
> didn't test on windows lately, think it was about the same as mac.
>
> I have created a small sample which demonstrates the basics of what I'm
> trying to do, resizing the window produces some nice psychedelic effects.
> Am I doing something totally wrong or is there possibly a bug lurking here?
>
> Oh, this is with qt 4.2.3, going to test with the current snapshot.
>
>
> Regards,
> Robert
>
#include <QtGui>
#include <QtGui>
class Widget : public QWidget {
QPixmap *bg;
public:
Widget();
protected:
void paintEvent(QPaintEvent *ev);
};
Widget::Widget() {
bg=NULL;
}
void Widget::paintEvent (QPaintEvent *ev) {
if (bg)
delete bg;
bg = new QPixmap(ev->rect().size());
QPainter b(bg);
b.fillRect(ev->rect(),QBrush(QColor(0,0,0,0))); // transparent, paints wierd things
// b.fillRect(ev->rect(),QBrush(QColor(0,0,0,255))); // no transparency, works
QPainter p(this);
p.drawPixmap(0,0,*bg);
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QMainWindow *t = new QMainWindow();
Widget *widget = new Widget();
t->setCentralWidget(widget);
t->show();
return app.exec();
}
Message 3 in thread
On Monday 23 April 2007 06:26, Robert Jonsson wrote:
> bg = new QPixmap(ev->rect().size());
> Â Â QPainter b(bg);
> Â Â b.fillRect(ev->rect(),QBrush(QColor(0,0,0,0)));
This is a no-op.
The default composition mode for QPainter is source-over. Meaning what you're
saying is "compose fully transparent rectangle on top of whatever is there in
my pixmap".
Since what you have in your pixmap at that point is uninitialized garbage -
that's what you're seeing.
You either need to use QPixmap::fill method (for example as in
(pixmap.fill(Qt::transparent)) or set CompositionMode on the QPainter to
QPainter::CompositionMode_Source, which will correctly replace the pixels in
pixmap with transparent ones and not compose them.
--
[ signature omitted ]
Message 4 in thread
Hello,
Zack Rusin wrote:
> On Monday 23 April 2007 06:26, Robert Jonsson wrote:
>> bg = new QPixmap(ev->rect().size());
>> QPainter b(bg);
>> b.fillRect(ev->rect(),QBrush(QColor(0,0,0,0)));
>
> This is a no-op.
>
> The default composition mode for QPainter is source-over. Meaning what you're
> saying is "compose fully transparent rectangle on top of whatever is there in
> my pixmap".
> Since what you have in your pixmap at that point is uninitialized garbage -
> that's what you're seeing.
> You either need to use QPixmap::fill method (for example as in
> (pixmap.fill(Qt::transparent)) or set CompositionMode on the QPainter to
> QPainter::CompositionMode_Source, which will correctly replace the pixels in
> pixmap with transparent ones and not compose them.
Great!
I opted for the first solution and it seems to work perfectly.
Thanks.
Robert
--
[ signature omitted ]