Results 1 to 6 of 6

Thread: custom shaped buttons

  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 18:56. Reason: new title

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom shaped buttons

    I wonder if u really searched
    Even with the same heading as your thread - "Custom shaped button" I could find this.

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

  3. #3
    Join Date
    Mar 2009
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Exclamation Re: custom shaped buttons

    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

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: custom shaped buttons

    try

    int qAlpha ( QRgb rgba )

    Qt Code:
    1. bool GuiButton::hitButton ( const QPoint & pos ) const
    2. {
    3. //QColor mcolor = icon().pixmap(iconSize()).alphaChannel().toImage().pixel(pos);
    4.  
    5. if ( qAlpha (icon().pixmap(iconSize()).alphaChannel().toImage().pixel(pos))== 0)
    6. return false;
    7. else
    8. return true;
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom shaped buttons

    My problem is when clicking the "non-visible" regions of the widget.
    Did you come across QWidget::setMask in those posts ?

  6. #6
    Join Date
    Mar 2009
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: custom shaped buttons

    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"?
    Last edited by jsabater; 2nd July 2009 at 16:45.

Similar Threads

  1. Best Practice - delete pointer QAbstractButton in QMessageBox
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 13: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.