PDA

View Full Version : custom shaped buttons



jsabater
1st July 2009, 17: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

aamer4yu
1st July 2009, 18:04
I wonder if u really searched ;)
Even with the same heading as your thread - "Custom shaped button" I could find this (http://www.qtcentre.org/forum/search.php?searchid=841181).

Also if u had seen shaped clock example in Qt Demo, it will give you a good idea.

jsabater
1st July 2009, 18:10
My problem is when clicking the "non-visible" regions of the widget.

The button is displaying fine... I already had a look in the forum and there is nothing with "hitButton"


Regards

nish
2nd July 2009, 03:03
try

int qAlpha ( QRgb rgba )


bool GuiButton::hitButton ( const QPoint & pos ) const
{
//QColor mcolor = icon().pixmap(iconSize()).alphaChannel().toImage() .pixel(pos);

if ( qAlpha (icon().pixmap(iconSize()).alphaChannel().toImage( ).pixel(pos))== 0)
return false;
else
return true;

}

aamer4yu
2nd July 2009, 05:13
My problem is when clicking the "non-visible" regions of the widget.
Did you come across QWidget::setMask in those posts ?

jsabater
2nd July 2009, 14:17
Thanks MrDeath and aamer4yu for your replies,

Unfortunately none helped.

When clicking in the transparent region of the Widget (also using "setMask") the widget emits de PRESSED signal.


Any other hint how to use "hitButton"?