mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-01 23:02:23 +08:00
qt 6.5.1 original
This commit is contained in:
56
tests/manual/embeddedintoforeignwindow/CMakeLists.txt
Normal file
56
tests/manual/embeddedintoforeignwindow/CMakeLists.txt
Normal file
@ -0,0 +1,56 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
#####################################################################
|
||||
## embeddedintoforeignwindow Binary:
|
||||
#####################################################################
|
||||
|
||||
qt_internal_add_manual_test(embeddedintoforeignwindow
|
||||
SOURCES
|
||||
../diaglib/eventfilter.cpp ../diaglib/eventfilter.h
|
||||
../diaglib/nativewindowdump.h
|
||||
../diaglib/qwindowdump.cpp ../diaglib/qwindowdump.h
|
||||
../diaglib/textdump.cpp ../diaglib/textdump.h
|
||||
itemwindow.cpp itemwindow.h
|
||||
main.cpp
|
||||
DEFINES
|
||||
QT_DIAG_LIB
|
||||
INCLUDE_DIRECTORIES
|
||||
../diaglib
|
||||
LIBRARIES
|
||||
Qt::CorePrivate
|
||||
Qt::Gui
|
||||
Qt::GuiPrivate
|
||||
)
|
||||
|
||||
## Scopes:
|
||||
#####################################################################
|
||||
|
||||
qt_internal_extend_target(embeddedintoforeignwindow CONDITION WIN32
|
||||
SOURCES
|
||||
../diaglib/nativewindowdump_win.cpp
|
||||
LIBRARIES
|
||||
user32
|
||||
)
|
||||
|
||||
qt_internal_extend_target(embeddedintoforeignwindow CONDITION UNIX
|
||||
SOURCES
|
||||
../diaglib/nativewindowdump.cpp
|
||||
)
|
||||
|
||||
qt_internal_extend_target(embeddedintoforeignwindow CONDITION QT_FEATURE_widgets
|
||||
SOURCES
|
||||
../diaglib/debugproxystyle.cpp ../diaglib/debugproxystyle.h
|
||||
../diaglib/logwidget.cpp ../diaglib/logwidget.h
|
||||
../diaglib/qwidgetdump.cpp ../diaglib/qwidgetdump.h
|
||||
LIBRARIES
|
||||
Qt::WidgetsPrivate
|
||||
)
|
||||
|
||||
qt_internal_extend_target(embeddedintoforeignwindow CONDITION QT_FEATURE_opengl
|
||||
SOURCES
|
||||
../diaglib/glinfo.cpp ../diaglib/glinfo.h
|
||||
LIBRARIES
|
||||
Qt::OpenGL
|
||||
Qt::OpenGLWidgets
|
||||
)
|
@ -0,0 +1,6 @@
|
||||
TEMPLATE = app
|
||||
QT += gui-private
|
||||
CONFIG += cmdline c++11
|
||||
SOURCES += main.cpp itemwindow.cpp
|
||||
HEADERS += itemwindow.h
|
||||
include(../diaglib/diaglib.pri)
|
42
tests/manual/embeddedintoforeignwindow/itemwindow.cpp
Normal file
42
tests/manual/embeddedintoforeignwindow/itemwindow.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2017 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "itemwindow.h"
|
||||
|
||||
#include <QtGui/QPainter>
|
||||
#include <QtGui/QPaintEvent>
|
||||
|
||||
void TextItem::paint(QPainter &painter)
|
||||
{
|
||||
painter.fillRect(m_rect, m_col);
|
||||
painter.drawRect(m_rect);
|
||||
QTextOption textOption;
|
||||
textOption.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
|
||||
painter.drawText(m_rect, m_text, textOption);
|
||||
}
|
||||
|
||||
void ButtonItem::mouseEvent(QMouseEvent *mouseEvent)
|
||||
{
|
||||
if (mouseEvent->type() == QEvent::MouseButtonPress && rect().contains(mouseEvent->pos())) {
|
||||
mouseEvent->accept();
|
||||
emit clicked();
|
||||
}
|
||||
}
|
||||
|
||||
void ButtonItem::keyEvent(QKeyEvent *keyEvent)
|
||||
{
|
||||
if (m_shortcut && keyEvent->type() == QEvent::KeyPress
|
||||
&& (keyEvent->key() + int(keyEvent->modifiers())) == m_shortcut) {
|
||||
keyEvent->accept();
|
||||
emit clicked();
|
||||
}
|
||||
}
|
||||
|
||||
void ItemWindow::paintEvent(QPaintEvent *)
|
||||
{
|
||||
QPainter painter(this);
|
||||
QRect rect(QPoint(0, 0), size());
|
||||
painter.fillRect(rect, m_background);
|
||||
foreach (Item *i, m_items)
|
||||
i->paint(painter);
|
||||
}
|
96
tests/manual/embeddedintoforeignwindow/itemwindow.h
Normal file
96
tests/manual/embeddedintoforeignwindow/itemwindow.h
Normal file
@ -0,0 +1,96 @@
|
||||
// Copyright (C) 2017 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#ifndef ITEMWINDOW_H
|
||||
#define ITEMWINDOW_H
|
||||
|
||||
#include <QtGui/QKeySequence>
|
||||
#include <QtGui/QRasterWindow>
|
||||
|
||||
QT_USE_NAMESPACE
|
||||
|
||||
// ItemWindow: Primitive UI item classes for use with a ItemWindow based
|
||||
// on QRasterWindow without widgets allowing a simple UI without widgets.
|
||||
|
||||
// Basic item to be used in a ItemWindow
|
||||
class Item : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit Item(QObject *parent = nullptr) : QObject(parent) {}
|
||||
|
||||
virtual void paint(QPainter &painter) = 0;
|
||||
virtual void mouseEvent(QMouseEvent *) {}
|
||||
virtual void keyEvent(QKeyEvent *) {}
|
||||
};
|
||||
|
||||
// Positionable text item
|
||||
class TextItem : public Item {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit TextItem(const QString &text, const QRect &rect, const QColor &col,
|
||||
QObject *parent = nullptr)
|
||||
: Item(parent), m_text(text), m_rect(rect), m_col(col) {}
|
||||
|
||||
void paint(QPainter &painter) override;
|
||||
|
||||
QRect rect() const { return m_rect; }
|
||||
|
||||
private:
|
||||
const QString m_text;
|
||||
const QRect m_rect;
|
||||
const QColor m_col;
|
||||
};
|
||||
|
||||
// "button" item
|
||||
class ButtonItem : public TextItem {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ButtonItem(const QString &text, const QRect &rect, const QColor &col,
|
||||
QObject *parent = nullptr)
|
||||
: TextItem(text, rect, col, parent), m_shortcut(0) {}
|
||||
|
||||
void mouseEvent(QMouseEvent *mouseEvent) override;
|
||||
void keyEvent(QKeyEvent *keyEvent) override;
|
||||
|
||||
int shortcut() const { return m_shortcut; }
|
||||
void setShortcut(int shortcut) { m_shortcut = shortcut; }
|
||||
|
||||
signals:
|
||||
void clicked();
|
||||
|
||||
private:
|
||||
int m_shortcut;
|
||||
};
|
||||
|
||||
#define PROPAGATE_EVENT(windowHandler, eventClass, itemHandler) \
|
||||
void windowHandler(eventClass *e) override \
|
||||
{ \
|
||||
foreach (Item *i, m_items) \
|
||||
i->itemHandler(e); \
|
||||
}
|
||||
|
||||
class ItemWindow : public QRasterWindow {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ItemWindow(QWindow *parent = nullptr) : QRasterWindow(parent), m_background(Qt::white) {}
|
||||
|
||||
void addItem(Item *item) { m_items.append(item); }
|
||||
|
||||
QColor background() const { return m_background; }
|
||||
void setBackground(const QColor &background) { m_background = background; }
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *) override;
|
||||
PROPAGATE_EVENT(mousePressEvent, QMouseEvent, mouseEvent)
|
||||
PROPAGATE_EVENT(mouseReleaseEvent, QMouseEvent, mouseEvent)
|
||||
PROPAGATE_EVENT(mouseDoubleClickEvent, QMouseEvent, mouseEvent)
|
||||
PROPAGATE_EVENT(mouseMoveEvent, QMouseEvent, mouseEvent)
|
||||
PROPAGATE_EVENT(keyPressEvent, QKeyEvent, keyEvent)
|
||||
PROPAGATE_EVENT(keyReleaseEvent, QKeyEvent, keyEvent)
|
||||
|
||||
private:
|
||||
QList<Item *> m_items;
|
||||
QColor m_background;
|
||||
};
|
||||
|
||||
#endif // ITEMWINDOW_H
|
254
tests/manual/embeddedintoforeignwindow/main.cpp
Normal file
254
tests/manual/embeddedintoforeignwindow/main.cpp
Normal file
@ -0,0 +1,254 @@
|
||||
// Copyright (C) 2017 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "itemwindow.h"
|
||||
|
||||
#include <QtGui/QGuiApplication>
|
||||
|
||||
#include <QtCore/QCommandLineOption>
|
||||
#include <QtCore/QCommandLineParser>
|
||||
#include <QtCore/QDebug>
|
||||
#include <QtCore/QStringList>
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
# include <qpa/qplatformnativeinterface.h>
|
||||
# include <QtCore/QMetaObject>
|
||||
# include <QtCore/qt_windows.h>
|
||||
#endif
|
||||
|
||||
#include <eventfilter.h> // diaglib
|
||||
#include <qwindowdump.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
QT_USE_NAMESPACE
|
||||
|
||||
static const char usage[] =
|
||||
"\nEmbeds a QWindow into a native foreign window passed on the command line.\n"
|
||||
"When no window ID is passed, a test window is created (Windows only).";
|
||||
|
||||
static QString windowTitle()
|
||||
{
|
||||
return QLatin1String(QT_VERSION_STR) + QLatin1Char(' ') + QGuiApplication::platformName();
|
||||
}
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
// Helper to create a native test window (Windows)
|
||||
static QString registerWindowClass(const QString &name)
|
||||
{
|
||||
QString result;
|
||||
void *proc = DefWindowProc;
|
||||
QPlatformNativeInterface *ni = QGuiApplication::platformNativeInterface();
|
||||
if (!QMetaObject::invokeMethod(ni, "registerWindowClass", Qt::DirectConnection,
|
||||
Q_RETURN_ARG(QString, result),
|
||||
Q_ARG(QString, name),
|
||||
Q_ARG(void *, proc))) {
|
||||
qWarning("registerWindowClass failed");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static HWND createNativeWindow(const QString &name)
|
||||
{
|
||||
const HWND hwnd =
|
||||
CreateWindowEx(0, reinterpret_cast<const wchar_t *>(name.utf16()),
|
||||
L"NativeWindow", WS_OVERLAPPEDWINDOW,
|
||||
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
0, 0, GetModuleHandle(NULL), NULL);
|
||||
|
||||
if (!hwnd) {
|
||||
qErrnoWarning("Cannot create window \"%s\"", qPrintable(name));
|
||||
return 0;
|
||||
}
|
||||
|
||||
const QString text = windowTitle() + QLatin1String(" 0x") + QString::number(quint64(hwnd), 16);
|
||||
SetWindowText(hwnd, reinterpret_cast<const wchar_t *>(text.utf16()));
|
||||
return hwnd;
|
||||
}
|
||||
#endif // Q_OS_WIN
|
||||
|
||||
// Helper functions for simple management of native windows.
|
||||
static WId createNativeTestWindow()
|
||||
{
|
||||
WId result = 0;
|
||||
#ifdef Q_OS_WIN
|
||||
const QString className = registerWindowClass(QLatin1String("TestClass") + windowTitle());
|
||||
const HWND nativeWin = createNativeWindow(className);
|
||||
result = WId(nativeWin);
|
||||
#else // Q_OS_WIN
|
||||
Q_UNIMPLEMENTED();
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
static void showNativeWindow(WId wid)
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
ShowWindow(HWND(wid), SW_SHOW);
|
||||
#else // Q_OS_WIN
|
||||
Q_UNUSED(wid);
|
||||
Q_UNIMPLEMENTED();
|
||||
#endif
|
||||
}
|
||||
|
||||
static void setFocusToNativeWindow(WId wid)
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
SetFocus(HWND(wid));
|
||||
#else // Q_OS_WIN
|
||||
Q_UNUSED(wid);
|
||||
Q_UNIMPLEMENTED();
|
||||
#endif
|
||||
}
|
||||
|
||||
static void destroyNativeWindow(WId wid)
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
DestroyWindow(HWND(wid));
|
||||
#else // Q_OS_WIN
|
||||
Q_UNUSED(wid);
|
||||
Q_UNIMPLEMENTED();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Main test window to be embedded into foreign window with some buttons.
|
||||
class EmbeddedTestWindow : public ItemWindow {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit EmbeddedTestWindow(QWindow *parent = nullptr) : ItemWindow(parent)
|
||||
{
|
||||
const int spacing = 10;
|
||||
const QSize buttonSize(100, 30);
|
||||
const int width = 3 * buttonSize.width() + 4 * spacing;
|
||||
|
||||
QPoint pos(spacing, spacing);
|
||||
addItem(new TextItem(::windowTitle(), QRect(pos, QSize(width - 2 * spacing, buttonSize.height())),
|
||||
Qt::white));
|
||||
|
||||
pos.ry() += 2 * spacing + buttonSize.height();
|
||||
|
||||
ButtonItem *mgi = new ButtonItem("Map to global", QRect(pos, buttonSize),
|
||||
QColor(Qt::yellow).lighter(), this);
|
||||
connect(mgi, &ButtonItem::clicked, this, &EmbeddedTestWindow::testMapToGlobal);
|
||||
addItem(mgi);
|
||||
|
||||
pos.rx() += buttonSize.width() + spacing;
|
||||
ButtonItem *di = new ButtonItem("Dump Wins", QRect(pos, buttonSize),
|
||||
QColor(Qt::cyan).lighter(), this);
|
||||
connect(di, &ButtonItem::clicked, this, [] () { QtDiag::dumpAllWindows(); });
|
||||
addItem(di);
|
||||
|
||||
pos.rx() += buttonSize.width() + spacing;
|
||||
ButtonItem *qi = new ButtonItem("Quit", QRect(pos, buttonSize),
|
||||
QColor(Qt::red).lighter(), this);
|
||||
qi->setShortcut(Qt::CTRL | Qt::Key_Q);
|
||||
connect(qi, &ButtonItem::clicked, qApp, &QCoreApplication::quit);
|
||||
addItem(qi);
|
||||
|
||||
setBackground(Qt::lightGray);
|
||||
resize(width, pos.y() + buttonSize.height() + spacing);
|
||||
}
|
||||
|
||||
public slots:
|
||||
void testMapToGlobal();
|
||||
};
|
||||
|
||||
void EmbeddedTestWindow::testMapToGlobal()
|
||||
{
|
||||
const QPoint globalPos = mapToGlobal(QPoint(0,0));
|
||||
qDebug() << "mapToGlobal(QPoint(0,0)" << globalPos
|
||||
<< "cursor at:" << QCursor::pos();
|
||||
}
|
||||
|
||||
struct EventFilterOption
|
||||
{
|
||||
const char *name;
|
||||
const char *description;
|
||||
QtDiag::EventFilter::EventCategories categories;
|
||||
};
|
||||
|
||||
EventFilterOption eventFilterOptions[] = {
|
||||
{"mouse-events", "Dump mouse events.", QtDiag::EventFilter::MouseEvents},
|
||||
{"keyboard-events", "Dump keyboard events.", QtDiag::EventFilter::KeyEvents},
|
||||
{"state-events", "Dump state/focus change events.", QtDiag::EventFilter::StateChangeEvents | QtDiag::EventFilter::FocusEvents}
|
||||
};
|
||||
|
||||
static inline bool isOptionSet(int argc, char *argv[], const char *option)
|
||||
{
|
||||
return (argv + argc) !=
|
||||
std::find_if(argv + 1, argv + argc,
|
||||
[option] (const char *arg) { return !qstrcmp(arg, option); });
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication::setApplicationVersion(QLatin1String(QT_VERSION_STR));
|
||||
QGuiApplication::setApplicationDisplayName("Foreign Window Embedding Tester");
|
||||
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
// Process command line
|
||||
QCommandLineParser parser;
|
||||
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
|
||||
parser.setApplicationDescription(QLatin1String(usage));
|
||||
parser.addHelpOption();
|
||||
parser.addVersionOption();
|
||||
QCommandLineOption noScalingDummy(QStringLiteral("s"),
|
||||
QStringLiteral("Disable High DPI scaling."));
|
||||
parser.addOption(noScalingDummy);
|
||||
const int eventFilterOptionCount = int(sizeof(eventFilterOptions) / sizeof(eventFilterOptions[0]));
|
||||
for (int i = 0; i < eventFilterOptionCount; ++i) {
|
||||
parser.addOption(QCommandLineOption(QLatin1String(eventFilterOptions[i].name),
|
||||
QLatin1String(eventFilterOptions[i].description)));
|
||||
}
|
||||
parser.addPositionalArgument(QStringLiteral("[windows]"), QStringLiteral("Window ID."));
|
||||
|
||||
parser.process(QCoreApplication::arguments());
|
||||
|
||||
QtDiag::EventFilter::EventCategories eventCategories = {};
|
||||
for (int i = 0; i < eventFilterOptionCount; ++i) {
|
||||
if (parser.isSet(QLatin1String(eventFilterOptions[i].name)))
|
||||
eventCategories |= eventFilterOptions[i].categories;
|
||||
}
|
||||
if (eventCategories)
|
||||
app.installEventFilter(new QtDiag::EventFilter(eventCategories, &app));
|
||||
|
||||
// Obtain foreign window to test with.
|
||||
WId testForeignWinId = 0;
|
||||
bool createdTestWindow = false;
|
||||
if (parser.positionalArguments().isEmpty()) {
|
||||
testForeignWinId = createNativeTestWindow();
|
||||
if (!testForeignWinId)
|
||||
parser.showHelp(-1);
|
||||
showNativeWindow(testForeignWinId);
|
||||
createdTestWindow = true;
|
||||
} else {
|
||||
bool ok;
|
||||
const QString &winIdArgument = parser.positionalArguments().constFirst();
|
||||
testForeignWinId = winIdArgument.toULongLong(&ok, 0);
|
||||
if (!ok) {
|
||||
std::cerr << "Invalid window id: \"" << qPrintable(winIdArgument) << "\"\n";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (!testForeignWinId)
|
||||
parser.showHelp(1);
|
||||
|
||||
QWindow *foreignWindow = QWindow::fromWinId(testForeignWinId);
|
||||
if (!foreignWindow)
|
||||
return -2;
|
||||
foreignWindow->setObjectName("ForeignWindow");
|
||||
EmbeddedTestWindow *embeddedTestWindow = new EmbeddedTestWindow(foreignWindow);
|
||||
embeddedTestWindow->setObjectName("EmbeddedTestWindow");
|
||||
embeddedTestWindow->show();
|
||||
setFocusToNativeWindow(embeddedTestWindow->winId()); // Windows: Set keyboard focus.
|
||||
|
||||
const int exitCode = app.exec();
|
||||
delete embeddedTestWindow;
|
||||
delete foreignWindow;
|
||||
if (createdTestWindow)
|
||||
destroyNativeWindow(testForeignWinId);
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
#include "main.moc"
|
Reference in New Issue
Block a user