Results 1 to 6 of 6

Thread: custom shaped buttons

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question custom shaped buttons

    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.
    Qt Code:
    1. GuiButton::GuiButton(QWidget *parent)
    2. : QAbstractButton(parent)
    3. {
    4. setAttribute(Qt::WA_TranslucentBackground);
    5. setAttribute(Qt::WA_NoSystemBackground);
    6. }
    To copy to clipboard, switch view to plain text mode 

    My PaintEvent looks like:
    Qt Code:
    1. void GuiButton::paintEvent ( QPaintEvent * event)
    2. {
    3. Q_UNUSED(event);
    4.  
    5. QPainter painter(this);
    6.  
    7. if ( this->isEnabled() )
    8. painter.drawPixmap(0,0,icon().pixmap(iconSize(),QIcon::Normal));
    9. else
    10. painter.drawPixmap(0,0,icon().pixmap(iconSize(),QIcon::Disabled));
    11. }
    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:
    Qt Code:
    1. bool GuiButton::hitButton ( const QPoint & pos ) const
    2. {
    3. QColor mcolor = icon().pixmap(iconSize()).alphaChannel().toImage().pixel(pos);
    4.  
    5. if ( mcolor.alpha() == 0)
    6. return false;
    7. else
    8. return true;
    9.  
    10. }
    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
    Last edited by jsabater; 1st July 2009 at 17:56. Reason: new title

Similar Threads

  1. Best Practice - delete pointer QAbstractButton in QMessageBox
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 12:43

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.