I'm trying a code to make a QPushButton
looking round , this code seems to be working, but after execution , it show me only the half of the ellipse, can anybody help me to figure out why is show only the half of it, its about the m_bouton1
?
I did before checked this link Change rectangular Qt button to round
But its not working , it appears only the half of it.
#include <QtWidgets>
#include "MyFenetre.h"
#include "MyWindow.h"
MyFenetre::MyFenetre() : QWidget()
{
setFixedSize(300, 150);
m_bouton = new QPushButton("Salut", this);
m_bouton->setFont(QFont("Comic Sans MS", 14));
m_bouton->move(110, 50);
m_bouton1=new QPushButton("Boutton RounD (*)");
m_bouton1->setFixedHeight(200);
m_bouton1->setFixedWidth(200);
QRect *rect = new QRect(0,0,190,190);
qDebug() << rect->size();
qDebug() << m_bouton1->size();
QRegion *region = new QRegion(*rect,QRegion::Ellipse);
qDebug() << region->boundingRect().size();
m_bouton1->setMask(*region);
QVBoxLayout *login_form= new QVBoxLayout;
login_form->addWidget(m_bouton);
login_form->addWidget(m_bouton1);
setLayout(login_form);
setWindowTitle("Button test");
//setWindowIcon(QIcon("icone.png"));
// Connexion du clic du bouton à la fermeture de l'application
QObject::connect(m_bouton, SIGNAL(clicked()), this, SLOT(changerFen()));
QObject::connect(m_bouton1, SIGNAL(clicked()), this, SLOT(changerFen()));
}
void MyFenetre::changerFen()
{
int f = 1;
emit askDisplayFen(f);
}
The main problem is that the size of the window is too small with respect to the sum of the sizes of both windows, so if you are going to establish a fixed size you should calculate it correctly, in this case, do it after adding the buttons.
Also I will improve the code since you are abusing the dynamic memory, for example QRect
is not necessary to create a pointer since only one copy is needed, the same for QRegion
.
myfenetre.h
#ifndef MYFENETRE_H
#define MYFENETRE_H
#include <QWidget>
class QPushButton;
class MyFenetre : public QWidget
{
Q_OBJECT
public:
MyFenetre(QWidget *parent = 0);
~MyFenetre();
signals:
void askDisplayFen(float f);
private slots:
void changerFen();
private:
QPushButton *m_bouton;
QPushButton *m_bouton1;
};
#endif // MYFENETRE_H
myfenetre.cpp
#include "myfenetre.h"
#include <QPushButton>
#include <QVBoxLayout>
MyFenetre::MyFenetre(QWidget *parent)
: QWidget(parent)
{
m_bouton = new QPushButton("Salut");
m_bouton->setFont(QFont("Comic Sans MS", 14));
m_bouton1 = new QPushButton("Boutton RounD (*)");
m_bouton1->setFixedSize(200, 200);
QRect rect(QPoint(), m_bouton1->size());
rect.adjust(10, 10, -10, -10);
QRegion region(rect,QRegion::Ellipse);
m_bouton1->setMask(region);
QVBoxLayout *login_form= new QVBoxLayout(this);
login_form->addWidget(m_bouton);
login_form->addWidget(m_bouton1);
setLayout(login_form);
setWindowTitle("Button test");
setFixedSize(300, minimumHeight());
// Connexion du clic du bouton à la fermeture de l'application
connect(m_bouton, &QPushButton::clicked, this, &MyFenetre::changerFen);
connect(m_bouton1, &QPushButton::clicked, this, &MyFenetre::changerFen);
}
MyFenetre::~MyFenetre()
{
}
void MyFenetre::changerFen()
{
int f = 1;
emit askDisplayFen(f);
}