Last active
February 25, 2016 14:36
-
-
Save mitchcurtis/233f78d42d62e654f773 to your computer and use it in GitHub Desktop.
Custom QWidget with paintEvent implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtWidgets> | |
class Widget : public QWidget | |
{ | |
public: | |
Widget(QWidget *parent = 0) : | |
QWidget(parent) | |
{ | |
resize(300, 300); | |
} | |
protected: | |
void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE | |
{ | |
QPainter painter(this); | |
QPen pen(Qt::red); | |
pen.setWidth(10); | |
painter.setPen(pen); | |
const qreal halfPenWidth = pen.width() / 2; | |
painter.drawRect(QRectF(halfPenWidth, halfPenWidth, | |
width() - halfPenWidth * 2, height() - halfPenWidth * 2)); | |
} | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
QApplication a(argc, argv); | |
Widget w; | |
w.show(); | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment