how to create a circular button in qt designer

  • Last Update :
  • Techknowledgy :

Add a QPushButton in Qt Desgner and set its width and height to 40. Then right-click the button, select "Change stylesheet...", and paste in the following stylesheet:

QPushButton {
   color: #333;
    border: 2px solid # 555;
   border - radius: 20 px;
   border - style: outset;
   background: qradialgradient(
      cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4,
      radius: 1.35, stop: 0 #fff, stop: 1 #888
        );
    padding: 5px;
    }

QPushButton:hover {
    background: qradialgradient(
        cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4,
        radius: 1.35, stop: 0 # fff, stop: 1 #bbb
   );
}

QPushButton: pressed {
   border - style: inset;
   background: qradialgradient(
      cx: 0.4, cy: -0.1, fx: 0.4, fy: -0.1,
      radius: 1.35, stop: 0 #fff, stop: 1 #ddd
   );
}

Suggestion : 2

VRonin last edited by VRonin @J.Hilk @J-Hilk said in Creating circular QPushbutton: an other approach Using this approach clicking a point inside CircularButton::rect but outside the pained circle would still trigger the click so you need to combine this with the setMask implementation Edit: code fixed Reply Quote 2 1 Reply Last reply ,@VRonin: Thanks a lot for your help. But I can see some lines in the button and it is not completely circular As we can see four lines at the corner of the button, Kira last edited by @VRonin @VRonin: Thanks a lot for your help. But I can see some lines in the button and it is not completely circular As we can see four lines at the corner of the button Reply Quote 0 1 Reply Last reply ,Using this approach clicking a point inside CircularButton::rect but outside the pained circle would still trigger the click so you need to combine this with the setMask implementation

Hello All,
Qt pushbutton is rectangular in shape by default. I am using QCommonstyle to provide an arrow button to pushbutton which is circular in shape. My problem is that when I try to assign an icon to the button it does not turn to exact size of the icon ie. I want my push button to turn to the size of the icon

QCommonStyle style;
_ui - > Forward - > setIconSize({
   _ui - > btnXForward - > width(),
   _ui - > btnXForward - > height()
});
_ui - > Forward - > setIcon(style.standardIcon(QStyle::SP_ArrowForward));

use this in button style sheet

QPushButton {
   color: #333;
border: 2px solid # 555;
   border - radius: 20 px;
   border - style: outset;
   background: qradialgradient(
      cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4,
      radius: 1.35, stop: 0 #fff, stop: 1 #888
);
padding: 5px;
}

QPushButton:hover {
background: qradialgradient(
cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4,
radius: 1.35, stop: 0 # fff, stop: 1 #bbb
   );
}

You can use setMask. Something like:

class RoundButton: public QPushButton {
   Q_DISABLE_COPY(RoundButton)
   public:
      using QPushButton::QPushButton;
   protected:
      void resizeEvent(QResizeEvent * event) override {
         QPushButton::resizeEvent(event);
         const QRect buttonRect = rect();
         setMask(QRegion(buttonRect.x(), buttonRect.y(), buttonRect.width(), buttonRect.height(), QRegion::Ellipse));
      }
};

an other approach:

#ifndef CIRCULARBUTTON_H
#define CIRCULARBUTTON_H

#include <QPushButton>

class CircularButton : public QPushButton
{
    Q_OBJECT
public:
    explicit CircularButton(QWidget *parent = nullptr);

signals:

public slots:

protected:
    virtual void paintEvent(QPaintEvent *) override;
    virtual void resizeEvent(QResizeEvent *)override;
};

#endif // CIRCULARBUTTON_H

#include "circularbutton.h"

#include <QPainter>

CircularButton::CircularButton(QWidget *parent) : QPushButton(parent)
{

}

void CircularButton::paintEvent(QPaintEvent *)
{
    //Do not paint base implementation -> no styles are applied

    QColor background = isDown() ? QColor("grey") : QColor("lightgrey");
    int diameter = qMin(height(), width());

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, false);
    painter.translate(width() / 2, height() / 2);

    painter.setPen(QPen(QColor("black"), 2));
    painter.setBrush(QBrush(background));
    painter.drawEllipse(QRect(-diameter / 2, -diameter / 2, diameter, diameter));

}

void CircularButton::resizeEvent(QResizeEvent *e)
{
    QPushButton::resizeEvent(e);
    int diameter = qMin(height(), width())+4 ;
    int xOff =(width() -diameter ) / 2;
    int yOff =(height() - diameter) / 2;
    setMask(QRegion(xOff,yOff, diameter, diameter,QRegion::Ellipse));
}

Suggestion : 3

I’m trying to create a round button in Qt. A simple form with a single button QPushButton was created in designer. I’m attempting to turn this into a round button using setMask(). As soon as setMask() is applied the button disappeares. Does a custom widget need to be created to make a round button?,Note: If the button is created in code and applied to a window before the window is displayed, the button is displayed. I would like to use the WYSIWIG capabilities of the Qt Designer rather than creating the entire form in code.,Note, that you have to create complete style for your button, as standard style will not be applicable.,QWidget::setMask “causes only the parts of the widget which overlap region to be visible. If the region includes pixels outside the rect() of the widget, window system controls in that area may or may not be visible, depending on the platform“.

I’m trying to create a round button in Qt. A simple form with a single button QPushButton was created in designer. I’m attempting to turn this into a round button using setMask(). As soon as setMask() is applied the button disappeares. Does a custom widget need to be created to make a round button?

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QtGui/QPushButton>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->pushButton->setText("Test Text");
    ui->pushButton->setFixedHeight(200);
    ui->pushButton->setFixedWidth(200);

    //Set Starting point of region 5 pixels inside , make region width & height
    //values same and less than button size so that we obtain a pure-round shape

    QRegion* region = new QRegion(*(new QRect(ui->pushButton->x()+5,ui->pushButton->y()+5,190,190)),QRegion::Ellipse);
    ui->pushButton->setMask(*region);
    ui->pushButton->show();


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QMessageBox msgbox;
    msgbox.setText("Text was set");
    msgbox.show();

}

Try this code instead and you’ll see:

MainWindow::MainWindow(QWidget * parent):
   QMainWindow(parent),
   ui(new Ui::MainWindow) {
      ui - > setupUi(this);
      ui - > pushButton - > setText("Test Text");
      ui - > pushButton - > setFixedHeight(200);
      ui - > pushButton - > setFixedWidth(200);
      QRect rect(0, 0, 190, 190);
      qDebug() << rect.size();
      qDebug() << ui - > pushButton - > size();
      QRegion region(rect, QRegion::Ellipse);
      qDebug() << region.boundingRect().size();
      ui - > pushButton - > setMask(region);
   }

Like this:

 background - color: white;
 border - style: solid;
 border - width: 1 px;
 border - radius: 50 px;
 border - color: red;
 max - width: 100 px;
 max - height: 100 px;
 min - width: 100 px;
 min - height: 100 px;
QPushButton {
   color: #333;
border: 2px solid # 555;
   border - radius: 20 px;
   border - style: outset;
   background: qradialgradient(
      cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4,
      radius: 1.35, stop: 0 #fff, stop: 1 #888
);
padding: 5px;
}

QPushButton:hover {
background: qradialgradient(
cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4,
radius: 1.35, stop: 0 # fff, stop: 1 #bbb
   );
}