Results 1 to 15 of 15

Thread: Hopefully the last time: round buttons

  1. #1
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Hopefully the last time: round buttons

    Yesterday I found a thread with "how to shape QPushbutton" and something like

    QIcon icon("myicon");
    ..dontknow...setMask(icon.getMask);

    and now I am searching again and find lots of "shapings" and "round widgets", but not the mentioned code

    So, can we do it one more time and then put the infos into the wiki? Round Buttons (or similiar)?

    In Qt3, it was like
    Qt Code:
    1. void TGuiObject::setBitmap(QWidget* w, const QString& file)
    2. {
    3. if( !file.isEmpty() && QFile::exists(file) ) {
    4. QPixmap pix(file);
    5. if( QFile::exists(file+fileMask) )
    6. pix.setMask(QBitmap(file+fileMask));
    7. QPalette pal;
    8. pal.setBrush(w->backgroundRole(), QBrush(pix));
    9. w->setPalette(pal);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    wasn'it?

    I wanted the Starfleet logo to be the button. When I click it with the above code, it is not really shaped... it is still a rectangle with white corners around the round image...
    Attached Images Attached Images

  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: Hopefully the last time: round buttons

    Check your bitmap, if its really transparent around the white area u get in ur button.
    setMask will operate on the opaque pixels, and if all pixels of you bitmap is opaque, you will get the result you ae now getting.

  3. #3
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Hopefully the last time: round buttons

    Ok, so I have to change from .jpg to .png. No problem. Now I have a round bitmap, thanks for that.
    But when I click the button, I see only a grey background.

    I read something about the style sheet property "border-radius" in the other thread - and the helps says "not available on Mac". Do I really have to reimplement the paint event?

  4. #4
    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: Hopefully the last time: round buttons

    Have you set any style sheet on button press ??
    How are you using the button ?

  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Hopefully the last time: round buttons

    i think stylesheet property "border-image" could very well solve the problem, but in that case, your image should only be that round thing, there shouldnt be any transparent white area around

  6. #6
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Hopefully the last time: round buttons

    Quote Originally Posted by aamer4yu View Post
    Have you set any style sheet on button press ??
    How are you using the button ?
    Until now, I did not use any style sheets, as the help says, that styles on Mac currently don't work. So I'd like to set the "basics stuff" by QPushButton's methods... my code is above, I only added the shaping mask function.

    Quote Originally Posted by talk2amulya View Post
    i think stylesheet property "border-image" could very well solve the problem
    Just to be sure, I changed it into the following code:
    Qt Code:
    1. if( !file.isEmpty() && QFile::exists(file) ) {
    2. QString style = w->styleSheet();
    3. style += QString("background-image: url(%1);").arg(file);
    4. style += QString("pressed: background-image: url(%1);").arg(file);
    5. w->setStyleSheet(style);
    6. if( QFile::exists(file+".mask") ) {
    7. QBitmap mask(file+".mask");
    8. w->setMask(mask);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    and I now see... well... some kind of bitmap

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Hopefully the last time: round buttons

    The simplest way to obtain what you want is to make the png image transculent so that the transparency "blends" into the image. Then you can use QPixmap::createHeuristicMask() or QPixmap::createMaskFromColor() to get the mask and to be sure you won't have any sharp edges on the button.

    There is also an alternative - subclass QPushButton or QAbstractButton and do the painting yourself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Hopefully the last time: round buttons

    I think I am nearby. I converted the "white" to "transparent" and saved it as png. I set the mask with the code of my first writing, and the button is displayed round. Obviously, setting the brush with an image an its mask is ok.

    The only problem left is that the pressed has neither a background nor a text, so all I get is a grey pressed button rectangle. Obviously, I have to set the brush of the pressed-background role. But I do not know how?

    Maybe I really should paint it on my own, to ensure that the button looks like it was pressed down a little bit?

  9. #9
    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: Hopefully the last time: round buttons

    And what stylesheet are u using for "pressed" state ?

  10. #10
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Hopefully the last time: round buttons

    QWidget::styleSheet : QString
    ...
    Note: Qt style sheets are currently not supported for QMacStyle (the default style on Mac OS X). We plan to address this in some future release.

    -> there must be an option without stylesheets, mustn't it? Currently, I am working on even that OS

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Hopefully the last time: round buttons

    Quote Originally Posted by auba View Post
    there must be an option without stylesheets, mustn't it?
    Sure there is. I even gave it to you...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Hopefully the last time: round buttons

    Sorry, but I did not understand it... to clearify: do I have to paint the button on my own to get the needed result (pressed button with picture)?

    Or does it make a difference if I use the QPixmap::mask() function instead of the createHeuristicMask as you wrote?

    As I wrote as the response to your help: the only thing left is the undrawn picture when pressing down.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Hopefully the last time: round buttons

    The best and probably fastest way to achieve what you want is to provide your own button class. It's essentially less than 10 lines of code if you're only after drawing and less than 15 lines of code if you also want the hit area of the button to be shaped.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Aug 2008
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Hopefully the last time: round buttons

    Is the problem only with QMacStyle? What you pasted, auba, states that it is the default style on the mac. Can you just set the default style to Plastique or Cleanlooks?

    While I've built a qt application on/for OS X, it was in Cleanlooks and didn't use any stylesheets (and was quite awhile ago) so I'm unsure of the actual behavior. Can OS X run a different Style?

  15. #15
    Join Date
    Apr 2009
    Posts
    21
    Thanked 3 Times in 2 Posts

    Default Re: Hopefully the last time: round buttons

    I wonder why you guys not try to use QPushButton::setFlat(true) and add a PNG as its icon.
    I think it is the easiest way to create shaped button and it's platform independent.

    Here's my widget. Look at the bottom-right buttons:



    Is it what you want?
    Attached Images Attached Images
    Welcome to My Chinese Qt Blog (Google Translate Integrated) - http://www.xlrw.co.cc
    Dedicated to make Qt popular in Chinese-speaking society.

Similar Threads

  1. Once again: buttons and time consuming tasks
    By pampo in forum Qt Programming
    Replies: 1
    Last Post: 4th May 2009, 18:26
  2. How to constantly refresh time on a view
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2008, 12:44
  3. Replies: 1
    Last Post: 1st February 2008, 18:55
  4. QDateTime GMT add sec. or - sec. from locale time....
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2007, 16:39
  5. Problem with pointers while using localtime() and time()
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 11th January 2006, 15:48

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.