File indexing completed on 2024-04-14 14:29:54

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2013 Aurélien Gâteau <agateau@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include <kwindowsystem.h>
0009 
0010 #include <QApplication>
0011 #include <QDialog>
0012 #include <QHBoxLayout>
0013 #include <QLabel>
0014 #include <QPushButton>
0015 
0016 #include "kx11extras.h"
0017 
0018 class Window : public QWidget
0019 {
0020 public:
0021     Window();
0022 
0023 private:
0024     void showWindow();
0025     QLabel *m_label;
0026 };
0027 
0028 Window::Window()
0029 {
0030     QPushButton *button = new QPushButton("Start Test");
0031     connect(button, &QPushButton::clicked, this, &Window::showWindow);
0032 
0033     m_label = new QLabel;
0034     m_label->setWordWrap(true);
0035 
0036     QVBoxLayout *layout = new QVBoxLayout(this);
0037     layout->addWidget(button);
0038     layout->addWidget(m_label);
0039 
0040     setMinimumSize(200, 150);
0041 }
0042 
0043 void Window::showWindow()
0044 {
0045     // Wait for user to select another window
0046     m_label->setText("Click on another window to show a dialog on it");
0047     WId us = winId();
0048     while (KX11Extras::activeWindow() == us) {
0049         QApplication::processEvents();
0050     }
0051 
0052     // Get the id of the selected window
0053     WId id = KX11Extras::activeWindow();
0054     m_label->setText(QString("Showing dialog on window with id: %1.").arg(id));
0055 
0056     // Create test dialog
0057     QDialog *dialog = new QDialog;
0058     dialog->setAttribute(Qt::WA_DeleteOnClose, true);
0059     QHBoxLayout *layout = new QHBoxLayout(dialog);
0060     layout->addWidget(new QLabel("Test Dialog.\nYou should not be able to bring the parent window on top of me."));
0061 
0062     // Show it
0063     dialog->setAttribute(Qt::WA_NativeWindow, true);
0064     KWindowSystem::setMainWindow(dialog->windowHandle(), id);
0065     dialog->exec();
0066 
0067     m_label->setText(QString());
0068 }
0069 
0070 int main(int argc, char **argv)
0071 {
0072     QApplication app(argc, argv);
0073     Window window;
0074     window.show();
0075     return app.exec();
0076 }