1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/popup/popup.doc:4 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Popup Widgets</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Popup Widgets</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example shows how to implement widgets that should
|
---|
37 | pop up.
|
---|
38 | <p> <hr>
|
---|
39 | <p> Header file:
|
---|
40 | <p> <pre>/****************************************************************************
|
---|
41 | ** $Id: popup-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
42 | **
|
---|
43 | ** Definition of something or other
|
---|
44 | **
|
---|
45 | ** Created : 979899
|
---|
46 | **
|
---|
47 | ** Copyright (C) 1997-2007 Trolltech ASA. All rights reserved.
|
---|
48 | **
|
---|
49 | ** This file is part of an example program for Qt. This example
|
---|
50 | ** program may be used, distributed and modified without limitation.
|
---|
51 | **
|
---|
52 | *****************************************************************************/
|
---|
53 |
|
---|
54 | #ifndef POPUP_H
|
---|
55 | #define POPUP_H
|
---|
56 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
57 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
58 | #include <<a href="qlineedit-h.html">qlineedit.h</a>>
|
---|
59 |
|
---|
60 | class FancyPopup : public <a href="qlabel.html">QLabel</a>
|
---|
61 | {
|
---|
62 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
63 | public:
|
---|
64 | FancyPopup( <a href="qwidget.html">QWidget</a>* parent = 0, const char* name=0);
|
---|
65 |
|
---|
66 | void popup( <a href="qwidget.html">QWidget</a>* parent = 0);
|
---|
67 | protected:
|
---|
68 | virtual void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
|
---|
69 | virtual void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
|
---|
70 | virtual void closeEvent( <a href="qcloseevent.html">QCloseEvent</a> * );
|
---|
71 |
|
---|
72 | private:
|
---|
73 | <a href="qwidget.html">QWidget</a>* popupParent;
|
---|
74 | int moves;
|
---|
75 | };
|
---|
76 |
|
---|
77 |
|
---|
78 | class Frame : public <a href="qframe.html">QFrame</a>
|
---|
79 | {
|
---|
80 | Q_OBJECT
|
---|
81 | public:
|
---|
82 | Frame( <a href="qwidget.html">QWidget</a> *parent=0, const char* name=0);
|
---|
83 |
|
---|
84 | protected:
|
---|
85 |
|
---|
86 | private slots:
|
---|
87 | void button1Clicked();
|
---|
88 | void button2Pressed();
|
---|
89 |
|
---|
90 | private:
|
---|
91 | <a href="qpushbutton.html">QPushButton</a> *button1;
|
---|
92 | <a href="qpushbutton.html">QPushButton</a> *button2;
|
---|
93 |
|
---|
94 | <a href="qframe.html">QFrame</a>* popup1;
|
---|
95 | FancyPopup* popup2;
|
---|
96 | };
|
---|
97 |
|
---|
98 | #endif
|
---|
99 | </pre>
|
---|
100 |
|
---|
101 | <p> <hr>
|
---|
102 | <p> Implementation:
|
---|
103 | <p> <pre>/****************************************************************************
|
---|
104 | ** $Id: popup-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
105 | **
|
---|
106 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
107 | **
|
---|
108 | ** This file is part of an example program for Qt. This example
|
---|
109 | ** program may be used, distributed and modified without limitation.
|
---|
110 | **
|
---|
111 | *****************************************************************************/
|
---|
112 |
|
---|
113 | #include "popup.h"
|
---|
114 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
115 | #include <<a href="qlayout-h.html">qlayout.h</a>>
|
---|
116 |
|
---|
117 | <a name="f468"></a>FancyPopup::FancyPopup( <a href="qwidget.html">QWidget</a>* parent, const char* name ):
|
---|
118 | <a href="qlabel.html">QLabel</a>( parent, name, WType_Popup ){
|
---|
119 | <a href="qframe.html#setFrameStyle">setFrameStyle</a>( WinPanel|Raised );
|
---|
120 | <a href="qlabel.html#setAlignment">setAlignment</a>( AlignCenter );
|
---|
121 | <a href="qwidget.html#resize">resize</a>(150,100);
|
---|
122 | moves = 0;
|
---|
123 | <a href="qwidget.html#setMouseTracking">setMouseTracking</a>( TRUE );
|
---|
124 | }
|
---|
125 |
|
---|
126 | <a name="x1620"></a>void FancyPopup::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * e){
|
---|
127 | moves++;
|
---|
128 | <a href="qstring.html">QString</a> s;
|
---|
129 | <a name="x1611"></a> s.<a href="qstring.html#sprintf">sprintf</a>("%d/%d", e-><a href="qmouseevent.html#pos">pos</a>().x(), e-><a href="qmouseevent.html#pos">pos</a>().y());
|
---|
130 | <a name="x1612"></a> if (e-><a href="qmouseevent.html#state">state</a>() & QMouseEvent::LeftButton)
|
---|
131 | s += " (down)";
|
---|
132 | <a href="qlabel.html#setText">setText</a>(s);
|
---|
133 | }
|
---|
134 |
|
---|
135 | <a name="x1621"></a>void FancyPopup::<a href="qwidget.html#mouseReleaseEvent">mouseReleaseEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * e){
|
---|
136 | if (<a href="qwidget.html#rect">rect</a>().contains( e-><a href="qmouseevent.html#pos">pos</a>() ) || moves > 5)
|
---|
137 | <a href="qwidget.html#close">close</a>();
|
---|
138 | }
|
---|
139 |
|
---|
140 | <a name="x1617"></a>void FancyPopup::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a> *e ){
|
---|
141 | <a name="x1607"></a> e-><a href="qcloseevent.html#accept">accept</a>();
|
---|
142 | moves = 0;
|
---|
143 | if (!popupParent)
|
---|
144 | return;
|
---|
145 |
|
---|
146 | // remember that we (as a popup) might recieve the mouse release
|
---|
147 | // event instead of the popupParent. This is due to the fact that
|
---|
148 | // the popupParent popped us up in its mousePressEvent handler. To
|
---|
149 | // avoid the button remaining in pressed state we simply send a
|
---|
150 | // faked mouse button release event to it.
|
---|
151 | <a href="qmouseevent.html">QMouseEvent</a> me( QEvent::MouseButtonRelease, QPoint(0,0), QPoint(0,0), QMouseEvent::LeftButton, QMouseEvent::NoButton);
|
---|
152 | <a name="x1602"></a> QApplication::<a href="qapplication.html#sendEvent">sendEvent</a>( popupParent, &me );
|
---|
153 | }
|
---|
154 |
|
---|
155 | void <a name="f469"></a>FancyPopup::popup( <a href="qwidget.html">QWidget</a>* parent) {
|
---|
156 | popupParent = parent;
|
---|
157 | setText("Move the mouse!");
|
---|
158 | if (popupParent)
|
---|
159 | <a href="qwidget.html#move">move</a>( popupParent->mapToGlobal( popupParent->rect().bottomLeft() ) );
|
---|
160 | <a href="qwidget.html#show">show</a>();
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 |
|
---|
166 |
|
---|
167 |
|
---|
168 | <a name="f470"></a>Frame::Frame(QWidget* parent, const char* name): <a href="qframe.html">QFrame</a>(parent, name){
|
---|
169 | button1 = new <a href="qpushbutton.html">QPushButton</a>("Simple Popup", this);
|
---|
170 | <a href="qobject.html#connect">connect</a> ( button1, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), SLOT( button1Clicked() ) );
|
---|
171 | button2 = new <a href="qpushbutton.html">QPushButton</a>("Fancy Popup", this);
|
---|
172 | <a name="x1606"></a> <a href="qobject.html#connect">connect</a> ( button2, SIGNAL( <a href="qbutton.html#pressed">pressed</a>() ), SLOT( button2Pressed() ) );
|
---|
173 |
|
---|
174 | <a href="qboxlayout.html">QBoxLayout</a> * l = new <a href="qhboxlayout.html">QHBoxLayout</a>( this );
|
---|
175 | <a name="x1627"></a><a name="x1614"></a> button1-><a href="qwidget.html#setMaximumSize">setMaximumSize</a>(button1-><a href="qwidget.html#sizeHint">sizeHint</a>());
|
---|
176 | button2-><a href="qwidget.html#setMaximumSize">setMaximumSize</a>(button2-><a href="qwidget.html#sizeHint">sizeHint</a>());
|
---|
177 | l-><a href="qboxlayout.html#addWidget">addWidget</a>( button1 );
|
---|
178 | l-><a href="qboxlayout.html#addWidget">addWidget</a>( button2 );
|
---|
179 | <a name="x1609"></a> l-><a href="qlayout.html#activate">activate</a>();
|
---|
180 |
|
---|
181 | <a name="x1613"></a>// button1-><a href="qwidget.html#setGeometry">setGeometry</a>(20,20,100,30);
|
---|
182 | // button2-><a href="qwidget.html#setGeometry">setGeometry</a>(140,20,100,30);
|
---|
183 | <a href="qwidget.html#resize">resize</a>(270, 70);
|
---|
184 |
|
---|
185 | //create a very simple popup: it is just composed with other
|
---|
186 | //widget and will be shown after clicking on button1
|
---|
187 |
|
---|
188 | popup1 = new <a href="qframe.html">QFrame</a>( this ,0, WType_Popup);
|
---|
189 | popup1-><a href="qframe.html#setFrameStyle">setFrameStyle</a>( WinPanel|Raised );
|
---|
190 | popup1-><a href="qwidget.html#resize">resize</a>(150,100);
|
---|
191 | <a href="qlineedit.html">QLineEdit</a> *tmpE = new <a href="qlineedit.html">QLineEdit</a>( popup1 );
|
---|
192 | <a name="x1619"></a><a name="x1610"></a> <a href="qobject.html#connect">connect</a>( tmpE, SIGNAL( <a href="qlineedit.html#returnPressed">returnPressed</a>() ), popup1, SLOT( <a href="qwidget.html#hide">hide</a>() ) );
|
---|
193 | <a name="x1626"></a> tmpE-><a href="qwidget.html#setGeometry">setGeometry</a>(10,10, 130, 30);
|
---|
194 | <a name="x1625"></a> tmpE-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
195 | <a href="qpushbutton.html">QPushButton</a> *tmpB = new <a href="qpushbutton.html">QPushButton</a>("Click me!", popup1);
|
---|
196 | <a name="x1616"></a> <a href="qobject.html#connect">connect</a>( tmpB, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), popup1, SLOT( <a href="qwidget.html#close">close</a>() ) );
|
---|
197 | tmpB-><a href="qwidget.html#setGeometry">setGeometry</a>(10, 50, 130, 30);
|
---|
198 |
|
---|
199 | // the fancier version uses its own class. It will be shown when
|
---|
200 | // pressing button2, so they behavior is more like a modern menu
|
---|
201 | // or toolbar.
|
---|
202 |
|
---|
203 | popup2 = new FancyPopup( this );
|
---|
204 |
|
---|
205 | // you might also add new widgets to the popup, just like you do
|
---|
206 | // it with any other widget. The next four lines (if not
|
---|
207 | // commented out) will for instance add a line edit widget.
|
---|
208 |
|
---|
209 | // tmpE = new <a href="qlineedit.html">QLineEdit</a>( popup2 );
|
---|
210 | // tmpE-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
211 | // connect( tmpE, SIGNAL( <a href="qlineedit.html#returnPressed">returnPressed</a>() ), popup2, SLOT( <a href="qwidget.html#close">close</a>() ) );
|
---|
212 | // tmpE-><a href="qwidget.html#setGeometry">setGeometry</a>(10, 10, 130, 30);
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | void <a name="f471"></a>Frame::button1Clicked(){
|
---|
217 | <a name="x1622"></a><a name="x1618"></a> popup1-><a href="qwidget.html#move">move</a>( <a href="qwidget.html#mapToGlobal">mapToGlobal</a>( button1-><a href="qwidget.html#geometry">geometry</a>().bottomLeft() ) );
|
---|
218 | <a name="x1628"></a> popup1-><a href="qwidget.html#show">show</a>();
|
---|
219 | }
|
---|
220 |
|
---|
221 | void <a name="f472"></a>Frame::button2Pressed(){
|
---|
222 | popup2->popup(button2);
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | int main( int argc, char **argv )
|
---|
227 | {
|
---|
228 | <a href="qapplication.html">QApplication</a> a(argc,argv);
|
---|
229 |
|
---|
230 | Frame frame;
|
---|
231 | frame.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Custom Popups");
|
---|
232 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>(&frame);
|
---|
233 | frame.<a href="qwidget.html#show">show</a>();
|
---|
234 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
235 | }
|
---|
236 | </pre>
|
---|
237 |
|
---|
238 | <p>See also <a href="examples.html">Examples</a>.
|
---|
239 |
|
---|
240 | <!-- eof -->
|
---|
241 | <p><address><hr><div align=center>
|
---|
242 | <table width=100% cellspacing=0 border=0><tr>
|
---|
243 | <td>Copyright © 2007
|
---|
244 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
245 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
246 | </table></div></address></body>
|
---|
247 | </html>
|
---|