PDA

View Full Version : transparency problem. pls help.



marc2050
3rd August 2011, 06:29
Here's my codes.
Attached is the result of the image.

You can see that the parts that are transparent ends up with black color.
What gives? I like it to be the background color of whatever that is underneath this rainbow drawing. How do I do that?

Many thanks!



mypainter->setPen(Qt::NoPen);

QLinearGradient rect_gradient(0, 0, 0, height());
rect_gradient.setColorAt(0, Qt::red);
rect_gradient.setColorAt(.17, Qt::yellow);
rect_gradient.setColorAt(.33, Qt::green);
rect_gradient.setColorAt(.50, Qt::cyan);
rect_gradient.setColorAt(.66, Qt::blue);
rect_gradient.setColorAt(.81, Qt::magenta);
rect_gradient.setColorAt(1, Qt::red);
mypainter->setBrush(rect_gradient);
mypainter->drawRect(width() / 2, 0, width() / 2, height());

QLinearGradient alpha_gradient(0, 0, width(), 0);
alpha_gradient.setColorAt(0, Qt::white);
alpha_gradient.setColorAt(0.2, Qt::white);
alpha_gradient.setColorAt(0.5, Qt::transparent);
alpha_gradient.setColorAt(0.8, Qt::white);
alpha_gradient.setColorAt(1, Qt::white);

mypainter->setCompositionMode(QPainter::CompositionMode_Desti nationIn);
mypainter->setBrush(alpha_gradient);
mypainter->drawRect(0, 0, width(), height());

mypainter->setCompositionMode(QPainter::CompositionMode_Desti nationOver);

mypainter->setPen(Qt::NoPen);
mypainter->setRenderHint(QPainter::SmoothPixmapTransform);

Lykurg
3rd August 2011, 07:23
Hi,

see QWidget::setAutoFillBackground(). Also have a look at the windget attribute Qt::WA_NoSystemBackground.

marc2050
3rd August 2011, 07:53
I tried that actually. They seem to have no effect over the results I'm getting. :(
Any clue??

marc2050
3rd August 2011, 10:02
Could someone please help me on this?
I've tried all kind of compostion mode and the suggestions. None of them work.
And I've been stuck on this for hours and hours. A seemingly simple task of drawing a partial transparent stuff yet it seems impossible to do...

Lykurg
3rd August 2011, 10:34
Ehm, yes, why do you alter the composition mode?
#include <QtGui>

class test : public QWidget
{
public:
test(QWidget* parent) : QWidget(parent)
{
}

protected:
void paintEvent(QPaintEvent*)
{
QPainter mypainter(this);
mypainter.fillRect(0, 0, width(), height(), Qt::transparent);
mypainter.setPen(Qt::NoPen);

QLinearGradient rect_gradient(0, 0, 0, height());
rect_gradient.setColorAt(0, Qt::red);
rect_gradient.setColorAt(.17, Qt::yellow);
rect_gradient.setColorAt(.33, Qt::green);
rect_gradient.setColorAt(.50, Qt::cyan);
rect_gradient.setColorAt(.66, Qt::blue);
rect_gradient.setColorAt(.81, Qt::magenta);
rect_gradient.setColorAt(1, Qt::red);
mypainter.setBrush(rect_gradient);
mypainter.drawRect(width() / 2, 0, width() / 2, height());

QLinearGradient alpha_gradient(0, 0, width(), 0);
alpha_gradient.setColorAt(0, Qt::white);
alpha_gradient.setColorAt(0.2, Qt::white);
alpha_gradient.setColorAt(0.5, Qt::transparent);
alpha_gradient.setColorAt(0.8, Qt::white);
alpha_gradient.setColorAt(1, Qt::white);

// mypainter.setCompositionMode(QPainter::Composition Mode_DestinationIn);
mypainter.setBrush(alpha_gradient);
mypainter.drawRect(0, 0, width(), height());

mypainter.setCompositionMode(QPainter::Composition Mode_DestinationOver);

mypainter.setPen(Qt::NoPen);
mypainter.setRenderHint(QPainter::SmoothPixmapTran sform);
}
};

int main(int argc, char* argv[])
{
QApplication a(argc, argv);

QWidget parent;
parent.setBackgroundRole(QPalette::ToolTipBase);
test t(&parent);
parent.show();

return a.exec();
}

marc2050
3rd August 2011, 13:42
Thanks. I comment out the codes you suggested.
and ended up with the attached picture.
It still does not make sense 'cause the transparent part should be
somewhere in the middle of the picture since that's where I
set the 'transparent'. yet in the picture, it is the right side that
seems to be getting the transparent. Beats me!? I just cant figure
out...
thank you for any pointer.

Lykurg
3rd August 2011, 14:07
? no it is white at left and right. Only in the middle it is transparent:
alpha_gradient.setColorAt(0.5, Qt::transparent); And keep in mind, you are drawing with a transparent pen, you are not making all pixel in that area transparent.

marc2050
3rd August 2011, 14:18
Sorry. I must be missing something terribly...
The expected results I thought I should get is as shown in the attached file.
(ignore the checkboard patten in the background. main idea is the transparency, where does it start)

So, this expected result has the transparency starts from the middle of the window and then slowly goes back to the "rainbow" color. But the actual result, after commenting out the code you suggested ends up the opposite.
I really dont get it. What really am I wrong in understanding what I should get?
Thanks once again.

Lykurg
3rd August 2011, 14:35
Well for that you have to cache the result in a pixmap or image:
#include <QtGui>

class test : public QWidget
{
public:
test(QWidget* parent) : QWidget(parent)
{
}

protected:
void paintEvent(QPaintEvent*)
{

QPixmap pix(width(), height());
pix.fill(Qt::transparent);

QPainter mypainter(&pix);
mypainter.setPen(Qt::NoPen);

QLinearGradient alpha_gradient(0, 0, width(), 0);
alpha_gradient.setColorAt(0, Qt::white);
alpha_gradient.setColorAt(0.2, Qt::white);
alpha_gradient.setColorAt(0.5, Qt::transparent);
alpha_gradient.setColorAt(0.8, Qt::white);
alpha_gradient.setColorAt(1, Qt::white);
mypainter.setBrush(alpha_gradient);
mypainter.drawRect(0, 0, width(), height());

mypainter.setCompositionMode(QPainter::Composition Mode_SourceIn);

QLinearGradient rect_gradient(0, 0, 0, height());
rect_gradient.setColorAt(0, Qt::red);
rect_gradient.setColorAt(.17, Qt::yellow);
rect_gradient.setColorAt(.33, Qt::green);
rect_gradient.setColorAt(.50, Qt::cyan);
rect_gradient.setColorAt(.66, Qt::blue);
rect_gradient.setColorAt(.81, Qt::magenta);
rect_gradient.setColorAt(1, Qt::red);
mypainter.setBrush(rect_gradient);
mypainter.drawRect(width() / 2, 0, width() / 2, height());

QPainter p(this);
p.drawPixmap(0,0,pix);

}
};

int main(int argc, char* argv[])
{
QApplication a(argc, argv);

QWidget parent;
parent.setBackgroundRole(QPalette::ToolTipBase);
test t(&parent);
parent.show();

return a.exec();
}

marc2050
3rd August 2011, 20:51
You are the man!!! THANK YOU!!