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
){
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_NoSystemBackground);
}
GuiButton::GuiButton(QWidget *parent)
: QAbstractButton(parent)
{
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_NoSystemBackground);
}
To copy to clipboard, switch view to plain text mode
My PaintEvent looks like:
{
Q_UNUSED(event);
if ( this->isEnabled() )
painter.
drawPixmap(0,
0,icon
().
pixmap(iconSize
(),
QIcon::Normal));
else
painter.
drawPixmap(0,
0,icon
().
pixmap(iconSize
(),
QIcon::Disabled));
}
void GuiButton::paintEvent ( QPaintEvent * event)
{
Q_UNUSED(event);
QPainter painter(this);
if ( this->isEnabled() )
painter.drawPixmap(0,0,icon().pixmap(iconSize(),QIcon::Normal));
else
painter.drawPixmap(0,0,icon().pixmap(iconSize(),QIcon::Disabled));
}
To copy to clipboard, switch view to plain text mode
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks