3

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);
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Possible duplicate of [Change rectangular Qt button to round](https://stackoverflow.com/questions/12734319/change-rectangular-qt-button-to-round) – Mailerdaimon Jun 14 '18 at 06:53
  • its actually the test of this link, but its not working – Madiha Harifi Jun 14 '18 at 06:56
  • Alright, I retracted the Vote. Please add this info to the question prevent further close votes :). – Mailerdaimon Jun 14 '18 at 06:58
  • 1
    thanks you can check its already done , I edited my post :) – Madiha Harifi Jun 14 '18 at 06:59
  • Do you absolutely need to use masks? Using the second answer (stylesheets) from the link would be way easier if you are just going for round buttons. – Mailerdaimon Jun 14 '18 at 07:00
  • I'm actually trying the two methods , the stylesheets one , is the next that I'll try to see the result of it , this one seems to be good too , but it need some math calculation to find the right size , and that is not easy to do , from the first try – Madiha Harifi Jun 14 '18 at 07:04

1 Answers1

2

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);
}

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241