| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2017 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the test suite of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 |
** included in the packaging of this file. Please review the following |
| 22 |
** information to ensure the GNU General Public License requirements will |
| 23 |
** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 |
** |
| 25 |
** $QT_END_LICENSE$ |
| 26 |
** |
| 27 |
****************************************************************************/ |
| 28 |
|
| 29 |
#include <QtWidgets> |
| 30 |
|
| 31 |
class CursorWindow : public QRasterWindow |
| 32 |
{ |
| 33 |
public: |
| 34 |
CursorWindow(QCursor cursor, QColor color) |
| 35 |
:m_cursor(cursor) |
| 36 |
,m_color(color) |
| 37 |
{ |
| 38 |
if (cursor.shape() == Qt::ArrowCursor) |
| 39 |
unsetCursor(); |
| 40 |
else |
| 41 |
setCursor(cursor); |
| 42 |
} |
| 43 |
|
| 44 |
void paintEvent(QPaintEvent *e) |
| 45 |
{ |
| 46 |
QPainter p(this); |
| 47 |
p.fillRect(e->rect(), m_color); |
| 48 |
} |
| 49 |
|
| 50 |
void mousePressEvent(QMouseEvent *) |
| 51 |
{ |
| 52 |
// Toggle cursor |
| 53 |
QCursor newCursor = (cursor().shape() == m_cursor.shape()) ? QCursor() : m_cursor; |
| 54 |
if (newCursor.shape() == Qt::ArrowCursor) |
| 55 |
unsetCursor(); |
| 56 |
else |
| 57 |
setCursor(newCursor); |
| 58 |
} |
| 59 |
|
| 60 |
private: |
| 61 |
QCursor m_cursor; |
| 62 |
QColor m_color; |
| 63 |
}; |
| 64 |
|
| 65 |
class CursorWidget : public QWidget |
| 66 |
{ |
| 67 |
public: |
| 68 |
CursorWidget(QCursor cursor, QColor color) |
| 69 |
:m_cursor(cursor) |
| 70 |
,m_color(color) |
| 71 |
{ |
| 72 |
if (cursor.shape() == Qt::ArrowCursor) |
| 73 |
unsetCursor(); |
| 74 |
else |
| 75 |
setCursor(cursor); |
| 76 |
} |
| 77 |
|
| 78 |
void paintEvent(QPaintEvent *e) |
| 79 |
{ |
| 80 |
QPainter p(this); |
| 81 |
p.fillRect(e->rect(), m_color); |
| 82 |
} |
| 83 |
|
| 84 |
void mousePressEvent(QMouseEvent *) |
| 85 |
{ |
| 86 |
// Toggle cursor |
| 87 |
QCursor newCursor = (cursor().shape() == m_cursor.shape()) ? QCursor() : m_cursor; |
| 88 |
if (newCursor.shape() == Qt::ArrowCursor) |
| 89 |
unsetCursor(); |
| 90 |
else |
| 91 |
setCursor(newCursor); |
| 92 |
} |
| 93 |
|
| 94 |
private: |
| 95 |
QCursor m_cursor; |
| 96 |
QColor m_color; |
| 97 |
}; |
| 98 |
|
| 99 |
int main(int argc, char **argv) |
| 100 |
{ |
| 101 |
QApplication app(argc, argv); |
| 102 |
|
| 103 |
{ |
| 104 |
// Create top-level windowContainer with window. Setting the cursor |
| 105 |
// for the container should set the cursor for the window as well. |
| 106 |
// Setting the cursor for the window overrides the cursor for the |
| 107 |
// container. The example starts out with a window cursor; click |
| 108 |
// to fall back to the container cursor. |
| 109 |
CursorWindow *w1 = new CursorWindow(QCursor(Qt::OpenHandCursor), QColor(Qt::red).darker()); |
| 110 |
QWidget* container = QWidget::createWindowContainer(w1); |
| 111 |
container->resize(200, 200); |
| 112 |
container->setCursor(Qt::PointingHandCursor); |
| 113 |
container->show(); |
| 114 |
} |
| 115 |
|
| 116 |
{ |
| 117 |
// Similar to above, but with a top-level QWiget |
| 118 |
CursorWidget *w1 = new CursorWidget(QCursor(Qt::IBeamCursor), QColor(Qt::green).darker()); |
| 119 |
w1->resize(200, 200); |
| 120 |
|
| 121 |
CursorWindow *w2 = new CursorWindow(QCursor(Qt::OpenHandCursor), QColor(Qt::red).darker()); |
| 122 |
QWidget* container = QWidget::createWindowContainer(w2); |
| 123 |
container->winId(); // must make the container native, otherwise setCursor |
| 124 |
// sets the cursor on a QWindowContainerClassWindow which |
| 125 |
// is outside the QWindow hierarchy (macOS). |
| 126 |
container->setParent(w1); |
| 127 |
container->setCursor(Qt::PointingHandCursor); |
| 128 |
container->setGeometry(0, 0, 100, 100); |
| 129 |
|
| 130 |
w1->show(); |
| 131 |
} |
| 132 |
|
| 133 |
return app.exec(); |
| 134 |
} |