Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: shape of push button

  1. #1
    Join Date
    Feb 2006
    Posts
    42
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post shape of push button

    How to set the shape of push button /tool button. ? say to oval or round shape

    Thanks in advance

  2. #2
    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: shape of push button

    Subclass and reimplement the paint event.

  3. #3
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: shape of push button

    Another way is to write a new QStyle that styles only pushbuttons, and set set that style for the pushbuttons in question.

  4. #4
    Join Date
    May 2006
    Location
    Australia
    Posts
    53
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: shape of push button

    this is the way that I chose to subclass my buttons to get them to look like little green round "electrodes" in an electrode array

    Qt Code:
    1. void ElectrodeButton::paintEvent(QPaintEvent* event)
    2. {
    3. QPainter painter(this);
    4. painter.setRenderHint(QPainter::Antialiasing);
    5.  
    6. QRadialGradient grad(15, 15, 15, 0, 0);
    7.  
    8. grad.setColorAt(0.0, Qt::white);
    9. grad.setColorAt(0.1, Qt::green);
    10. grad.setColorAt(0.7, Qt::black);
    11. QBrush brush(grad);
    12. painter.setBrush(brush);
    13. painter.drawEllipse(2,2,20,20);
    14. }
    To copy to clipboard, switch view to plain text mode 

    ElectrodeButton is a subclass of QPushButton

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

    MarkoSan (21st January 2008)

  6. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: shape of push button

    Is it possible to adapt button shape dynamically according to loaded icon? What I want to achieve is when button icon is loaded, it's size and shape is reset to size and shape of overlaping icon. How do I do it?
    Qt 5.3 Opensource & Creator 3.1.2

  7. #6
    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: shape of push button

    It might be simplest to reimplement the paintEvent from the button. An alternative is to create a custom widget and provide an icon setting method that will also change the mask of the widget.

  8. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: shape of push button

    Ok, but how do I extract icon shape? For size, I can use QIcon::actualSize?
    Qt 5.3 Opensource & Creator 3.1.2

  9. #8
    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: shape of push button


  10. The following user says thank you to wysota for this useful post:

    MarkoSan (22nd January 2008)

  11. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: shape of push button

    Can you provide me with simple example, please?
    Qt 5.3 Opensource & Creator 3.1.2

  12. #10
    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: shape of push button

    An example of what? Calling createHeuristicMask?

  13. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: shape of push button

    I did not get to step where I will use Calling createHeuristicMask yet. Right now, I have a big pixmap on disk (512x512) an when I set it in QPushButton it is so tiny it can be barely seen. I would like to resize it. How do I do that? I trided rescled, does not work. I tried to put some huge text near this pixmap in button, text is shown ok, pixmap is still tiny.

    Shit, I've mixed up threads. I've posted new thread regarding how to resize icon in subclassed qpushbutton. Can you move it?
    Qt 5.3 Opensource & Creator 3.1.2

  14. #12
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: shape of push button

    Quote Originally Posted by wysota View Post
    An example of what? Calling createHeuristicMask?
    Yes, how is heuristic mask used, becuase I have no idea how to adept push button shape to loaded pixmap.
    Qt 5.3 Opensource & Creator 3.1.2

  15. #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: shape of push button

    It returns a mask which you should then pass to QWidget::setMask.

  16. #14
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: shape of push button

    Well, this is what I've done:
    Qt Code:
    1. CFlagButton::CFlagButton(QWidget* pParent, const QString flagPicture)
    2. {
    3. // sets up button font
    4. m_ButtonFont.setFamily("1942 Report"); // sets font name
    5. m_ButtonFont.setPointSize(36); // sets font size
    6. m_ButtonFont.setBold(true); // sets bold on
    7. this->setFont(m_ButtonFont); // sets font
    8. // sets button palette (background and text color)
    9. this->setAutoFillBackground(true); // sets auto fill background feature
    10. m_ButtonPalette=this->palette(); // reads current palette
    11. m_ButtonPalette.setColor(QPalette::Button, Qt::black); // sets up new palette componenet
    12. m_ButtonPalette.setColor(QPalette::ButtonText, Qt::white); // sets up new palette componenet
    13. this->setPalette(m_ButtonPalette); // sets modfied palette
    14. this->setFocusPolicy(Qt::NoFocus); // disables focus
    15. m_ButtonIcon.load(flagPicture); // creates new icon based on icon
    16. /*
    17.   m_ButtonIcon.scaled(256, 256,
    18.   Qt::KeepAspectRatioByExpanding,
    19.   Qt::SmoothTransformation); // tries to resize icon
    20. */
    21. setIcon(m_ButtonIcon); // sets icon
    22. setMask(m_ButtonIcon.createHeuristicMask()); // sets mask
    23. }
    To copy to clipboard, switch view to plain text mode 

    And the buttons are not drawn at all, neither are icons in buttons.
    Last edited by MarkoSan; 22nd January 2008 at 00:27.
    Qt 5.3 Opensource & Creator 3.1.2

  17. #15
    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: shape of push button

    Try reimplementing the paint event and draw the pixmap yourself if you can't handle this approach.

  18. #16
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: shape of push button

    Hmm, there is no need for insulting me that I am incompetent, but I am doing this thing for the first time and I am really lost.
    Qt 5.3 Opensource & Creator 3.1.2

  19. #17
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: shape of push button

    Whoa!!!

    Now it workd perfectly!!!

    Here is working code:
    Qt Code:
    1. CFlagButton::CFlagButton(QWidget* pParent, const QString flagPicture)
    2. {
    3. // sets up button font
    4. m_ButtonFont.setFamily("1942 Report"); // sets font name
    5. m_ButtonFont.setPointSize(36); // sets font size
    6. m_ButtonFont.setBold(true); // sets bold on
    7. this->setFont(m_ButtonFont); // sets font
    8. // sets button palette (background and text color)
    9. this->setAutoFillBackground(true); // sets auto fill background feature
    10. m_ButtonPalette=this->palette(); // reads current palette
    11. m_ButtonPalette.setColor(QPalette::Button, Qt::black); // sets up new palette componenet
    12. m_ButtonPalette.setColor(QPalette::ButtonText, Qt::white); // sets up new palette componenet
    13. this->setPalette(m_ButtonPalette); // sets modfied palette
    14. this->setFocusPolicy(Qt::NoFocus); // disables focus
    15. m_ButtonIcon.load(flagPicture); // creates new icon based on icon
    16. setIcon(m_ButtonIcon); // sets icon
    17. setIconSize(QSize(256, 256)); // sets icon size
    18. setMask(m_ButtonIcon.createHeuristicMask()); // sets mask
    19. }
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  20. #18
    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: shape of push button

    Quote Originally Posted by MarkoSan View Post
    Hmm, there is no need for insulting me that I am incompetent, but I am doing this thing for the first time and I am really lost.
    I didn't say anything like that. I just said that if you can't make something work, try a different approach.

  21. #19
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question Re: shape of push button

    Dear MarkoSan!
    Quote Originally Posted by MarkoSan View Post
    Whoa!!!

    Now it workd perfectly!!!

    Here is working code:
    Qt Code:
    1. CFlagButton::CFlagButton(QWidget* pParent, const QString flagPicture)
    2. {
    3. // sets up button font
    4. m_ButtonFont.setFamily("1942 Report"); // sets font name
    5. m_ButtonFont.setPointSize(36); // sets font size
    6. m_ButtonFont.setBold(true); // sets bold on
    7. this->setFont(m_ButtonFont); // sets font
    8. // sets button palette (background and text color)
    9. this->setAutoFillBackground(true); // sets auto fill background feature
    10. m_ButtonPalette=this->palette(); // reads current palette
    11. m_ButtonPalette.setColor(QPalette::Button, Qt::black); // sets up new palette componenet
    12. m_ButtonPalette.setColor(QPalette::ButtonText, Qt::white); // sets up new palette componenet
    13. this->setPalette(m_ButtonPalette); // sets modfied palette
    14. this->setFocusPolicy(Qt::NoFocus); // disables focus
    15. m_ButtonIcon.load(flagPicture); // creates new icon based on icon
    16. setIcon(m_ButtonIcon); // sets icon
    17. setIconSize(QSize(256, 256)); // sets icon size
    18. setMask(m_ButtonIcon.createHeuristicMask()); // sets mask
    19. }
    To copy to clipboard, switch view to plain text mode 
    Can I know without passing pParent into a QPushButon how a way your CFlagButton display?
    When my button is appeared on the screen some of the blackish area appeared up & down corner of the push button?
    Can you put your complete code for clear understanding or change in my code which I am attaching?
    Qt Code:
    1. #include<QtGui>
    2. class CFlagButton:QPushButton
    3. {
    4. public:
    5. CFlagButton(QWidget* parent, const QString flagPicture):QPushButton(parent)
    6. {
    7. QFont m_ButtonFont;
    8. // sets up button font
    9. //m_ButtonFont.setFamily("1942 Report"); // sets font name
    10. m_ButtonFont.setPointSize(36); // sets font size
    11. m_ButtonFont.setBold(true); // sets bold on
    12. this->setFont(m_ButtonFont); // sets font
    13. // sets button palette (background and text color)
    14. this->setAutoFillBackground(true); // sets auto fill background feature
    15. QPalette m_ButtonPalette;
    16. m_ButtonPalette=this->palette();// reads current palette
    17. m_ButtonPalette.setColor(QPalette::Button, Qt::black);//sets up new palette componenet
    18. m_ButtonPalette.setColor(QPalette::ButtonText, Qt::white);//sets up new palette componenet
    19. this->setPalette(m_ButtonPalette);//sets modfied palette
    20. this->setFocusPolicy(Qt::NoFocus);//disables focus
    21. QPixmap m_ButtonIcon;
    22. m_ButtonIcon.load(flagPicture);//creates new icon based on icon
    23. setIcon(m_ButtonIcon);//sets icon
    24. setIconSize(QSize(256, 256));// sets icon size
    25. setMask(m_ButtonIcon.createHeuristicMask());//sets mask
    26.  
    27. }
    28. };
    29.  
    30. int main (int argc, char **argv)
    31. {
    32.  
    33. QApplication app(argc,argv);
    34. CFlagButton btn(&w,"plane.jpg");
    35. w.show();
    36. return app.exec();
    37. }
    To copy to clipboard, switch view to plain text mode 
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  22. #20
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: shape of push button

    I can show the code to you next thurdsay, until then I cannot ....
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. push button fast activation
    By bhogasena in forum Qt Programming
    Replies: 1
    Last Post: 20th January 2009, 01:48
  2. Line edit and push button
    By dela in forum Newbie
    Replies: 1
    Last Post: 10th December 2008, 16:10
  3. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 22:25
  4. Push Button problem!!
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2006, 16:31
  5. Push button double click
    By curtisw in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2006, 16:40

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.