PDA

View Full Version : Round QPushButton



ShamusVW
13th April 2016, 08:33
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?


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class RoundPushbutton;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);

private:
RoundPushbutton *roundButton;
};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "roundpushbutton.h"

#include <QVBoxLayout>
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
QWidget *window = new QWidget(this);
QVBoxLayout *vlay = new QVBoxLayout(window);
QPushButton *btn1 = new QPushButton("1");
vlay->addWidget(btn1);
QPushButton *btn2 = new QPushButton("2");
vlay->addWidget(btn2);
RoundPushbutton *roundButton = new RoundPushbutton();
vlay->addWidget(roundButton);
window->setLayout(vlay);
setCentralWidget(window);
}


#ifndef ROUNDPUSHBUTTON_H
#define ROUNDPUSHBUTTON_H

#include <QPushButton>

class RoundPushbutton : public QPushButton
{
public:
explicit RoundPushbutton(QWidget *parent = 0);
};

#endif // ROUNDPUSHBUTTON_H


#include "roundpushbutton.h"

#include <QPushButton>

RoundPushbutton::RoundPushbutton(QWidget *parent)
: QPushButton(parent)
{
QPushButton *button = new QPushButton();
//button->setGeometry(QRect(100, 100, 300, 300));
button->setText("Quit");
button->setFlat(true);
button->setAttribute(Qt::WA_TranslucentBackground);
button->setStyleSheet(
"background-color: red;"
"border: 1px solid black;" //outline
"border-radius: 150px;" //corners
"color: lightGray; " //text
"font-size: 35px;"
);
}


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

anda_skoa
13th April 2016, 09:05
You create a button inside a button?

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

Cheers,
_

ShamusVW
13th April 2016, 09:31
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?

wysota
13th April 2016, 09:33
Where exactly am I creating the button within a button?
In line #8 of the last snippet.

ShamusVW
13th April 2016, 10:19
If I remove that line then, and change the remaining code from e.g. button->setText(... to setText(... I then have the following...


RoundPushbutton::RoundPushbutton(QWidget *parent)
: QPushButton(parent)
{
//QPushButton *button = new QPushButton();
setGeometry(QRect(100, 100, 300, 300));
setText("Quit");
setFlat(true);
setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet(
"background-color: red;"
"border: 1px solid black;" //outline
"border-radius: 150px;" //corners
"color: lightGray; " //text
"font-size: 35px;"
);
}

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.

anda_skoa
13th April 2016, 10:29
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,
_

ShamusVW
13th April 2016, 10:33
But I am. Doesn't matter what I use in setGeometry(x, y, w, h), it still stays the same size.

anda_skoa
13th April 2016, 13:34
But I am.

Not in the code you've posted.



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



Maybe it gets overridden by the layout chosen.


Cheers,
_

ShamusVW
13th April 2016, 14:03
Ah, got it to work, thanks.
setFixedSize(int w, int h); does it for me.
Thanks for all the help.