I'm making a custom widget in Qt, and drawing an image as its background. The background image is to be exactly the same for all instances of widgets. I'd like to know if I'm doing it in an appropriate way.
Here's how I'm doing it now:
// QMyWidget.h
class QMyWidget : public QWidget
{
/* some stuff.. and then: */
protected:
static QImage *imgBackground;
}
// QMyWidget.cpp
QImage *QMyWidget::imgDial = NULL;
QMyWidget::QMyWidget(QWidget *parent) : QWidget(parent)
{
if(imgBackground== NULL)
{
imgBac开发者_StackOverflowkground= new QImage();
imgBackground->load(":/Images/background.png");
}
}
void QMyWidget::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
painter.drawImage(QPoint(), *imgBackground);
}
The code works just fine, but is this considered a good way to do it?
That's one way of doing it, and it's a reasonably good way if you're only dealing with a single image, but if you ever decide to scale and use a couple of custom resources then the Qt Resource System is the better way to do it. It'll save you time in code (don't need to repeatedly do QImage), and has a few other nice features like resource compression.
精彩评论