summaryrefslogtreecommitdiffstats
path: root/doc/html/popup-example.html
blob: ec60e3c01520e0679e5b394cf27a4a10dec7ed9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/popup/popup.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Popup Widgets</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>

   
<p> 
This example shows how to implement widgets that should
pop up.
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id: qt/popup.h   3.3.8   edited Jan 11 14:46 $
**
** Definition of something or other
**
** Created : 979899
**
** Copyright (C) 1997-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef POPUP_H
#define POPUP_H
#include &lt;<a href="qlabel-h.html">ntqlabel.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">ntqpushbutton.h</a>&gt;
#include &lt;<a href="qlineedit-h.html">ntqlineedit.h</a>&gt;

class FancyPopup : public <a href="ntqlabel.html">TQLabel</a>
{
    <a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>
public:
    FancyPopup( <a href="ntqwidget.html">TQWidget</a>* parent = 0, const char*  name=0);

    void popup( <a href="ntqwidget.html">TQWidget</a>* parent = 0);
protected:
    virtual void mouseMoveEvent( <a href="qmouseevent.html">TQMouseEvent</a> * );
    virtual void mouseReleaseEvent( <a href="qmouseevent.html">TQMouseEvent</a> * );
    virtual void closeEvent( <a href="qcloseevent.html">TQCloseEvent</a> * );

private:
    <a href="ntqwidget.html">TQWidget</a>* popupParent;
    int moves;
};


 class Frame : public <a href="ntqframe.html">TQFrame</a>
 {
     TQ_OBJECT
 public:
     Frame( <a href="ntqwidget.html">TQWidget</a> *parent=0, const char*  name=0);

 protected:

 private slots:
     void button1Clicked();
     void button2Pressed();

 private:
     <a href="ntqpushbutton.html">TQPushButton</a> *button1;
     <a href="ntqpushbutton.html">TQPushButton</a> *button2;

     <a href="ntqframe.html">TQFrame</a>* popup1;
     FancyPopup* popup2;
 };

#endif
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/popup.cpp   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "popup.h"
#include &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;
#include &lt;<a href="qlayout-h.html">ntqlayout.h</a>&gt;

<a name="f468"></a>FancyPopup::FancyPopup( <a href="ntqwidget.html">TQWidget</a>* parent, const char*  name ):
    <a href="ntqlabel.html">TQLabel</a>( parent, name, WType_Popup ){
        <a href="ntqframe.html#setFrameStyle">setFrameStyle</a>( WinPanel|Raised );
        <a href="ntqlabel.html#setAlignment">setAlignment</a>( AlignCenter );
        <a href="ntqwidget.html#resize">resize</a>(150,100);
        moves = 0;
        <a href="ntqwidget.html#setMouseTracking">setMouseTracking</a>( TRUE );
}

<a name="x1620"></a>void FancyPopup::<a href="ntqwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">TQMouseEvent</a> * e){
    moves++;
    <a href="ntqstring.html">TQString</a> s;
<a name="x1611"></a>    s.<a href="ntqstring.html#sprintf">sprintf</a>("%d/%d", e-&gt;<a href="qmouseevent.html#pos">pos</a>().x(), e-&gt;<a href="qmouseevent.html#pos">pos</a>().y());
<a name="x1612"></a>    if (e-&gt;<a href="qmouseevent.html#state">state</a>() &amp; TQMouseEvent::LeftButton)
        s += " (down)";
    <a href="ntqlabel.html#setText">setText</a>(s);
}

<a name="x1621"></a>void FancyPopup::<a href="ntqwidget.html#mouseReleaseEvent">mouseReleaseEvent</a>( <a href="qmouseevent.html">TQMouseEvent</a> * e){
    if  (<a href="ntqwidget.html#rect">rect</a>().contains( e-&gt;<a href="qmouseevent.html#pos">pos</a>() ) || moves &gt; 5)
        <a href="ntqwidget.html#close">close</a>();
}

<a name="x1617"></a>void FancyPopup::<a href="ntqwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">TQCloseEvent</a> *e ){
<a name="x1607"></a>    e-&gt;<a href="qcloseevent.html#accept">accept</a>();
    moves = 0;
    if (!popupParent)
        return;

    // remember that we (as a popup) might recieve the mouse release
    // event instead of the popupParent. This is due to the fact that
    // the popupParent popped us up in its mousePressEvent handler. To
    // avoid the button remaining in pressed state we simply send a
    // faked mouse button release event to it.
    <a href="qmouseevent.html">TQMouseEvent</a> me( TQEvent::MouseButtonRelease, TQPoint(0,0), TQPoint(0,0), TQMouseEvent::LeftButton, TQMouseEvent::NoButton);
<a name="x1602"></a>    TQApplication::<a href="ntqapplication.html#sendEvent">sendEvent</a>( popupParent, &amp;me );
}

void <a name="f469"></a>FancyPopup::popup( <a href="ntqwidget.html">TQWidget</a>* parent) {
    popupParent = parent;
    setText("Move the mouse!");
    if (popupParent)
        <a href="ntqwidget.html#move">move</a>( popupParent-&gt;mapToGlobal( popupParent-&gt;rect().bottomLeft() ) );
    <a href="ntqwidget.html#show">show</a>();
}






