C++ (Qt)#include <QApplication>#include <QWidget>#include <QPushButton>#include <QLabel>#include <QPixmap>#include <QTimer>#include <QPainter> class Widget : public QWidget{ Q_OBJECTpublic: explicit Widget(QWidget *parent = 0) : QWidget(parent) , m_target(new QLabel) , m_timer(new QTimer(this)) { m_target->show(); auto pb = new QPushButton(tr("Hello"), this); Q_UNUSED(pb); connect(m_timer, &QTimer::timeout, [this]() { m_text = QString("%1").arg(qrand()); update(); }); m_timer->start(1000); } void paintEvent(QPaintEvent *event) { Q_UNUSED(event); { // Рисуем сначала все в нашу пиксмапу. QPainter p(&m_pixmap); p.drawText(rect(), Qt::AlignBottom, m_text); } // Дублируем пиксмапу на другом целевом виджете. m_target->setPixmap(m_pixmap); { // Потом рисуем пиксмапу на нашем главном виджете. QPainter p(this); p.drawPixmap(m_pixmap.rect(), m_pixmap); } // Потом очищаем пиксмапу. m_pixmap = QPixmap(size()); } void resizeEvent(QResizeEvent *event) { QWidget::resizeEvent(event); m_pixmap = QPixmap(size()); } private: QLabel *m_target; QTimer *m_timer; QPixmap m_pixmap; QString m_text;}; int main(int argc, char *argv[]){ QApplication a(argc, argv); Widget w; w.show(); return a.exec();} #include "main.moc"
C++ (Qt)#include <QApplication>#include <QWidget>#include <QPushButton>#include <QLabel>#include <QPixmap>#include <QTimer>#include <QPainter>#include <QScreen>#include <QElapsedTimer>#include <QDebug> class Widget : public QWidget{ Q_OBJECTpublic: explicit Widget(QWidget *parent = 0) : QWidget(parent) , m_target(new QLabel) , m_timer(new QTimer(this)) , m_screen(QGuiApplication::primaryScreen()) { m_target->show(); auto pb = new QPushButton(tr("Hello"), this); Q_UNUSED(pb); connect(m_timer, &QTimer::timeout, [this]() { m_text = QString("%1").arg(qrand()); update(); }); m_timer->start(50); // ~20 FPS } void paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter p(this); p.drawText(rect(), Qt::AlignBottom, m_text); QElapsedTimer et; et.start(); const auto pixmap = m_screen->grabWindow(winId()); qDebug() << "elapsed:" << et.elapsed(); m_target->setPixmap(pixmap); } private: QLabel *m_target; QTimer *m_timer; QScreen *m_screen; QString m_text;}; int main(int argc, char *argv[]){ QApplication a(argc, argv); Widget w; w.resize(1920, 1080); w.show(); return a.exec();} #include "main.moc"
C++ (Qt)#include <QApplication>#include <QWidget>#include <QPushButton>#include <QLabel>#include <QPixmap>#include <QTimer>#include <QPainter>#include <QPaintEvent>#include <QScreen>#include <QElapsedTimer>#include <QDebug> class Widget : public QWidget{ Q_OBJECTpublic: explicit Widget(QWidget *parent = 0) : QWidget(parent) , m_target(new QLabel) , m_timer(new QTimer(this)) , m_screen(QGuiApplication::primaryScreen()) { //setAttribute(Qt::WA_DontShowOnScreen); m_target->show(); auto pb = new QPushButton(tr("Hello"), this); Q_UNUSED(pb); connect(m_timer, &QTimer::timeout, [this]() { //m_text = QString("%1").arg(qrand()); //update(); }); m_timer->start(5000); // ~20 FPS } void paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter p(this); p.drawText(rect(), Qt::AlignBottom, m_text); QTimer::singleShot(0, [=](){ QElapsedTimer et; et.start(); QPixmap pixmap = grab(); qDebug() << "elapsed:" << et.elapsed(); m_target->setPixmap(pixmap); }); } private: QLabel *m_target; QTimer *m_timer; QScreen *m_screen; QString m_text;}; int main(int argc, char *argv[]){ QApplication a(argc, argv); Widget w; w.resize(800, 600); w.show(); return a.exec();}#include "main.moc"
C++ (Qt)connect(m_timer, &QTimer::timeout, [this]() { m_text = QString("%1").arg(qrand()); update(); QPixmap pixmap = grab(); m_target->setPixmap(pixmap);});