📄 popup-popup-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - popup/popup.cpp example file</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: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Popup Widgets</h1><br clear="all"> This examples shows how to implement widgets which should popup. <hr> Header file: <pre>/****************************************************************************** $Id: qt/examples/popup/popup.h 2.3.8 edited 2004-05-12 $**** 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 QLabel{ Q_OBJECTpublic: 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 QFrame { 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> <hr> Implementation:<pre>/****************************************************************************** $Id: qt/examples/popup/popup.cpp 2.3.8 edited 2004-05-12 $**** 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 name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>#include <qkeycode.h>#include <<a name="qlayout.h"></a><a href="qlayout-h.html">qlayout.h</a>>FancyPopup::FancyPopup( <a name="QWidget"></a><a href="qwidget.html">QWidget</a>* parent, const char* name ): <a name="QLabel"></a><a href="qlabel.html">QLabel</a>( parent, name, WType_Popup ){ <a name="setFrameStyle"></a><a href="qframe.html#558f79">setFrameStyle</a>( WinPanel|Raised ); <a name="setAlignment"></a><a href="qlabel.html#4743c8">setAlignment</a>( AlignCenter ); <a name="resize"></a><a href="qwidget.html#ff9d07">resize</a>(150,100); moves = 0; <a name="setMouseTracking"></a><a href="qwidget.html#36406c">setMouseTracking</a>( TRUE );}void <a name="402"></a>FancyPopup::mouseMoveEvent( <a name="QMouseEvent"></a><a href="qmouseevent.html">QMouseEvent</a> * e){ moves++; <a name="QString"></a><a href="qstring.html">QString</a> s; s.<a name="sprintf"></a><a href="qstring.html#926f67">sprintf</a>("%d/%d", e->pos().x(), e->pos().y()); if (e->state() & QMouseEvent::LeftButton) s += " (down)"; <a name="setText"></a><a href="qlabel.html#bc5ea6">setText</a>(s);}void <a name="403"></a>FancyPopup::mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * e){ if (rect().contains( e->pos() ) || moves > 5) <a name="close"></a><a href="qwidget.html#3d9c87">close</a>();}void <a name="404"></a>FancyPopup::closeEvent( <a name="QCloseEvent"></a><a href="qcloseevent.html">QCloseEvent</a> *e ){ e-><a name="accept"></a><a href="qcloseevent.html#7cf66f">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="QApplication::sendEvent"></a><a href="qapplication.html#333290">QApplication::sendEvent</a>( popupParent, &me );}void <a name="405"></a>FancyPopup::popup( <a href="qwidget.html">QWidget</a>* parent) { popupParent = parent; <a href="qlabel.html#bc5ea6">setText</a>("Move the mouse!"); if (popupParent) <a name="move"></a><a href="qwidget.html#f753ed">move</a>( popupParent->mapToGlobal( popupParent->rect().bottomLeft() ) ); <a name="show"></a><a href="qwidget.html#200ee5">show</a>();}Frame::Frame(<a href="qwidget.html">QWidget</a>* parent, const char* name): <a name="QFrame"></a><a href="qframe.html">QFrame</a>(parent, name){ button1 = new <a name="QPushButton"></a><a href="qpushbutton.html">QPushButton</a>("Simple Popup", this); <a name="connect"></a><a href="qobject.html#fbde73">connect</a> ( button1, SIGNAL( clicked() ), SLOT( <a name="button1Clicked"></a><a href="#406">button1Clicked</a>() ) ); button2 = new <a href="qpushbutton.html">QPushButton</a>("Fancy Popup", this); <a href="qobject.html#fbde73">connect</a> ( button2, SIGNAL( pressed() ), SLOT( <a name="button2Pressed"></a><a href="#407">button2Pressed</a>() ) ); <a name="QBoxLayout"></a><a href="qboxlayout.html">QBoxLayout</a> * l = new <a name="QHBoxLayout"></a><a href="qhboxlayout.html">QHBoxLayout</a>( this ); button1->setMaximumSize(button1->sizeHint()); button2->setMaximumSize(button2->sizeHint()); l-><a name="addWidget"></a><a href="qboxlayout.html#ebba99">addWidget</a>( button1 ); l-><a href="qboxlayout.html#ebba99">addWidget</a>( button2 ); l-><a name="activate"></a><a href="qlayout.html#1cb33d">activate</a>();// button1->setGeometry(20,20,100,30);// button2->setGeometry(140,20,100,30); <a href="qwidget.html#ff9d07">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->setFrameStyle( WinPanel|Raised ); popup1->resize(150,100); <a name="QLineEdit"></a><a href="qlineedit.html">QLineEdit</a> *tmpE = new <a href="qlineedit.html">QLineEdit</a>( popup1 ); <a href="qobject.html#fbde73">connect</a>( tmpE, SIGNAL( returnPressed() ), popup1, SLOT( <a name="hide"></a><a href="qwidget.html#410481">hide</a>() ) ); tmpE-><a name="setGeometry"></a><a href="qwidget.html#9ede68">setGeometry</a>(10,10, 130, 30); tmpE-><a name="setFocus"></a><a href="qwidget.html#25775a">setFocus</a>(); <a href="qpushbutton.html">QPushButton</a> *tmpB = new <a href="qpushbutton.html">QPushButton</a>("Click me!", popup1); <a href="qobject.html#fbde73">connect</a>( tmpB, SIGNAL( clicked() ), popup1, SLOT( <a href="qwidget.html#3d9c87">close</a>() ) ); tmpB-><a name="setGeometry"></a><a href="qpushbutton.html#c9b88e">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#25775a">setFocus</a>();// connect( tmpE, SIGNAL( returnPressed() ), popup2, SLOT( <a href="qwidget.html#3d9c87">close</a>() ) );// tmpE-><a href="qwidget.html#9ede68">setGeometry</a>(10, 10, 130, 30);}void <a name="406"></a>Frame::button1Clicked(){ popup1->move( <a name="mapToGlobal"></a><a href="qwidget.html#eb4b9c">mapToGlobal</a>( button1->geometry().bottomLeft() ) ); popup1->show();}void <a name="407"></a>Frame::button2Pressed(){ popup2->popup(button2);}int main( int argc, char **argv ){ <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a(argc,argv); Frame frame; frame.<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - Custom Popups"); a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>(&frame); frame.<a href="qwidget.html#200ee5">show</a>(); return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -