#include <QtWidgets>
class MyWindow: public QWindow
{
public:
MyWindow():
m_update_pending(false)
{
m_backingStore = new QBackingStore(this);
setTitle("QWindow Background Image");
resize(300,250);
}
{
if (event
->type
() == QEvent::UpdateRequest) {
m_update_pending = false;
renderNow();
return true;
}
return QWindow::event(event);
}
void renderLater()
{
if (!m_update_pending)
{
m_update_pending = true;
//Post this event to event queue for later processing
}
}
{
m_backingStore->resize(resizeEvent->size());
if (isExposed())
{
renderNow();
}
}
void exposeEvent(QExposeEvent *)
{
if (isExposed())
{
renderNow();
}
}
void renderNow()
{
if (!isExposed())
return;
QRect rect
(0,
0, width
(), height
());
m_backingStore->beginPaint(rect);
painter.
drawPixmap(rect,
QPixmap("background.jpg"));
painter.
drawText(QRectF(0,
0, width
(), height
()),
Qt::AlignCenter, QStringLiteral("QWindow"));
m_backingStore->endPaint();
m_backingStore->flush(rect);
}
private:
QBackingStore *m_backingStore;
bool m_update_pending;
};
int main(int argc, char *argv[])
{
MyWindow window;
window.show();
return app.exec();
}
#include <QtWidgets>
class MyWindow: public QWindow
{
public:
MyWindow():
m_update_pending(false)
{
m_backingStore = new QBackingStore(this);
setTitle("QWindow Background Image");
resize(300,250);
}
bool event(QEvent *event)
{
if (event->type() == QEvent::UpdateRequest)
{
m_update_pending = false;
renderNow();
return true;
}
return QWindow::event(event);
}
void renderLater()
{
if (!m_update_pending)
{
m_update_pending = true;
//Post this event to event queue for later processing
QCoreApplication::postEvent(this,
new QEvent(QEvent::UpdateRequest));
}
}
void resizeEvent(QResizeEvent *resizeEvent)
{
m_backingStore->resize(resizeEvent->size());
if (isExposed())
{
renderNow();
}
}
void exposeEvent(QExposeEvent *)
{
if (isExposed())
{
renderNow();
}
}
void renderNow()
{
if (!isExposed())
return;
QRect rect(0, 0, width(), height());
m_backingStore->beginPaint(rect);
QPaintDevice *device = m_backingStore->paintDevice();
QPainter painter(device);
painter.drawPixmap(rect,QPixmap("background.jpg"));
painter.drawText(QRectF(0, 0, width(), height()),
Qt::AlignCenter, QStringLiteral("QWindow"));
m_backingStore->endPaint();
m_backingStore->flush(rect);
}
private:
QBackingStore *m_backingStore;
bool m_update_pending;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWindow window;
window.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks