| 1 |
// Copyright (C) 2022 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "mainwindow.h" |
| 5 |
#include "./ui_mainwindow.h" |
| 6 |
#include <QPushButton> |
| 7 |
#include <QSlider> |
| 8 |
#include <QLabel> |
| 9 |
#include <QGraphicsEllipseItem> |
| 10 |
#include <QRandomGenerator> |
| 11 |
#include <QOpenGLWidget> |
| 12 |
|
| 13 |
MainWindow::MainWindow(QWidget *parent) |
| 14 |
: QMainWindow(parent) |
| 15 |
, ui(new Ui::MainWindow) |
| 16 |
{ |
| 17 |
ui->setupUi(this); |
| 18 |
|
| 19 |
ui->graphicsView->setViewport(new QOpenGLWidget); |
| 20 |
ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate); |
| 21 |
|
| 22 |
QRect rect = ui->graphicsView->rect(); |
| 23 |
QGraphicsScene *m_scene = new QGraphicsScene(rect); |
| 24 |
ui->graphicsView->setScene(m_scene); |
| 25 |
|
| 26 |
QSize initialSize(150, 150); |
| 27 |
m_ellipse = m_scene->addEllipse( |
| 28 |
rect.width() / 2 - initialSize.width() / 2, |
| 29 |
rect.height() / 2 - initialSize.height() / 2, |
| 30 |
initialSize.width(), |
| 31 |
initialSize.height(), |
| 32 |
QPen(Qt::magenta, 5), |
| 33 |
Qt::blue); |
| 34 |
|
| 35 |
QPushButton *button = new QPushButton(); |
| 36 |
button->setGeometry(QRect(rect.width() / 2 - 75, 100, 150, 50)); |
| 37 |
button->setText("Random ellipse color"); |
| 38 |
QObject::connect(button, &QPushButton::pressed, [&]() { |
| 39 |
m_ellipse->setBrush(QColor::fromRgb(QRandomGenerator::global()->generate())); |
| 40 |
m_ellipse->setPen(QPen(QColor::fromRgb(QRandomGenerator::global()->generate()), 5)); |
| 41 |
}); |
| 42 |
m_scene->addWidget(button); |
| 43 |
|
| 44 |
m_label = new QLabel(); |
| 45 |
m_label->setGeometry(QRect(rect.width() / 2 - 50, rect.height() - 100, 100, 50)); |
| 46 |
m_label->setAlignment(Qt::AlignCenter); |
| 47 |
m_scene->addWidget(m_label); |
| 48 |
|
| 49 |
QSlider *slider = new QSlider(Qt::Horizontal); |
| 50 |
slider->setGeometry(QRect(rect.width() / 2 - 100, rect.height() - 50, 200, 50)); |
| 51 |
slider->setMinimum(10); |
| 52 |
slider->setMaximum(300); |
| 53 |
slider->setValue(initialSize.width()); |
| 54 |
QObject::connect(slider, &QAbstractSlider::valueChanged, [this](int value){ |
| 55 |
QRectF rect = m_ellipse->rect(); |
| 56 |
rect.setWidth(value); |
| 57 |
rect.setHeight(value); |
| 58 |
m_ellipse->setRect(rect); |
| 59 |
m_label->setText("Ellipse size: " + QString::number(value)); |
| 60 |
}); |
| 61 |
emit slider->valueChanged(initialSize.width()); |
| 62 |
m_scene->addWidget(slider); |
| 63 |
|
| 64 |
QMenu *screenShotMenu = menuBar()->addMenu("&Screenshot"); |
| 65 |
screenShotMenu->addAction("Left buffer", this, [this](){ |
| 66 |
ui->graphicsView->saveImage(QOpenGLWidget::LeftBuffer); |
| 67 |
}); |
| 68 |
|
| 69 |
screenShotMenu->addAction("Right buffer", this, [this](){ |
| 70 |
ui->graphicsView->saveImage(QOpenGLWidget::RightBuffer); |
| 71 |
}); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
MainWindow::~MainWindow() |
| 76 |
{ |
| 77 |
delete ui; |
| 78 |
} |
| 79 |
|