C++ (Qt)void graphicsView::drawBackground(QPainter *painter, const QRectF &rect){ if (image.size() != viewport()->size()) { image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied); } QPainter imagePainter(&image); renderer->load(QString("map.svg")); renderer->render(&imagePainter, rect); imagePainter.end(); painter->drawImage(0, 0, image); }
C++ (Qt)QGraphicsView::QGraphicsView(){ renderer->load(QString("map.svg"));} void QGraphicsView::resizeEvent(QResizeEvent *event){ image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied); QPainter imagePainter(&image); renderer->render(&imagePainter, rect); imagePainter.end();} void graphicsView::drawBackground(QPainter *painter, const QRectF &rect){ painter->drawImage(0, 0, image);}
C++ (Qt)void QGraphicsView::resizeEvent(QResizeEvent *event){ image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied); QPainter imagePainter(&image); renderer->render(&imagePainter, rect); imagePainter.end();}
C++ (Qt)void graphicsView::drawBackground(QPainter *painter, const QRectF &rect){ renderer->render(painter, rect);}
C++ (Qt)void graphicsView::drawBackground(QPainter *painter, const QRectF &rect){ renderer->render(painter, QRectF(0, 0, 2700, 2000));}
C++ (Qt)class testWidget : public QWidget{ Q_OBJECT public: testWidget(QWidget* parent = 0); protected: virtual void paintEvent(QPaintEvent *);}; testWidget::testWidget(QWidget *parent) : QWidget(parent){ resize(200, 200); QTimer* processTimer = new QTimer(); processTimer->setInterval(40); connect(processTimer, SIGNAL(timeout()), this, SLOT(update())); processTimer->start(); } void testWidget::paintEvent(QPaintEvent *){ QPainter p(this); static int i = 0; i+=1; p.setBrush((QColor)i); p.drawRect(0, 0, 200, 200); return;}
C++ (Qt)MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ resize(500, 500); // Главный виджет wgtMain = new QWidget(this); wgtMain->setBackgroundRole(QPalette::Base); // SVG-карта map = new QGraphicsSvgItem("map.svg"); map->setFlags(QGraphicsItem::ItemClipsToShape); map->setCacheMode(QGraphicsItem::DeviceCoordinateCache); map->setZValue(0); // Тестовый виджет test = new testWidget; // Графическая сцена scene = new QGraphicsScene(wgtMain); scene->addItem(map); scene->addWidget(test); // Область отображения для сцены scene->setSceneRect(map->boundingRect()); lblBackgroundImage = new QGraphicsView(scene, wgtMain); // Область просмотра сменил на OGL-виджет, иначе загрузка процессора была под 100%. Сейчас 50% lblBackgroundImage->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); lblBackgroundImage->setGeometry(0, 55, width(), height() - 55); lblBackgroundImage->setDragMode(QGraphicsView::ScrollHandDrag); lblBackgroundImage->setRenderHint(QPainter::Antialiasing); setCentralWidget(wgtMain);}
C++ (Qt)graphicsView::graphicsView(QGraphicsScene* scene, QWidget* parent) : QGraphicsView(scene, parent){ setCacheMode(QGraphicsView::CacheBackground); renderer = new QSvgRenderer(this); renderer->load(QString("map.svg")); setSceneRect(renderer->viewBoxF());} void graphicsView::drawBackground(QPainter* painter, const QRectF&){ renderer->render(painter, renderer->viewBoxF());}