2022-05-13 13:12:05 +00:00
|
|
|
// Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2014-02-18 11:15:30 +00:00
|
|
|
|
2021-11-08 15:16:16 +00:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QCommandLineParser>
|
2014-02-18 11:15:30 +00:00
|
|
|
#include <QQuickWidget>
|
2016-09-20 10:53:31 +00:00
|
|
|
#include <QQuickItem>
|
2014-03-05 12:21:31 +00:00
|
|
|
#include <QQmlError>
|
2021-11-08 15:16:16 +00:00
|
|
|
#include <QMdiArea>
|
|
|
|
#include <QLCDNumber>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QStatusBar>
|
|
|
|
#include <QMainWindow>
|
|
|
|
#include <QMenuBar>
|
|
|
|
#include <QPushButton>
|
2017-09-05 11:51:52 +00:00
|
|
|
|
2014-03-05 12:21:31 +00:00
|
|
|
class MainWindow : public QMainWindow {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
MainWindow();
|
2014-02-18 11:15:30 +00:00
|
|
|
|
2014-03-05 12:21:31 +00:00
|
|
|
private slots:
|
|
|
|
void quickWidgetStatusChanged(QQuickWidget::Status);
|
|
|
|
void sceneGraphError(QQuickWindow::SceneGraphError error, const QString &message);
|
2016-09-20 10:53:31 +00:00
|
|
|
void grabFramebuffer();
|
|
|
|
void renderToPixmap();
|
|
|
|
void grabToImage();
|
2016-06-20 09:00:39 +00:00
|
|
|
void createQuickWidgetsInTabs(QMdiArea *mdiArea);
|
2014-03-05 12:21:31 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
QQuickWidget *m_quickWidget;
|
|
|
|
};
|
|
|
|
|
2021-11-08 15:16:16 +00:00
|
|
|
static bool optMultipleSample = false;
|
|
|
|
|
2014-03-05 12:21:31 +00:00
|
|
|
MainWindow::MainWindow()
|
|
|
|
: m_quickWidget(new QQuickWidget)
|
|
|
|
{
|
2014-06-04 13:05:00 +00:00
|
|
|
QSurfaceFormat format;
|
2017-09-05 11:51:52 +00:00
|
|
|
if (optMultipleSample)
|
2014-06-04 13:05:00 +00:00
|
|
|
format.setSamples(4);
|
|
|
|
m_quickWidget->setFormat(format);
|
2014-03-17 14:30:49 +00:00
|
|
|
|
2014-03-05 12:21:31 +00:00
|
|
|
QMdiArea *centralWidget = new QMdiArea;
|
2014-02-18 11:15:30 +00:00
|
|
|
|
|
|
|
QLCDNumber *lcd = new QLCDNumber;
|
|
|
|
lcd->display(1337);
|
|
|
|
lcd->setMinimumSize(250,100);
|
2016-06-20 09:00:39 +00:00
|
|
|
centralWidget->addSubWindow(lcd);
|
2014-02-18 11:15:30 +00:00
|
|
|
|
|
|
|
QUrl source("qrc:quickwidget/rotatingsquare.qml");
|
|
|
|
|
2015-07-21 14:20:38 +00:00
|
|
|
connect(m_quickWidget, &QQuickWidget::statusChanged,
|
|
|
|
this, &MainWindow::quickWidgetStatusChanged);
|
|
|
|
connect(m_quickWidget, &QQuickWidget::sceneGraphError,
|
|
|
|
this, &MainWindow::sceneGraphError);
|
2014-03-05 12:21:31 +00:00
|
|
|
m_quickWidget->resize(300,300);
|
|
|
|
m_quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView );
|
|
|
|
m_quickWidget->setSource(source);
|
2014-02-18 11:15:30 +00:00
|
|
|
|
2016-06-20 09:00:39 +00:00
|
|
|
centralWidget->addSubWindow(m_quickWidget);
|
2014-02-18 11:15:30 +00:00
|
|
|
|
2014-03-05 12:21:31 +00:00
|
|
|
setCentralWidget(centralWidget);
|
2014-02-18 11:15:30 +00:00
|
|
|
|
2014-08-07 13:41:58 +00:00
|
|
|
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
2023-12-29 02:15:14 +00:00
|
|
|
auto grabAction = fileMenu->addAction(tr("Grab framebuffer"), this, &MainWindow::grabFramebuffer);
|
|
|
|
auto renderAction = fileMenu->addAction(tr("Render to pixmap"), this, &MainWindow::renderToPixmap);
|
|
|
|
auto grabToImageAction = fileMenu->addAction(tr("Grab via grabToImage"), this, &MainWindow::grabToImage);
|
2015-07-21 14:20:38 +00:00
|
|
|
fileMenu->addAction(tr("Quit"), qApp, &QCoreApplication::quit);
|
2016-06-20 09:00:39 +00:00
|
|
|
|
|
|
|
QMenu *windowMenu = menuBar()->addMenu(tr("&Window"));
|
|
|
|
windowMenu->addAction(tr("Add tab widget"), this,
|
|
|
|
[this, centralWidget] { createQuickWidgetsInTabs(centralWidget); });
|
2023-12-29 02:15:14 +00:00
|
|
|
|
|
|
|
connect(m_quickWidget, &QObject::destroyed, this,
|
|
|
|
[this, grabAction, renderAction, grabToImageAction] {
|
|
|
|
m_quickWidget = nullptr;
|
|
|
|
grabAction->setEnabled(false);
|
|
|
|
renderAction->setEnabled(false);
|
|
|
|
grabToImageAction->setEnabled(false);
|
|
|
|
});
|
2016-06-20 09:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::createQuickWidgetsInTabs(QMdiArea *mdiArea)
|
|
|
|
{
|
2021-11-08 15:16:16 +00:00
|
|
|
// A QQuickWidget should work like any other widget when it comes to being
|
|
|
|
// in layouts, in tab widgets, MDI areas, etc. It can also be freely
|
|
|
|
// reparented and made top-level.
|
2016-06-20 09:00:39 +00:00
|
|
|
|
2021-11-08 15:16:16 +00:00
|
|
|
QTabWidget *tabWidget = new QTabWidget;
|
2016-06-20 09:00:39 +00:00
|
|
|
const QSize size(400, 400);
|
2021-11-08 15:16:16 +00:00
|
|
|
const QString msgToTopLevel = QLatin1String("Break out to top-level window");
|
|
|
|
const QString msgFromTopLevel = QLatin1String("Move back under tab widget");
|
|
|
|
|
|
|
|
static const int N = 4;
|
|
|
|
static const QColor colorTab[N] = { Qt::green, Qt::blue, Qt::yellow, Qt::magenta };
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
QQuickWidget *widget = new QQuickWidget;
|
|
|
|
widget->resize(size);
|
|
|
|
widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
|
|
|
|
QObject::connect(widget, &QQuickWidget::statusChanged, widget, [widget, i] {
|
|
|
|
if (widget->status() == QQuickWidget::Ready) {
|
|
|
|
if (QQuickItem *rootItem = widget->rootObject()) {
|
|
|
|
rootItem->setProperty("rectColor", colorTab[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
widget->setSource(QUrl("qrc:quickwidget/rotatingsquare.qml"));
|
|
|
|
widget->setWindowTitle(QString::asprintf("Tab %d", i + 1));
|
|
|
|
QPushButton *btn = new QPushButton(msgToTopLevel, widget);
|
|
|
|
connect(btn, &QPushButton::clicked, widget, [=] {
|
|
|
|
if (widget->parent()) {
|
|
|
|
widget->setAttribute(Qt::WA_DeleteOnClose, true);
|
|
|
|
widget->setParent(nullptr);
|
2024-02-29 09:13:08 +00:00
|
|
|
connect(this, &QObject::destroyed, widget, &QWidget::close);
|
2021-11-08 15:16:16 +00:00
|
|
|
widget->show();
|
|
|
|
btn->setText(msgFromTopLevel);
|
|
|
|
} else {
|
|
|
|
widget->setAttribute(Qt::WA_DeleteOnClose, false);
|
2024-02-29 09:13:08 +00:00
|
|
|
disconnect(this, &QObject::destroyed, widget, &QWidget::close);
|
2021-11-08 15:16:16 +00:00
|
|
|
tabWidget->addTab(widget, widget->windowTitle());
|
|
|
|
btn->setText(msgToTopLevel);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
tabWidget->addTab(widget, QString::asprintf("Tab %d", i + 1));
|
|
|
|
}
|
2016-06-20 09:00:39 +00:00
|
|
|
|
|
|
|
mdiArea->addSubWindow(tabWidget);
|
|
|
|
tabWidget->show();
|
2014-03-05 12:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::quickWidgetStatusChanged(QQuickWidget::Status status)
|
|
|
|
{
|
|
|
|
if (status == QQuickWidget::Error) {
|
|
|
|
QStringList errors;
|
2023-12-29 02:15:14 +00:00
|
|
|
Q_ASSERT(m_quickWidget);
|
2016-08-18 15:03:47 +00:00
|
|
|
const auto widgetErrors = m_quickWidget->errors();
|
|
|
|
for (const QQmlError &error : widgetErrors)
|
2014-03-05 12:21:31 +00:00
|
|
|
errors.append(error.toString());
|
|
|
|
statusBar()->showMessage(errors.join(QStringLiteral(", ")));
|
|
|
|
}
|
2014-02-18 11:15:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 12:21:31 +00:00
|
|
|
void MainWindow::sceneGraphError(QQuickWindow::SceneGraphError, const QString &message)
|
|
|
|
{
|
|
|
|
statusBar()->showMessage(message);
|
|
|
|
}
|
2014-02-18 11:15:30 +00:00
|
|
|
|
2014-08-07 13:41:58 +00:00
|
|
|
template<class T> void saveToFile(QWidget *parent, T *saveable)
|
|
|
|
{
|
2016-09-20 10:53:31 +00:00
|
|
|
QFileDialog fd(parent);
|
2014-08-07 13:41:58 +00:00
|
|
|
fd.setAcceptMode(QFileDialog::AcceptSave);
|
|
|
|
fd.setDefaultSuffix("png");
|
|
|
|
fd.selectFile("test.png");
|
|
|
|
if (fd.exec() == QDialog::Accepted)
|
|
|
|
saveable->save(fd.selectedFiles().first());
|
|
|
|
}
|
|
|
|
|
2016-09-20 10:53:31 +00:00
|
|
|
void MainWindow::grabFramebuffer()
|
2014-08-07 13:41:58 +00:00
|
|
|
{
|
2023-12-29 02:15:14 +00:00
|
|
|
Q_ASSERT(m_quickWidget);
|
2014-08-07 13:41:58 +00:00
|
|
|
QImage image = m_quickWidget->grabFramebuffer();
|
|
|
|
saveToFile(this, &image);
|
|
|
|
}
|
|
|
|
|
2016-09-20 10:53:31 +00:00
|
|
|
void MainWindow::renderToPixmap()
|
2014-08-07 13:41:58 +00:00
|
|
|
{
|
2023-12-29 02:15:14 +00:00
|
|
|
Q_ASSERT(m_quickWidget);
|
2014-08-07 13:41:58 +00:00
|
|
|
QPixmap pixmap(m_quickWidget->size());
|
|
|
|
m_quickWidget->render(&pixmap);
|
|
|
|
saveToFile(this, &pixmap);
|
|
|
|
}
|
|
|
|
|
2016-09-20 10:53:31 +00:00
|
|
|
void MainWindow::grabToImage()
|
|
|
|
{
|
|
|
|
QFileDialog fd(this);
|
|
|
|
fd.setAcceptMode(QFileDialog::AcceptSave);
|
|
|
|
fd.setDefaultSuffix("png");
|
|
|
|
fd.selectFile("test_grabToImage.png");
|
|
|
|
if (fd.exec() == QDialog::Accepted) {
|
2023-12-29 02:15:14 +00:00
|
|
|
Q_ASSERT(m_quickWidget);
|
2016-09-20 10:53:31 +00:00
|
|
|
QMetaObject::invokeMethod(m_quickWidget->rootObject(), "performLayerBasedGrab",
|
|
|
|
Q_ARG(QVariant, fd.selectedFiles().first()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 12:21:31 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
QApplication app(argc, argv);
|
|
|
|
|
2017-09-05 11:51:52 +00:00
|
|
|
QCoreApplication::setApplicationName("Qt QQuickWidget Example");
|
|
|
|
QCoreApplication::setOrganizationName("QtProject");
|
|
|
|
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
|
2021-11-08 15:16:16 +00:00
|
|
|
|
2017-09-05 11:51:52 +00:00
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.setApplicationDescription(QCoreApplication::applicationName());
|
|
|
|
parser.addHelpOption();
|
|
|
|
parser.addVersionOption();
|
|
|
|
QCommandLineOption multipleSampleOption("multisample", "Multisampling");
|
|
|
|
parser.addOption(multipleSampleOption);
|
|
|
|
|
|
|
|
parser.process(app);
|
|
|
|
|
|
|
|
optMultipleSample = parser.isSet(multipleSampleOption);
|
|
|
|
|
2024-02-29 09:13:08 +00:00
|
|
|
MainWindow *mainWindow = new MainWindow;
|
|
|
|
mainWindow->setAttribute(Qt::WA_DeleteOnClose, true);
|
|
|
|
mainWindow->show();
|
2014-02-18 11:15:30 +00:00
|
|
|
|
2014-03-05 12:21:31 +00:00
|
|
|
return app.exec();
|
|
|
|
}
|
2014-02-18 11:15:30 +00:00
|
|
|
|
2014-03-05 12:21:31 +00:00
|
|
|
#include "main.moc"
|