C++ (Qt)while(!asleep()) sheep++;
myWidget->setFocus(Qt::MouseFocusReason);
void MyWidget::focusOutEvent ( QFocusEvent * event ){ hide();}
C++ (Qt)#include <QtGui> #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)#include <QtWidgets/QtWidgets>#endif class A : public QWidget{public: A(QWidget *parent = 0) : QWidget(parent), _hidingLabel(new QLabel("hide me", this)) { _hidingLabel->setFrameShape(QFrame::Box); adjustSize(); installEventFilter(this); } protected: void showEvent(QShowEvent *e) { Q_UNUSED(e); _labelRect = _hidingLabel->frameGeometry(); } bool eventFilter(QObject *o, QEvent *e) { if (e->type() == QEvent::MouseButtonPress) { QPoint clickedPos = static_cast<QMouseEvent *>(e)->pos(); if (!childAt(clickedPos)) { if (_labelRect.contains(clickedPos)) _hidingLabel->show(); else _hidingLabel->hide(); return true; } } return QWidget::eventFilter(o, e); } private: QLabel *_hidingLabel; QRect _labelRect;}; int main( int argc, char** argv ){ QApplication app(argc, argv); A a; a.show(); return app.exec();}
C++ (Qt)#include <QtGui> class MyPopup : public QWidget {public: MyPopup( QWidget * parent = 0 ) : QWidget(parent, Qt::Popup) { setAttribute(Qt::WA_ShowWithoutActivating); } void mousePressEvent( QMouseEvent * e ) { if (!rect().contains(e->pos())) hide(); } }; int main(int argc, char *argv[]){ QApplication app(argc, argv); QDialog dlg; dlg.show(); QPointer <MyPopup> test(new MyPopup); test->setGeometry(50, 50, 100, 100); test->show(); test->setMouseTracking(true); return app.exec();}