When I place five objects of MTR::Button class on MyWidget object (simple testing area) using QVBoxLayout I receive five buttons on one pile!
How to achieve an ordinary vertical box layout effect in this case?
MTRButton.h:
#ifndef MTR_Button_H
#define MTR_Button_H
#include <QAbstractButton>
namespace MTR {
Q_OBJECT
public:
protected:
QSize minimumSizeHint
() const;
private:
bool hovered;
};
}
#endif
#ifndef MTR_Button_H
#define MTR_Button_H
#include <QAbstractButton>
class QSize;
class QPaintEvent;
namespace MTR {
class Button : public QAbstractButton {
Q_OBJECT
public:
Button(QWidget *parent = 0);
Button(QString tekst, QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
QSize sizeHint() const;
QSize minimumSizeHint() const;
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
bool hovered;
QFont font;
};
}
#endif
To copy to clipboard, switch view to plain text mode
its implementation
MTRButton.cpp
#include "MTRButton.h"
#include <QtGlobal>
#include <QtCore>
#include <QtGui>
namespace MTR {
painter.
setRenderHint(QPainter::Antialiasing,
true);
painter.setPen(borderPen);
QBrush ribbonBrush
(palette
().
dark());
painter.setBrush(ribbonBrush);
painter.drawRoundRect(5,5,this->width() -10, this->height() - 10,25,25);
QBrush insideBrush
(palette
().
mid());
if(isDown()) insideBrush = palette().light();
painter.setBrush(insideBrush);
painter.drawRoundRect(10,10,this->width() -20, this->height() - 20,25,25);
if((this->height()-20)/2 >= 0) {
QRadialGradient gradRed
(QPointF(15+(this
->height
()-20)/2/4,
15+(this
->height
()-20)/2/3),
(this
->height
()-20)/2);
if(!hovered) {
gradRed.setColorAt(0,Qt::white);
gradRed.setColorAt(1,Qt::red);
} else {
gradRed.setColorAt(0,Qt::white);
gradRed.setColorAt(1,Qt::black);
}
painter.setBrush(circBrush);
painter.drawEllipse(15,15,(this->height()-20)/2,(this->height()-20)/2);
QRadialGradient gradGreen
(QPointF(15+(this
->height
()-20)/2 + 2 +(this
->height
()-20)/2/4,
15+(this
->height
()-20)/2/3),
(this
->height
()-20)/2);
if(hovered || isDown()) {
gradGreen.setColorAt(0,Qt::white);
gradGreen.setColorAt(1,Qt::green);
} else {
gradGreen.setColorAt(0,Qt::white);
gradGreen.setColorAt(1,Qt::black);
}
circBrush
= QBrush(gradGreen
);
painter.setBrush(circBrush);
painter.drawEllipse(15+(this->height()-20)/2 + 2,15,(this->height()-20)/2,(this->height()-20)/2);
}
painter.setPen(borderPen);
font
= QFont(tr
("Arial"),
10,
50,
false);
painter.setFont(font);
QRect textRect
(this
->height
(),
5 + this
->height
()/6, this
->width
() - this
->height
() - 20, this
->height
()-20);
painter.drawText(textRect,Qt::AlignTop | Qt::AlignHCenter, this->text());
}
QSize Button
::minimumSizeHint() const { }
QSize Button
::sizeHint() const { int tekstSzer = fm.width(this->text());
return QSize(tekstSzer
+ 28 + 40,
40);
}
QRect rect
(10,
10,this
->width
()-20,this
->height
()-20);
if(rect.contains(event->pos())) {
if(hovered) return;
else { hovered = true; update(); }
}
else {
if(!hovered) return;
else { hovered = false; update(); }
}
}
void Button
::mousePressEvent(QMouseEvent * /* event */) { setDown(true);
}
setDown(false);
QRect rect
(10,
10,this
->width
()-20,this
->height
()-20);
if(rect.contains(event->pos())) emit clicked();
}
}
#include "MTRButton.h"
#include <QtGlobal>
#include <QtCore>
#include <QtGui>
namespace MTR {
Button::Button(QWidget *parent) : QAbstractButton(parent) { setMouseTracking(true); hovered = false; }
Button::Button(QString tekst,QWidget *parent) : QAbstractButton(parent) { setMouseTracking(true); hovered = false; this->setText(tekst); }
void Button::paintEvent(QPaintEvent * /* event */) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QPen borderPen(QColor(Qt::black),1);
painter.setPen(borderPen);
QBrush ribbonBrush(palette().dark());
painter.setBrush(ribbonBrush);
painter.drawRoundRect(5,5,this->width() -10, this->height() - 10,25,25);
QBrush insideBrush(palette().mid());
if(isDown()) insideBrush = palette().light();
painter.setBrush(insideBrush);
painter.drawRoundRect(10,10,this->width() -20, this->height() - 20,25,25);
if((this->height()-20)/2 >= 0) {
QRadialGradient gradRed(QPointF(15+(this->height()-20)/2/4, 15+(this->height()-20)/2/3), (this->height()-20)/2);
if(!hovered) {
gradRed.setColorAt(0,Qt::white);
gradRed.setColorAt(1,Qt::red);
} else {
gradRed.setColorAt(0,Qt::white);
gradRed.setColorAt(1,Qt::black);
}
QBrush circBrush(gradRed);
painter.setBrush(circBrush);
painter.drawEllipse(15,15,(this->height()-20)/2,(this->height()-20)/2);
QRadialGradient gradGreen(QPointF(15+(this->height()-20)/2 + 2 +(this->height()-20)/2/4, 15+(this->height()-20)/2/3), (this->height()-20)/2);
if(hovered || isDown()) {
gradGreen.setColorAt(0,Qt::white);
gradGreen.setColorAt(1,Qt::green);
} else {
gradGreen.setColorAt(0,Qt::white);
gradGreen.setColorAt(1,Qt::black);
}
circBrush = QBrush(gradGreen);
painter.setBrush(circBrush);
painter.drawEllipse(15+(this->height()-20)/2 + 2,15,(this->height()-20)/2,(this->height()-20)/2);
}
painter.setPen(borderPen);
font = QFont(tr("Arial"),10,50,false);
painter.setFont(font);
QRect textRect(this->height(), 5 + this->height()/6, this->width() - this->height() - 20, this->height()-20);
painter.drawText(textRect,Qt::AlignTop | Qt::AlignHCenter, this->text());
}
QSize Button::minimumSizeHint() const {
return QSize(200,40);
}
QSize Button::sizeHint() const {
QFontMetrics fm(font);
int tekstSzer = fm.width(this->text());
return QSize(tekstSzer + 28 + 40,40);
}
void Button::mouseMoveEvent(QMouseEvent *event) {
QRect rect(10,10,this->width()-20,this->height()-20);
if(rect.contains(event->pos())) {
if(hovered) return;
else { hovered = true; update(); }
}
else {
if(!hovered) return;
else { hovered = false; update(); }
}
}
void Button::mousePressEvent(QMouseEvent * /* event */) {
setDown(true);
}
void Button::mouseReleaseEvent(QMouseEvent *event) {
setDown(false);
QRect rect(10,10,this->width()-20,this->height()-20);
if(rect.contains(event->pos())) emit clicked();
}
}
To copy to clipboard, switch view to plain text mode
Five such objects are placed in a QVBoxLayout on MyWidget
MyWidget.h
#ifndef TESTWIDGET
#define TESTWIDGET
#include <QWidget>
namespace MTR {
class Button;
}
Q_OBJECT
public:
public slots:
void TestujPrzycisk() { qDebug("Click!"); }
private:
MTR::Button *b1;
MTR::Button *b2;
MTR::Button *b3;
MTR::Button *b4;
MTR::Button *b5;
};
#endif
#ifndef TESTWIDGET
#define TESTWIDGET
#include <QWidget>
namespace MTR {
class Button;
}
class MyWidget : public QWidget {
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
public slots:
void TestujPrzycisk() { qDebug("Click!"); }
private:
MTR::Button *b1;
MTR::Button *b2;
MTR::Button *b3;
MTR::Button *b4;
MTR::Button *b5;
};
#endif
To copy to clipboard, switch view to plain text mode
MyWidget.cpp
#include "MyWidget.h"
#include "MTRButton.h"
#include <QtCore>
#include <QtGui>
b1
= new MTR
::Button(QString::fromUtf8("Rozpocznij grę"),
this);
b2
= new MTR
::Button(QString::fromUtf8("Wczytaj grę"),
this);
b3
= new MTR
::Button(QString::fromUtf8("Wyniki"),
this);
b4
= new MTR
::Button(QString::fromUtf8("Zasady"),
this);
b5
= new MTR
::Button(QString::fromUtf8("Wyjdź"),
this);
qvb.addWidget(b1);
qvb.addWidget(b2);
qvb.addWidget(b3);
qvb.addWidget(b4);
qvb.addWidget(b5);
setLayout(&qvb);
}
#include "MyWidget.h"
#include "MTRButton.h"
#include <QtCore>
#include <QtGui>
MyWidget::MyWidget(QWidget *parent) : QWidget(parent) {
b1 = new MTR::Button(QString::fromUtf8("Rozpocznij grę"),this);
b2 = new MTR::Button(QString::fromUtf8("Wczytaj grę"),this);
b3 = new MTR::Button(QString::fromUtf8("Wyniki"),this);
b4 = new MTR::Button(QString::fromUtf8("Zasady"),this);
b5 = new MTR::Button(QString::fromUtf8("Wyjdź"),this);
QVBoxLayout qvb;
qvb.addWidget(b1);
qvb.addWidget(b2);
qvb.addWidget(b3);
qvb.addWidget(b4);
qvb.addWidget(b5);
setLayout(&qvb);
}
To copy to clipboard, switch view to plain text mode
and finally main.cpp
#include <QApplication>
#include <QtCore>
#include <QtGui>
#include "MTRButton.h"
#include "MyWidget.h"
int main(int argc, char * argv[]) {
MyWidget widget;
widget.setPalette(orangePalette);
widget.show();
return app.exec();
}
#include <QApplication>
#include <QtCore>
#include <QtGui>
#include "MTRButton.h"
#include "MyWidget.h"
int main(int argc, char * argv[]) {
QApplication app(argc, argv);
MyWidget widget;
QPalette orangePalette;
orangePalette.setColorGroup(QPalette::Active,QBrush(Qt::black),QBrush(QColor(255,180,51)),
QBrush(QColor(255,212,71)),QBrush(QColor(70,100,105)),QBrush(QColor(255,118,54)),
QBrush(QColor(Qt::black)),QBrush(QColor(Qt::gray)),QBrush(QColor(Qt::black)),QBrush(QColor(255,180,51)));
orangePalette.setColorGroup(QPalette::Inactive,QBrush(Qt::black),QBrush(QColor(255,180,51)),
QBrush(QColor(255,212,71)),QBrush(QColor(70,100,105)),QBrush(QColor(255,118,54)),
QBrush(QColor(Qt::black)),QBrush(QColor(Qt::gray)),QBrush(QColor(Qt::black)),QBrush(QColor(255,180,51)));
widget.setPalette(orangePalette);
widget.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks