jsabater
1st July 2009, 18:41
Hi all,
I know similar topics have been addressed in the forum but I was not able to found a solution to my problem.
I want to implement a non-squared button (that emits clicked signals only when the visible part is pressed)
I subclass from QAbstractButton.
This is how my constructor looks like.
GuiButton::GuiButton(QWidget *parent)
: QAbstractButton(parent)
{
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_NoSystemBackground);
}
My PaintEvent looks like:
void GuiButton::paintEvent ( QPaintEvent * event)
{
Q_UNUSED(event);
QPainter painter(this);
if ( this->isEnabled() )
painter.drawPixmap(0,0,icon().pixmap(iconSize(),QI con::Normal));
else
painter.drawPixmap(0,0,icon().pixmap(iconSize(),QI con::Disabled));
}
My Icon is a PNG with transparent regions (round button).
The widget displays well (transparency...). However, when I click in the transparent region it emits signals.
According to the documentation, reimplementing "hitButton" should do the trick, but I do not get it working...
This is my code:
bool GuiButton::hitButton ( const QPoint & pos ) const
{
QColor mcolor = icon().pixmap(iconSize()).alphaChannel().toImage() .pixel(pos);
if ( mcolor.alpha() == 0)
return false;
else
return true;
}
mcolor.alpha() is always 255 (independently) of the coordinate.
In addition, I have problems with the "clicked" signal. "pressed" works fine, but not "clicked".
I am using Qt 4.5 under Linux.
Thank you
I know similar topics have been addressed in the forum but I was not able to found a solution to my problem.
I want to implement a non-squared button (that emits clicked signals only when the visible part is pressed)
I subclass from QAbstractButton.
This is how my constructor looks like.
GuiButton::GuiButton(QWidget *parent)
: QAbstractButton(parent)
{
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_NoSystemBackground);
}
My PaintEvent looks like:
void GuiButton::paintEvent ( QPaintEvent * event)
{
Q_UNUSED(event);
QPainter painter(this);
if ( this->isEnabled() )
painter.drawPixmap(0,0,icon().pixmap(iconSize(),QI con::Normal));
else
painter.drawPixmap(0,0,icon().pixmap(iconSize(),QI con::Disabled));
}
My Icon is a PNG with transparent regions (round button).
The widget displays well (transparency...). However, when I click in the transparent region it emits signals.
According to the documentation, reimplementing "hitButton" should do the trick, but I do not get it working...
This is my code:
bool GuiButton::hitButton ( const QPoint & pos ) const
{
QColor mcolor = icon().pixmap(iconSize()).alphaChannel().toImage() .pixel(pos);
if ( mcolor.alpha() == 0)
return false;
else
return true;
}
mcolor.alpha() is always 255 (independently) of the coordinate.
In addition, I have problems with the "clicked" signal. "pressed" works fine, but not "clicked".
I am using Qt 4.5 under Linux.
Thank you