Results 1 to 9 of 9

Thread: Round QPushButton

  1. #1
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Round QPushButton

    I have attempted to create a round QPushButton using Styles, however it is still coming out rectangular. Could someone point out where I am going wrong please?

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class RoundPushbutton;
    7.  
    8. class MainWindow : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit MainWindow(QWidget *parent = 0);
    14.  
    15. private:
    16. RoundPushbutton *roundButton;
    17. };
    18.  
    19. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "roundpushbutton.h"
    3.  
    4. #include <QVBoxLayout>
    5. #include <QPushButton>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    8. {
    9. QWidget *window = new QWidget(this);
    10. QVBoxLayout *vlay = new QVBoxLayout(window);
    11. QPushButton *btn1 = new QPushButton("1");
    12. vlay->addWidget(btn1);
    13. QPushButton *btn2 = new QPushButton("2");
    14. vlay->addWidget(btn2);
    15. RoundPushbutton *roundButton = new RoundPushbutton();
    16. vlay->addWidget(roundButton);
    17. window->setLayout(vlay);
    18. setCentralWidget(window);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef ROUNDPUSHBUTTON_H
    2. #define ROUNDPUSHBUTTON_H
    3.  
    4. #include <QPushButton>
    5.  
    6. class RoundPushbutton : public QPushButton
    7. {
    8. public:
    9. explicit RoundPushbutton(QWidget *parent = 0);
    10. };
    11.  
    12. #endif // ROUNDPUSHBUTTON_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "roundpushbutton.h"
    2.  
    3. #include <QPushButton>
    4.  
    5. RoundPushbutton::RoundPushbutton(QWidget *parent)
    6. : QPushButton(parent)
    7. {
    8. QPushButton *button = new QPushButton();
    9. //button->setGeometry(QRect(100, 100, 300, 300));
    10. button->setText("Quit");
    11. button->setFlat(true);
    12. button->setAttribute(Qt::WA_TranslucentBackground);
    13. button->setStyleSheet(
    14. "background-color: red;"
    15. "border: 1px solid black;" //outline
    16. "border-radius: 150px;" //corners
    17. "color: lightGray; " //text
    18. "font-size: 35px;"
    19. );
    20. }
    To copy to clipboard, switch view to plain text mode 

    Instead of 2 normal QPushbuttons followed by a round one, they all look normal.
    Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Round QPushButton

    You create a button inside a button?

    Maybe you wanted to apply the stylesheet to the button itself?

    Cheers,
    _

  3. #3
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Round QPushButton

    Thanks for replying.
    Where exactly am I creating the button within a button?
    I'm creating a widget that contains 3 buttons, but the 3rd button I'm not using a standard QPushButton, I modified the standard one in the RoundPushbutton class, and this is the one that I'm trying to insert as the 3rd pushbutton.
    I'm obviously confusing things and doing something wrong, so can you explain what it is?

  4. #4
    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: Round QPushButton

    Quote Originally Posted by ShamusVW View Post
    Where exactly am I creating the button within a button?
    In line #8 of the last snippet.
    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.


  5. #5
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Round QPushButton

    If I remove that line then, and change the remaining code from e.g. button->setText(... to setText(... I then have the following...

    Qt Code:
    1. RoundPushbutton::RoundPushbutton(QWidget *parent)
    2. : QPushButton(parent)
    3. {
    4. //QPushButton *button = new QPushButton();
    5. setGeometry(QRect(100, 100, 300, 300));
    6. setText("Quit");
    7. setFlat(true);
    8. setAttribute(Qt::WA_TranslucentBackground);
    9. setStyleSheet(
    10. "background-color: red;"
    11. "border: 1px solid black;" //outline
    12. "border-radius: 150px;" //corners
    13. "color: lightGray; " //text
    14. "font-size: 35px;"
    15. );
    16. }
    To copy to clipboard, switch view to plain text mode 

    however I then get the following error

    undefined reference to 'vtable for RoundPushbutton
    pointing to line 2 of the above code


    Added after 7 minutes:


    My mistake, I had added a destructor to the header file and not the c++ file.

    Running the program, I have a little bit of success now. It now shows the 3 buttons, the 3rd being the new "RoundPushbutton", however it is still not round...?
    It is rectangular, same length as the first 2 but wider. It is coloured red, and has "Quit" written on it, but no rounded corners.


    Added after 25 minutes:


    As an update for others, I got this to work.
    It seems that my border radius was too big. Setting it to 30 with a font size of 50 works. Border radius to 31 goes back to square corners. I would think 25 (being half of 50 font size) is the limiting factor, but I can only think that the pushbutton comes out at 60 in height, hence 30 for the radius.
    I'm not sure why setGeometry is being ignored though. Maybe it gets overridden by the layout chosen.
    Last edited by ShamusVW; 13th April 2016 at 10:19.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Round QPushButton

    Quote Originally Posted by ShamusVW View Post
    I'm not sure why setGeometry is being ignored though. Maybe it gets overridden by the layout chosen.
    Yes it does.
    If you want a fixed size, set a fixed size.

    Cheers,
    _

  7. #7
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Round QPushButton

    But I am. Doesn't matter what I use in setGeometry(x, y, w, h), it still stays the same size.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Round QPushButton

    Quote Originally Posted by ShamusVW View Post
    But I am.
    Not in the code you've posted.

    Quote Originally Posted by ShamusVW View Post
    Doesn't matter what I use in setGeometry(x, y, w, h), it still stays the same size.
    Quote Originally Posted by ShamusVW View Post
    Maybe it gets overridden by the layout chosen.
    Cheers,
    _

  9. #9
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Round QPushButton

    Ah, got it to work, thanks.
    setFixedSize(int w, int h); does it for me.
    Thanks for all the help.

Similar Threads

  1. Round Icon shaped QPushButton Click event
    By kcsomisetty in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 16th June 2010, 06:52
  2. Replies: 0
    Last Post: 22nd February 2010, 09:30
  3. QPushButton -- Round shape
    By kishore7771 in forum Qt Programming
    Replies: 6
    Last Post: 15th February 2010, 12:40
  4. Round QSlider
    By Havard in forum Qt Programming
    Replies: 5
    Last Post: 25th June 2007, 20:14
  5. Replies: 3
    Last Post: 26th September 2006, 12:16

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.