<a name="f470"></a>Frame::Frame(TQWidget* parent, const char* name): <a href="ntqframe.html">TQFrame</a>(parent, name){
    button1 = new <a href="ntqpushbutton.html">TQPushButton</a>("Simple Popup", this);
    <a href="ntqobject.html#connect">connect</a> ( button1, SIGNAL( <a href="ntqbutton.html#clicked">clicked</a>() ), SLOT( button1Clicked() ) );
    button2 = new <a href="ntqpushbutton.html">TQPushButton</a>("Fancy Popup", this);
<a name="x1606"></a>    <a href="ntqobject.html#connect">connect</a> ( button2, SIGNAL( <a href="ntqbutton.html#pressed">pressed</a>() ), SLOT( button2Pressed() ) );

    <a href="qboxlayout.html">TQBoxLayout</a> * l = new <a href="qhboxlayout.html">TQHBoxLayout</a>( this );
<a name="x1627"></a><a name="x1614"></a>    button1-&gt;<a href="ntqwidget.html#setMaximumSize">setMaximumSize</a>(button1-&gt;<a href="ntqwidget.html#sizeHint">sizeHint</a>());
    button2-&gt;<a href="ntqwidget.html#setMaximumSize">setMaximumSize</a>(button2-&gt;<a href="ntqwidget.html#sizeHint">sizeHint</a>());
    l-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( button1 );
    l-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( button2 );
<a name="x1609"></a>    l-&gt;<a href="ntqlayout.html#activate">activate</a>();

<a name="x1613"></a>//     button1-&gt;<a href="ntqwidget.html#setGeometry">setGeometry</a>(20,20,100,30);
//     button2-&gt;<a href="ntqwidget.html#setGeometry">setGeometry</a>(140,20,100,30);
    <a href="ntqwidget.html#resize">resize</a>(270, 70);

    //create a very simple popup: it is just composed with other
    //widget and will be shown after clicking on button1

    popup1 = new <a href="ntqframe.html">TQFrame</a>( this ,0, WType_Popup);
    popup1-&gt;<a href="ntqframe.html#setFrameStyle">setFrameStyle</a>( WinPanel|Raised );
    popup1-&gt;<a href="ntqwidget.html#resize">resize</a>(150,100);
    <a href="ntqlineedit.html">TQLineEdit</a> *tmpE = new <a href="ntqlineedit.html">TQLineEdit</a>( popup1 );
<a name="x1619"></a><a name="x1610"></a>    <a href="ntqobject.html#connect">connect</a>( tmpE, SIGNAL( <a href="ntqlineedit.html#returnPressed">returnPressed</a>() ), popup1, SLOT( <a href="ntqwidget.html#hide">hide</a>() ) );
<a name="x1626"></a>    tmpE-&gt;<a href="ntqwidget.html#setGeometry">setGeometry</a>(10,10, 130, 30);
<a name="x1625"></a>    tmpE-&gt;<a href="ntqwidget.html#setFocus">setFocus</a>();
    <a href="ntqpushbutton.html">TQPushButton</a> *tmpB = new <a href="ntqpushbutton.html">TQPushButton</a>("Click me!", popup1);
<a name="x1616"></a>    <a href="ntqobject.html#connect">connect</a>( tmpB, SIGNAL( <a href="ntqbutton.html#clicked">clicked</a>() ), popup1, SLOT( <a href="ntqwidget.html#close">close</a>() ) );
    tmpB-&gt;<a href="ntqwidget.html#setGeometry">setGeometry</a>(10, 50, 130, 30);

    // the fancier version uses its own class. It will be shown when
    // pressing button2, so they behavior is more like a modern menu
    // or toolbar.

    popup2 = new FancyPopup( this );

    // you might also add new widgets to the popup, just like you do
    // it with any other widget.  The next four lines (if not
    // commented out) will for instance add a line edit widget.

//     tmpE = new <a href="ntqlineedit.html">TQLineEdit</a>( popup2 );
//     tmpE-&gt;<a href="ntqwidget.html#setFocus">setFocus</a>();
//     connect( tmpE, SIGNAL( <a href="ntqlineedit.html#returnPressed">returnPressed</a>() ), popup2, SLOT( <a href="ntqwidget.html#close">close</a>() ) );
//     tmpE-&gt;<a href="ntqwidget.html#setGeometry">setGeometry</a>(10, 10, 130, 30);
}


void <a name="f471"></a>Frame::button1Clicked(){
<a name="x1622"></a><a name="x1618"></a>    popup1-&gt;<a href="ntqwidget.html#move">move</a>( <a href="ntqwidget.html#mapToGlobal">mapToGlobal</a>( button1-&gt;<a href="ntqwidget.html#geometry">geometry</a>().bottomLeft() ) );
<a name="x1628"></a>    popup1-&gt;<a href="ntqwidget.html#show">show</a>();
}

void <a name="f472"></a>Frame::button2Pressed(){
    popup2-&gt;popup(button2);
}


int main( int argc, char **argv )
{
    <a href="ntqapplication.html">TQApplication</a> a(argc,argv);

    Frame frame;
    frame.<a href="ntqwidget.html#setCaption">setCaption</a>("TQt Example - Custom Popups");
    a.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>(&amp;frame);
    frame.<a href="ntqwidget.html#show">show</a>();
    return a.<a href="ntqapplication.html#exec">exec</a>();
}
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>