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
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "openglwindow.h"
#include <QApplication>
#include <QFocusEvent>
#include <QHBoxLayout>
#include <QKeyEvent>
#include <QLineEdit>
#include <QMouseEvent>
#include <QPainter>
#include <QWidget>
// Making use of the class from the openglwindow example
class Window : public OpenGLWindow
{
Q_OBJECT
public:
using OpenGLWindow::OpenGLWindow;
void render(QPainter *p) override
{
QLinearGradient g(0, 0, 0, height());
g.setColorAt(0, QColor("lightsteelblue"));
g.setColorAt(1, Qt::black);
p->fillRect(0, 0, width(), height(), g);
p->setPen(Qt::white);
p->drawText(20, 30, QLatin1String("This is an OpenGL based QWindow"));
if (m_key.trimmed().length() > 0) {
QRect bounds = p->boundingRect(QRect(0, 0, width(), height()), Qt::AlignTop | Qt::AlignLeft, m_key);
p->save();
p->translate(width() / 2.0, height() / 2.0);
p->scale(10, 10);
p->translate(-bounds.width() / 2.0, -bounds.height() / 2.0);
p->drawText(bounds, Qt::AlignCenter, m_key);
p->restore();
}
if (m_focus)
p->drawText(20, height() - 20, QLatin1String("Window has focus!"));
p->setRenderHint(QPainter::Antialiasing);
p->drawPolyline(m_polygon);
}
void mousePressEvent(QMouseEvent *e) override
{
if (!m_mouseDown) {
m_mouseDown = true;
m_polygon.clear();
m_polygon.append(e->position().toPoint());
renderLater();
}
}
void mouseMoveEvent(QMouseEvent *e) override
{
if (m_mouseDown) {
m_polygon.append(e->position().toPoint());
renderLater();
}
}
void mouseReleaseEvent(QMouseEvent *e) override
{
if (m_mouseDown) {
m_mouseDown = false;
m_polygon.append(e->position().toPoint());
renderLater();
}
}
void focusInEvent(QFocusEvent *) override
{
m_focus = true;
renderLater();
}
void focusOutEvent(QFocusEvent *) override
{
m_focus = false;
m_polygon.clear();
renderLater();
}
void keyPressEvent(QKeyEvent *e) override
{
m_key = e->text();
renderLater();
}
void keyReleaseEvent(QKeyEvent *) override
{
m_key = QString();
renderLater();
}
private:
QPolygon m_polygon;
QString m_key;
bool m_mouseDown = false;
bool m_focus = false;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *widget = new QWidget;
QHBoxLayout *layout = new QHBoxLayout(widget);
Window *window = new Window;
QWidget *container = QWidget::createWindowContainer(window);
container->setMinimumSize(300, 300);
container->setMaximumSize(600, 600);
container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
container->setFocusPolicy(Qt::StrongFocus);
window->setGeometry(100, 100, 300, 200);
layout->addWidget(new QLineEdit(QLatin1String("A QLineEdit")));
layout->addWidget(container);
layout->addWidget(new QLineEdit(QLatin1String("A QLabel")));
widget->show();
return app.exec();
}
#include "windowcontainer.moc"
|