QtBase  v6.3.1
main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 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 Window : public QWidget
32 {
33  Q_OBJECT
34 
35 public:
36  Window();
37 
38 public slots:
39  void triggered(QAction*);
40  void clean();
41  void showPoppWindow();
42 
43 private:
44  QLabel *explanation;
45  QToolButton *toolButton;
46  QMenu *menu;
47  QLineEdit *echo;
48  QComboBox *comboBox;
49  QPushButton *pushButton;
50 };
51 
53 {
54  QGroupBox* group = new QGroupBox(tr("test the popup"));
55 
56  explanation = new QLabel(
57  "This test is used to verify that popup windows will be closed "
58  "as expected. This includes when clicking outside the popup or moving the "
59  "parent window. Tested popups include context menus, combo box popups, tooltips "
60  "and QWindow with Qt::Popup set."
61  );
62  explanation->setWordWrap(true);
63  explanation->setToolTip("I'm a tool tip!");
64 
65  menu = new QMenu(group);
66  menu->addAction(tr("line one"));
67  menu->addAction(tr("line two"));
68  menu->addAction(tr("line three"));
69  menu->addAction(tr("line four"));
70  menu->addAction(tr("line five"));
71 
72  QMenu *subMenu1 = new QMenu();
73  subMenu1->addAction("1");
74  subMenu1->addAction("2");
75  subMenu1->addAction("3");
76  menu->addMenu(subMenu1);
77 
78  QMenu *subMenu2 = new QMenu();
79  subMenu2->addAction("2 1");
80  subMenu2->addAction("2 2");
81  subMenu2->addAction("2 3");
82  menu->addMenu(subMenu2);
83 
84  toolButton = new QToolButton(group);
85  toolButton->setMenu(menu);
86  toolButton->setPopupMode( QToolButton::MenuButtonPopup );
87  toolButton->setText("select me");
88 
89  echo = new QLineEdit(group);
90  echo->setPlaceholderText("not triggered");
91 
92  connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(triggered(QAction*)));
93  connect(menu, SIGNAL(aboutToShow()), this, SLOT(clean()));
94 
95  comboBox = new QComboBox();
96  comboBox->addItem("Item 1");
97  comboBox->addItem("Item 2");
98  comboBox->addItem("Item 3");
99 
100  pushButton = new QPushButton("Show popup window");
101  connect(pushButton, SIGNAL(clicked()), this, SLOT(showPoppWindow()));
102 
104  layout->addWidget(explanation);
105  layout->addWidget(toolButton);
106  layout->addWidget(echo);
107  layout->addWidget(comboBox);
108  layout->addWidget(pushButton);
109 
110  group ->setLayout(layout);
111  setLayout(layout);
112  setWindowTitle(tr("Popup Window Testing"));
113 }
114 
116 {
117  echo->setText("");
118 }
119 
121 {
122  QWindow *window = new QWindow();
123  window->setTransientParent(this->windowHandle());
124  window->setPosition(this->pos());
125  window->setWidth(100);
126  window->setHeight(100);
127  window->setFlags(Qt::Window | Qt::Popup);
128  window->show();
129 }
130 
132 {
133  if (!act)
134  return;
135  echo->setText(act->text());
136 }
137 
138 int main(int argc, char *argv[])
139 {
140  QApplication app(argc, argv);
141 
142  Window window;
143  window.show();
144  return app.exec();
145 }
146 
147 #include "main.moc"
void setText(const QString &text)
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition: qaction.h:65
QString text
the action's descriptive text
Definition: qaction.h:74
The QApplication class manages the GUI application's control flow and main settings.
Definition: qapplication.h:68
static int exec()
The QComboBox widget is a combined button and popup list.
Definition: qcombobox.h:60
void addItem(const QString &text, const QVariant &userData=QVariant())
Definition: qcombobox.h:260
friend class QPushButton
[1]
Definition: qdialog.h:57
void setLayout(QGraphicsLayout *layout)
void setWindowTitle(const QString &title)
QGraphicsLayout * layout
The layout of the widget.
The QGroupBox widget provides a group box frame with a title.
Definition: qgroupbox.h:53
The QLabel widget provides a text or image display.
Definition: qlabel.h:56
void setWordWrap(bool on)
Definition: qlabel.cpp:506
The QLineEdit widget is a one-line text editor.
Definition: qlineedit.h:64
void setPlaceholderText(const QString &)
Definition: qlineedit.cpp:346
void setText(const QString &)
Definition: qlineedit.cpp:316
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition: qmenu.h:62
QAction * addMenu(QMenu *menu)
Definition: qmenu.cpp:1871
void addAction(QAction *action)
Definition: qwidget.cpp:3129
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
The QPushButton widget provides a command button.
Definition: qpushbutton.h:56
The QToolButton class provides a quick-access button to commands or options, usually used inside a QT...
Definition: qtoolbutton.h:56
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:127
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
QWidget * window() const
Definition: qwidget.cpp:4319
QPoint pos
the position of the widget within its parent widget
Definition: qwidget.h:145
void show()
Definition: qwidget.cpp:7825
QWindow * windowHandle() const
Definition: qwidget.cpp:2495
The QWindow class represents a window in the underlying windowing system.
Definition: qwindow.h:99
void setWidth(int arg)
Definition: qwindow.cpp:1613
[Window class definition]
Definition: window.h:64
void triggered(QAction *)
Definition: main.cpp:131
void clean()
Definition: main.cpp:115
void showPoppWindow()
Definition: main.cpp:120
int main(int argc, char **argv)
Definition: main.cpp:1
@ Popup
Definition: qnamespace.h:236
@ Window
Definition: qnamespace.h:232
#define SLOT(a)
Definition: qobjectdefs.h:87
#define SIGNAL(a)
Definition: qobjectdefs.h:88
GLboolean GLuint group
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define slots
Definition: qtmetamacros.h:76
QString tr(const char *text)
Definition: main.cpp:53
QApplication app(argc, argv)
[0]
aWidget window() -> setWindowTitle("New Window Title")
[2]