📄 popup-example.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/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"><!--h3.fn,span.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 Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped 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 shouldpop up.<p> <hr><p> Header file:<p> <pre>/****************************************************************************** $Id: qt/popup.h 3.0.5 edited Oct 12 2001 $**** Definition of something or other**** Created : 979899**** Copyright (C) 1997 by Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef POPUP_H#define POPUP_H#include <<a href="qlabel-h.html">qlabel.h</a>>#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>#include <<a href="qlineedit-h.html">qlineedit.h</a>>class FancyPopup : public <a href="qlabel.html">QLabel</a>{ <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>public: FancyPopup( <a href="qwidget.html">QWidget</a>* parent = 0, const char* name=0); void popup( <a href="qwidget.html">QWidget</a>* parent = 0);protected: virtual void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> * ); virtual void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * ); virtual void closeEvent( <a href="qcloseevent.html">QCloseEvent</a> * );private: <a href="qwidget.html">QWidget</a>* popupParent; int moves;}; class Frame : public <a href="qframe.html">QFrame</a> { Q_OBJECT public: Frame( <a href="qwidget.html">QWidget</a> *parent=0, const char* name=0); protected: private slots: void button1Clicked(); void button2Pressed(); private: <a href="qpushbutton.html">QPushButton</a> *button1; <a href="qpushbutton.html">QPushButton</a> *button2; <a href="qframe.html">QFrame</a>* popup1; FancyPopup* popup2; };#endif</pre><p> <hr><p> Implementation:<p> <pre>/****************************************************************************** $Id: qt/popup.cpp 3.0.5 edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "popup.h"#include <<a href="qapplication-h.html">qapplication.h</a>>#include <<a href="qlayout-h.html">qlayout.h</a>><a name="f461"></a>FancyPopup::FancyPopup( <a href="qwidget.html">QWidget</a>* parent, const char* name ): <a href="qlabel.html">QLabel</a>( parent, name, WType_Popup ){ <a href="qframe.html#setFrameStyle">setFrameStyle</a>( WinPanel|Raised ); <a href="qlabel.html#setAlignment">setAlignment</a>( AlignCenter ); <a href="qwidget.html#resize">resize</a>(150,100); moves = 0; <a href="qwidget.html#setMouseTracking">setMouseTracking</a>( TRUE );}<a name="x1586"></a>void FancyPopup::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * e){ moves++; <a href="qstring.html">QString</a> s;<a name="x1577"></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());<a name="x1578"></a> if (e-><a href="qmouseevent.html#state">state</a>() & QMouseEvent::LeftButton) s += " (down)"; <a href="qlabel.html#setText">setText</a>(s);}<a name="x1587"></a>void FancyPopup::<a href="qwidget.html#mouseReleaseEvent">mouseReleaseEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * e){ if (<a href="qwidget.html#rect">rect</a>().contains( e-><a href="qmouseevent.html#pos">pos</a>() ) || moves > 5) <a href="qwidget.html#close">close</a>();}<a name="x1583"></a>void FancyPopup::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a> *e ){<a name="x1573"></a> e-><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">QMouseEvent</a> me( QEvent::MouseButtonRelease, QPoint(0,0), QPoint(0,0), QMouseEvent::LeftButton, QMouseEvent::NoButton);<a name="x1568"></a> QApplication::<a href="qapplication.html#sendEvent">sendEvent</a>( popupParent, &me );}void <a name="f462"></a>FancyPopup::popup( <a href="qwidget.html">QWidget</a>* parent) { popupParent = parent; setText("Move the mouse!"); if (popupParent) <a href="qwidget.html#move">move</a>( popupParent->mapToGlobal( popupParent->rect().bottomLeft() ) ); <a href="qwidget.html#show">show</a>();}<a name="f463"></a>Frame::Frame(QWidget* parent, const char* name): <a href="qframe.html">QFrame</a>(parent, name){ button1 = new <a href="qpushbutton.html">QPushButton</a>("Simple Popup", this); <a href="qobject.html#connect">connect</a> ( button1, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), SLOT( button1Clicked() ) ); button2 = new <a href="qpushbutton.html">QPushButton</a>("Fancy Popup", this);<a name="x1572"></a> <a href="qobject.html#connect">connect</a> ( button2, SIGNAL( <a href="qbutton.html#pressed">pressed</a>() ), SLOT( button2Pressed() ) ); <a href="qboxlayout.html">QBoxLayout</a> * l = new <a href="qhboxlayout.html">QHBoxLayout</a>( this );<a name="x1593"></a><a name="x1580"></a> button1-><a href="qwidget.html#setMaximumSize">setMaximumSize</a>(button1-><a href="qwidget.html#sizeHint">sizeHint</a>()); button2-><a href="qwidget.html#setMaximumSize">setMaximumSize</a>(button2-><a href="qwidget.html#sizeHint">sizeHint</a>()); l-><a href="qboxlayout.html#addWidget">addWidget</a>( button1 ); l-><a href="qboxlayout.html#addWidget">addWidget</a>( button2 );<a name="x1575"></a> l-><a href="qlayout.html#activate">activate</a>();<a name="x1579"></a>// button1-><a href="qwidget.html#setGeometry">setGeometry</a>(20,20,100,30);// button2-><a href="qwidget.html#setGeometry">setGeometry</a>(140,20,100,30); <a href="qwidget.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="qframe.html">QFrame</a>( this ,0, WType_Popup); popup1-><a href="qframe.html#setFrameStyle">setFrameStyle</a>( WinPanel|Raised ); popup1-><a href="qwidget.html#resize">resize</a>(150,100); <a href="qlineedit.html">QLineEdit</a> *tmpE = new <a href="qlineedit.html">QLineEdit</a>( popup1 );<a name="x1585"></a><a name="x1576"></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>() ) );<a name="x1592"></a> tmpE-><a href="qwidget.html#setGeometry">setGeometry</a>(10,10, 130, 30);<a name="x1591"></a> tmpE-><a href="qwidget.html#setFocus">setFocus</a>(); <a href="qpushbutton.html">QPushButton</a> *tmpB = new <a href="qpushbutton.html">QPushButton</a>("Click me!", popup1);<a name="x1582"></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>() ) ); tmpB-><a href="qwidget.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="qlineedit.html">QLineEdit</a>( popup2 );// tmpE-><a href="qwidget.html#setFocus">setFocus</a>();// connect( tmpE, SIGNAL( <a href="qlineedit.html#returnPressed">returnPressed</a>() ), popup2, SLOT( <a href="qwidget.html#close">close</a>() ) );// tmpE-><a href="qwidget.html#setGeometry">setGeometry</a>(10, 10, 130, 30);}void <a name="f464"></a>Frame::button1Clicked(){<a name="x1588"></a><a name="x1584"></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() ) );<a name="x1594"></a> popup1-><a href="qwidget.html#show">show</a>();}void <a name="f465"></a>Frame::button2Pressed(){ popup2->popup(button2);}int main( int argc, char **argv ){ <a href="qapplication.html">QApplication</a> a(argc,argv); Frame frame; frame.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Custom Popups"); a.<a href="qapplication.html#setMainWidget">setMainWidget</a>(&frame); frame.<a href="qwidget.html#show">show</a>(); return a.<a href="qapplication.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 © 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -