#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QPushButton>
#include <QGridLayout>
#include <QApplication>
class DrawingWidget
: public QWidget{
public:
protected:
void paintEvent
(QPaintEvent *event
) // <<<<<<<<<<<<< Do drawing here on this widget (which does not has layout) {
painter.
setRenderHint(QPainter::Antialiasing,
true);
painter.setPen(Qt::blue);
QSize size
= event
->rect
().
size();
// <<<<<<<<<< always do painting in side the event->rect()
QPointF(size.
width(), size.
height()),
};
painter.drawConvexPolygon(points, 4);
}
};
{
Q_OBJECT
public:
, layout
(new QGridLayout(this)) // This will be your main layout, you could change this to a combination on VBox/Hbox, I used grid for its flexibility to act as both {
layout->addWidget(new DrawingWidget, 0, 0, 1, 2); // <<<<<<<<<<<<< Drawing Widget
layout
->addWidget
(new QPushButton("< Prev"),
1,
0,
1,
1);
layout
->addWidget
(new QPushButton("Next >"),
1,
1,
1,
1);
}
private:
};
int main(int argc, char *argv[])
{
Widget w;
w.show();
return app.exec();
}
#include "main.moc"
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QPushButton>
#include <QGridLayout>
#include <QApplication>
class DrawingWidget : public QWidget
{
public:
DrawingWidget(QWidget *parent = 0) : QWidget(parent) {}
protected:
void paintEvent(QPaintEvent *event) // <<<<<<<<<<<<< Do drawing here on this widget (which does not has layout)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(Qt::blue);
QSize size = event->rect().size(); // <<<<<<<<<< always do painting in side the event->rect()
QPointF points[4] = {
QPointF(0.0, 0.0),
QPointF(size.width(), 0.0),
QPointF(size.width(), size.height()),
QPointF(0.0, size.height())
};
painter.drawConvexPolygon(points, 4);
}
};
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0)
: QWidget(parent)
, layout(new QGridLayout(this)) // This will be your main layout, you could change this to a combination on VBox/Hbox, I used grid for its flexibility to act as both
{
layout->addWidget(new DrawingWidget, 0, 0, 1, 2); // <<<<<<<<<<<<< Drawing Widget
layout->addWidget(new QPushButton("< Prev"), 1, 0, 1, 1);
layout->addWidget(new QPushButton("Next >"), 1, 1, 1, 1);
}
private:
QGridLayout *layout;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks