1
我正在Qt中製作一個自定義小部件,並將圖像作爲其背景。背景圖像對於小部件的所有實例都是完全相同的。我想知道我是否以適當的方式做到這一點。Qt:正確存儲自定義小部件的圖像資源
下面是我現在做:
// 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)
{
imgBackground= new QImage();
imgBackground->load(":/Images/background.png");
}
}
void QMyWidget::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
painter.drawImage(QPoint(), *imgBackground);
}
代碼工作得很好,但是這被認爲是做一個好辦法嗎?
那麼,我使用資源系統來嵌入它們。但我不應該加載和存儲它們嗎?或者在每次繪畫事件中只做QImage(「:/ resource/path.png」)就可以了嗎? – tacospice
你可以在繪畫事件中設置它,或者你可以使用樣式表來實現它。 –