Problems with laying out custom widget
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:
Code:
#ifndef MTR_Button_H
#define MTR_Button_H
#include <QAbstractButton>
namespace MTR {
Q_OBJECT
public:
protected:
QSize minimumSizeHint
() const;
private:
bool hovered;
};
}
#endif
its implementation
MTRButton.cpp
Code:
#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();
}
}
Five such objects are placed in a QVBoxLayout on MyWidget
MyWidget.h
Code:
#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
MyWidget.cpp
Code:
#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);
}
and finally main.cpp
Code:
#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();
}
Re: Problems with laying out custom widget
Don't allocate the layout on the stack. It get's deleted as soon the constructor exists.
Do it on the heap( i.e. QVBoxLayout * qvb = new QVBoxLayout(this));
Regards
Re: Problems with laying out custom widget
"QVBoxLayout qvb" gets automatically destructed while going out of scope by the time MyWidge constructor ends.
Try something like this:
Code:
qvb->addWidget(b1);
...
setLayout(qvb);
Edit: D*mn you Marcel! ;)
Re: Problems with laying out custom widget
Quote:
Originally Posted by
jpn
[/code]Edit: D*mn you Marcel! ;)
:) Don't worry. These days I have time to answer only a few posts/day( not even those ), so the rest of them are yours.
Regards
Re: Problems with laying out custom widget
Bloody hell!
It is obvious!
:o
A short break from coding and such a stupid mistake.
Thanks
Re: Problems with laying out custom widget
Quote:
Originally Posted by
marcel
These days I have time to answer only a few posts/day( not even those ), so the rest of them are yours.
Hell, they are! I want my share too!
Re: Problems with laying out custom widget
Quote:
Hell, they are! I want my share too!
I meant working days, not weekend :).
Regards