1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "mainwindow.h"
#include "fbitem.h"
#include <QVBoxLayout>
#include <QGroupBox>
#include <QRadioButton>
#include <QCheckBox>
#include <QLabel>
#include <QQuickItem>
MainWindow::MainWindow(bool transparency, bool noRenderAlpha)
: m_currentView(nullptr),
m_currentRootObject(nullptr),
m_transparent(transparency),
m_noRenderAlpha(noRenderAlpha)
{
auto *layout = new QVBoxLayout;
QGroupBox *groupBox = new QGroupBox(tr("Type"));
auto *vbox = new QVBoxLayout;
m_radioView = new QRadioButton(tr("QQuickView in a window container (direct)"));
m_radioWidget = new QRadioButton(tr("QQuickWidget (indirect through framebuffer objects)"));
vbox->addWidget(m_radioWidget);
vbox->addWidget(m_radioView);
m_radioWidget->setChecked(true);
m_state = Unknown;
connect(m_radioWidget, &QRadioButton::toggled, this, &MainWindow::updateView);
connect(m_radioView, &QRadioButton::toggled, this, &MainWindow::updateView);
groupBox->setLayout(vbox);
layout->addWidget(groupBox);
m_checkboxMultiSample = new QCheckBox(tr("Multisample (4x)"));
connect(m_checkboxMultiSample, &QCheckBox::toggled, this, &MainWindow::updateView);
layout->addWidget(m_checkboxMultiSample);
m_labelStatus = new QLabel;
layout->addWidget(m_labelStatus);
QWidget *quickContainer = new QWidget;
layout->addWidget(quickContainer);
layout->setStretchFactor(quickContainer, 8);
m_containerLayout = new QVBoxLayout;
quickContainer->setLayout(m_containerLayout);
// Add an overlay widget to demonstrate that it will _not_ work with
// QQuickView, whereas it is perfectly fine with QQuickWidget.
QPalette semiTransparent(QColor(255,0,0,128));
semiTransparent.setBrush(QPalette::Text, Qt::white);
semiTransparent.setBrush(QPalette::WindowText, Qt::white);
m_overlayLabel = new QLabel("This is a\nsemi-transparent\n overlay widget\nwhich is placed\non top\n of the Quick\ncontent.", this);
m_overlayLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
m_overlayLabel->setAutoFillBackground(true);
m_overlayLabel->setPalette(semiTransparent);
QFont f = font();
f.setPixelSize(QFontInfo(f).pixelSize()*2);
f.setWeight(QFont::Bold);
m_overlayLabel->setFont(f);
m_overlayLabel->hide();
m_checkboxOverlayVisible = new QCheckBox(tr("Show widget overlay"));
connect(m_checkboxOverlayVisible, &QCheckBox::toggled, m_overlayLabel, &QWidget::setVisible);
layout->addWidget(m_checkboxOverlayVisible);
setLayout(layout);
updateView();
}
void MainWindow::resizeEvent(QResizeEvent*)
{
int margin = width() / 10;
int top = m_checkboxMultiSample->y();
int bottom = m_checkboxOverlayVisible->geometry().bottom();
m_overlayLabel->setGeometry(margin, top, width() - 2 * margin, bottom - top);
}
void MainWindow::switchTo(QWidget *view)
{
if (m_containerLayout->count())
m_containerLayout->takeAt(0);
delete m_currentView;
m_currentView = view;
m_containerLayout->addWidget(m_currentView);
m_currentView->setFocus();
}
void MainWindow::updateView()
{
QSurfaceFormat format;
format.setDepthBufferSize(16);
format.setStencilBufferSize(8);
if (m_transparent)
format.setAlphaBufferSize(8);
if (m_checkboxMultiSample->isChecked())
format.setSamples(4);
State state = m_radioView->isChecked() ? UseWindow : UseWidget;
if (m_format == format && m_state == state)
return;
m_format = format;
m_state = state;
QString text = m_currentRootObject
? m_currentRootObject->property("currentText").toString()
: QStringLiteral("Hello Qt");
QUrl source("qrc:qquickwidgetversuswindow_opengl/test.qml");
if (m_state == UseWindow) {
auto *quickView = new QQuickView;
// m_transparent is not supported here since many systems have problems with semi-transparent child windows
quickView->setFormat(m_format);
quickView->setResizeMode(QQuickView::SizeRootObjectToView);
connect(quickView, &QQuickView::statusChanged, this, &MainWindow::onStatusChangedView);
connect(quickView, &QQuickView::sceneGraphError, this, &MainWindow::onSceneGraphError);
quickView->setSource(source);
m_currentRootObject = quickView->rootObject();
switchTo(QWidget::createWindowContainer(quickView));
} else if (m_state == UseWidget) {
auto *quickWidget = new QQuickWidget;
if (m_transparent)
quickWidget->setClearColor(Qt::transparent);
quickWidget->setFormat(m_format);
quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
connect(quickWidget, &QQuickWidget::statusChanged, this, &MainWindow::onStatusChangedWidget);
connect(quickWidget, &QQuickWidget::sceneGraphError, this, &MainWindow::onSceneGraphError);
quickWidget->setSource(source);
switchTo(quickWidget);
m_currentRootObject = quickWidget->rootObject();
}
if (m_currentRootObject) {
m_currentRootObject->setProperty("currentText", text);
m_currentRootObject->setProperty("multisample", m_checkboxMultiSample->isChecked());
if (!m_noRenderAlpha)
m_currentRootObject->setProperty("translucency", m_transparent);
}
m_overlayLabel->raise();
}
void MainWindow::onStatusChangedView(QQuickView::Status status)
{
QString s;
switch (status) {
case QQuickView::Null:
s = tr("Null");
break;
case QQuickView::Ready:
s = tr("Ready");
break;
case QQuickView::Loading:
s = tr("Loading");
break;
case QQuickView::Error:
s = tr("Error");
break;
default:
s = tr("Unknown");
break;
}
m_labelStatus->setText(tr("QQuickView status: %1").arg(s));
}
void MainWindow::onStatusChangedWidget(QQuickWidget::Status status)
{
QString s;
switch (status) {
case QQuickWidget::Null:
s = tr("Null");
break;
case QQuickWidget::Ready:
s = tr("Ready");
break;
case QQuickWidget::Loading:
s = tr("Loading");
break;
case QQuickWidget::Error:
s = tr("Error");
break;
default:
s = tr("Unknown");
break;
}
m_labelStatus->setText(tr("QQuickWidget status: %1").arg(s));
}
void MainWindow::onSceneGraphError(QQuickWindow::SceneGraphError error, const QString &message)
{
m_labelStatus->setText(tr("Scenegraph error %1: %2").arg(error).arg(message));
}
|