Results 1 to 19 of 19

Thread: Creating Custom Ellipse Button

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Creating Custom Ellipse Button

    I used 0, and I finally saw the picture. However, the clip stuff never works. Also putting everything inside a QHBoxLayout and settingLayout with a widget, makes it dissappear.

    Also Hover absolutely does not work.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: Creating Custom Ellipse Button

    Regarding hover:
    Hover means you just move your mouse over the widget. You switch to hover if the left button is pressed, in mouseMoveEvent, which is not correct.
    To detect hover, there are less processor stressing methods, like QWidget::enterEvent and QWidget::leaveEvent.

    Regarding adding your custom btn to layouts:
    When I said drawing everything relative to (0,0) I meant to the origin of what rect() returns: rect().topLeft().
    Adding to layouts did not worked because everything was drawn in the topleft corner of the layout .

    Hope it works now.

    Regards

  3. #3
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Creating Custom Ellipse Button

    Well how do I add automaticlaly to wherever it needs to draw it...? Is there a way?
    Any code examples?

  4. #4
    Join Date
    Oct 2006
    Posts
    279
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    6
    Thanked 40 Times in 39 Posts

    Default Re: Creating Custom Ellipse Button

    Wouldn't it be easier to do this with style sheets?
    i.e.
    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication app(argc, argv);
    4. QPushButton btn("quit");
    5. btn.setStyleSheet
    6. (
    7. "QPushButton {"
    8. " border-image: url(normal.png) 17% 17% 17% 17% stretch stretch; "
    9. " border-width: 8px;"
    10. "}"
    11. "QPushButton:hover {"
    12. " border-image: url(hover.png) 17% 17% 17% 17% stretch stretch; "
    13. " border-width: 8px;"
    14. "}"
    15. "QPushButton:pressed {"
    16. " border-image: url(pressed.png) 17% 17% 17% 17% stretch stretch; "
    17. " border-width: 8px;"
    18. "}"
    19. );
    20. QObject::connect(&btn, SIGNAL(clicked()), &app, SLOT(quit()));
    21. btn.show();
    22. app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 
    You obviously have to adjust the percentage and border values to fit your bitmap and you won't be able to get a true ellipse since some part of the image must be stretched or repeated depending on the text length, but it seems a lot easier.

  5. The following user says thank you to spud for this useful post:

    WinchellChung (26th June 2007)

  6. #5
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Creating Custom Ellipse Button

    I realize that, I won't be able to get it perfectly. I won't be able to cut the square black background into a transparent one or turn the Square feature of pushbutton into an ELLIPSE feature.

    the Stretch stuff by the way didn't change anything except when the image size is different than the button size.

  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: Creating Custom Ellipse Button

    Hey man, it works just fine if you do it as I've shown you.

    It will work anyway if you use "ready-made" png images for the states. I mean you draw an ellipse in a png and leave everything else transparent. Then just fill the widget with these pixmaps.

    If you use transp pixmaps the you don't need setting any clipping masks.

    Regards

  8. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Creating Custom Ellipse Button

    If you use transp pixmaps the you don't need setting any clipping masks.
    Are you sure about that?
    Is this from experience?
    Some time back I implemented a styled button, which can take any shape, and as I remember, I had to use masks in addition to the png (which had transparent areas).
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: Creating Custom Ellipse Button

    Yes, I have already tried it some time ago.
    Also, I might have filled the widget with a transparent pixmap first, then applied the actual png... Can't remember exactly, but it can be done.

    Regards

  10. #9
    Join Date
    Oct 2011
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Smile Re: Creating Custom Ellipse Button

    Qt Code:
    1. ui->pushButton->setFixedSize(150,100);
    2.  
    3. ui->pushButton->setStyleSheet("QPushButton {"
    4. "background-color: red;"
    5. "border-style: solid;"
    6. "border-width: 3px;"
    7. "border-radius: 75px 50px;"
    8. "border-color: blue;"
    9. "font: bold 14px;"
    10. "padding: 6px;}"
    11. "QPushButton:pressed {"
    12. "background-color: rgb(224, 0, 0);}");
    To copy to clipboard, switch view to plain text mode 

    That's what i have implemented ... (For someone looking for a way to go)

    Please note : setFixedSize(150,100); and "border-radius: 75px 50px;"

    Regards.

Similar Threads

  1. Connect Button to a custom slot
    By Majestade in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2007, 18:17
  2. custom maximize button---
    By Naveen in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 14:11

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.