Skip to content

Instantly share code, notes, and snippets.

@mitchcurtis
Last active February 25, 2016 14:36
Show Gist options
  • Save mitchcurtis/233f78d42d62e654f773 to your computer and use it in GitHub Desktop.
Save mitchcurtis/233f78d42d62e654f773 to your computer and use it in GitHub Desktop.
Custom QWidget with paintEvent implementation
#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