| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QtWidgets> |
| 5 |
|
| 6 |
class Window : public QWidget |
| 7 |
{ |
| 8 |
Q_OBJECT |
| 9 |
|
| 10 |
public: |
| 11 |
Window(); |
| 12 |
|
| 13 |
public slots: |
| 14 |
void triggered(QAction*); |
| 15 |
void clean(); |
| 16 |
void showPoppWindow(); |
| 17 |
|
| 18 |
private: |
| 19 |
QLabel *explanation; |
| 20 |
QToolButton *toolButton; |
| 21 |
QMenu *menu; |
| 22 |
QLineEdit *echo; |
| 23 |
QComboBox *comboBox; |
| 24 |
QPushButton *pushButton; |
| 25 |
}; |
| 26 |
|
| 27 |
Window::Window() |
| 28 |
{ |
| 29 |
QGroupBox* group = new QGroupBox(tr("test the popup")); |
| 30 |
|
| 31 |
explanation = new QLabel( |
| 32 |
"This test is used to verify that popup windows will be closed " |
| 33 |
"as expected. This includes when clicking outside the popup or moving the " |
| 34 |
"parent window. Tested popups include context menus, combo box popups, tooltips " |
| 35 |
"and QWindow with Qt::Popup set." |
| 36 |
); |
| 37 |
explanation->setWordWrap(true); |
| 38 |
explanation->setToolTip("I'm a tool tip!"); |
| 39 |
|
| 40 |
menu = new QMenu(group); |
| 41 |
menu->addAction(tr("line one")); |
| 42 |
menu->addAction(tr("line two")); |
| 43 |
menu->addAction(tr("line three")); |
| 44 |
menu->addAction(tr("line four")); |
| 45 |
menu->addAction(tr("line five")); |
| 46 |
|
| 47 |
QMenu *subMenu1 = new QMenu(); |
| 48 |
subMenu1->addAction("1"); |
| 49 |
subMenu1->addAction("2"); |
| 50 |
subMenu1->addAction("3"); |
| 51 |
menu->addMenu(subMenu1); |
| 52 |
|
| 53 |
QMenu *subMenu2 = new QMenu(); |
| 54 |
subMenu2->addAction("2 1"); |
| 55 |
subMenu2->addAction("2 2"); |
| 56 |
subMenu2->addAction("2 3"); |
| 57 |
menu->addMenu(subMenu2); |
| 58 |
|
| 59 |
toolButton = new QToolButton(group); |
| 60 |
toolButton->setMenu(menu); |
| 61 |
toolButton->setPopupMode( QToolButton::MenuButtonPopup ); |
| 62 |
toolButton->setText("select me"); |
| 63 |
|
| 64 |
echo = new QLineEdit(group); |
| 65 |
echo->setPlaceholderText("not triggered"); |
| 66 |
|
| 67 |
connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(triggered(QAction*))); |
| 68 |
connect(menu, SIGNAL(aboutToShow()), this, SLOT(clean())); |
| 69 |
|
| 70 |
comboBox = new QComboBox(); |
| 71 |
comboBox->addItem("Item 1"); |
| 72 |
comboBox->addItem("Item 2"); |
| 73 |
comboBox->addItem("Item 3"); |
| 74 |
|
| 75 |
pushButton = new QPushButton("Show popup window"); |
| 76 |
connect(pushButton, SIGNAL(clicked()), this, SLOT(showPoppWindow())); |
| 77 |
|
| 78 |
QVBoxLayout* layout = new QVBoxLayout; |
| 79 |
layout->addWidget(explanation); |
| 80 |
layout->addWidget(toolButton); |
| 81 |
layout->addWidget(echo); |
| 82 |
layout->addWidget(comboBox); |
| 83 |
layout->addWidget(pushButton); |
| 84 |
|
| 85 |
group ->setLayout(layout); |
| 86 |
setLayout(layout); |
| 87 |
setWindowTitle(tr("Popup Window Testing")); |
| 88 |
} |
| 89 |
|
| 90 |
void Window::clean() |
| 91 |
{ |
| 92 |
echo->setText(""); |
| 93 |
} |
| 94 |
|
| 95 |
void Window::showPoppWindow() |
| 96 |
{ |
| 97 |
QWindow *window = new QWindow(); |
| 98 |
window->setTransientParent(this->windowHandle()); |
| 99 |
window->setPosition(this->pos()); |
| 100 |
window->setWidth(100); |
| 101 |
window->setHeight(100); |
| 102 |
window->setFlags(Qt::Window | Qt::Popup); |
| 103 |
window->show(); |
| 104 |
} |
| 105 |
|
| 106 |
void Window::triggered(QAction* act) |
| 107 |
{ |
| 108 |
if (!act) |
| 109 |
return; |
| 110 |
echo->setText(act->text()); |
| 111 |
} |
| 112 |
|
| 113 |
int main(int argc, char *argv[]) |
| 114 |
{ |
| 115 |
QApplication app(argc, argv); |
| 116 |
|
| 117 |
Window window; |
| 118 |
window.show(); |
| 119 |
return app.exec(); |
| 120 |
} |
| 121 |
|
| 122 |
#include "main.moc" |