#include #include class MyMainWidget : public QWidget { Q_OBJECT public: MyMainWidget (QWidget *aParent = 0, Qt::WindowFlags aFlags = 0) : QWidget (aParent, aFlags) { setWindowTitle (QString ("MyMainWidget (0x%1)") .arg ((intptr_t) this, 8, 16, QChar('0'))); QHBoxLayout *pbLayout = new QHBoxLayout(); for (int i = 1; i <= 10; ++ i) { QPushButton *pb = new QPushButton (QString::number (i), this); connect (pb, SIGNAL (clicked()), this, SLOT (nextTest())); pbLayout->addWidget (pb); } mPBClone = new QPushButton ("Clone", this); connect (mPBClone, SIGNAL (clicked()), this, SLOT (clone())); mPBCloneModal = new QPushButton ("Modal Clone", this); connect (mPBCloneModal, SIGNAL (clicked()), this, SLOT (cloneModal())); mPBCloneAppModal = new QPushButton ("App Modal Clone", this); connect (mPBCloneAppModal, SIGNAL (clicked()), this, SLOT (cloneAppModal())); QVBoxLayout *layout = new QVBoxLayout(); layout->addLayout (pbLayout); layout->addWidget (mPBClone); layout->addWidget (mPBCloneModal); layout->addWidget (mPBCloneAppModal); setLayout (layout); } virtual ~MyMainWidget() { } void test (int aNum, QWidget *aWgt, const QString &aText) { aWgt->setAttribute (Qt::WA_DeleteOnClose); aWgt->setWindowTitle (QString ("[%1] %2 (0x%3)") .arg (aNum) .arg (aWgt->metaObject()->className()) .arg ((intptr_t) aWgt, 8, 16, QChar('0'))); QString newText = QString ( "[%1] %2\n\n" "parentWidget() is %3\n" "className() is %4\n" "isWindow() is %5\n" "windowFlags() is 0x%6\n" "windowModality() is %7") .arg (aNum) .arg (aText) .arg (aWgt->parentWidget() ? aWgt->parentWidget()->metaObject() ->className() : "") .arg (aWgt->metaObject()->className()) .arg (aWgt->isWindow()) .arg (aWgt->windowFlags(), 8, 16, QLatin1Char ('0')) .arg (aWgt->windowModality()); if (aWgt->inherits ("QMessageBox")) { static_cast (aWgt)->setText (newText); } else { QLabel *label = new QLabel (aWgt); label->setText (newText); QVBoxLayout *layout = new QVBoxLayout(); layout->addWidget (label); aWgt->setLayout (layout); } aWgt->show(); } public slots: void nextTest() { int num = static_cast (sender())->text().toInt(); switch (num) { case 1: { QWidget *w = new QWidget(); test (num, w, "QWidget().\n" "Should have a taskbar entry.\n" "Should not be always on top of parent.\n" "Should not block parent."); break; } case 2: { QWidget *w = new QWidget (this, Qt::Window); test (num, w, "QWidget (this, Qt::Window).\n" "Should not have a taskbar entry.\n" "Should be always on top of parent.\n" "Should not block parent."); break; } case 3: { QWidget *w = new QDialog(); test (num, w, "QDialog().\n" "Should have a taskbar entry.\n" "Should not be always on top of parent.\n" "Should not block parent."); break; } case 4: { QWidget *w = new QDialog (this); test (num, w, "QDialog(this).\n" "Should not have a taskbar entry.\n" "Should be always on top of parent.\n" "Should not block parent."); break; } case 5: { QWidget *w = new QMessageBox(); test (num, w, "QMessageBox().\n" "Should have a taskbar entry.\n" "Should be always on top of parent.\n" "Should block parent."); break; } case 6: { QWidget *w = new QMessageBox (this); test (num, w, "QMessageBox (this).\n" "Should not have a taskbar entry.\n" "Should be always on top of parent.\n" "Should block parent."); break; } case 7: { QWidget *w = new QWidget(); w->setWindowModality (Qt::WindowModal); test (num, w, "QWidget() - WindowModal.\n" "Should have a taskbar entry.\n" "Should not be always on top of parent.\n" "Should not block parent."); break; } case 8: { QWidget *w = new QWidget (this, Qt::Window); w->setWindowModality (Qt::WindowModal); test (num, w, "QWidget (this, Qt::Window) - WindowModal.\n" "Should not have a taskbar entry.\n" "Should be always on top of parent.\n" "Should block parent."); break; } case 9: { QWidget *w = new QWidget(0, Qt::Dialog); w->setWindowModality (Qt::ApplicationModal); test (num, w, "QWidget() - ApplicationModal.\n" "Should have a taskbar entry.\n" "Should be always on top of parent.\n" "Should block parent."); break; } case 10: { QWidget *w = new QWidget (this, Qt::Window); w->setWindowModality (Qt::ApplicationModal); test (num, w, "QWidget (this, Qt::Window) - ApplicationModal.\n" "Should not have a taskbar entry.\n" "Should be always on top of parent.\n" "Should block parent."); break; } default: return; } } void clone() { QWidget *w = new MyMainWidget (); w->setAttribute (Qt::WA_DeleteOnClose); w->show(); } void cloneModal() { QWidget *w = new MyMainWidget (this, Qt::Window); w->setAttribute (Qt::WA_DeleteOnClose); w->setWindowModality (Qt::WindowModal); w->setWindowTitle (QString("[M] ") + w->windowTitle()); w->show(); } void cloneAppModal() { QWidget *w = new MyMainWidget(); w->setAttribute (Qt::WA_DeleteOnClose); w->setWindowModality (Qt::ApplicationModal); w->setWindowTitle (QString("[AM] ") + w->windowTitle()); w->show(); } private: QPushButton *mPBClone; QPushButton *mPBCloneModal; QPushButton *mPBCloneAppModal; }; int main (int argc, char **argv) { QApplication app (argc, argv); app.setQuitOnLastWindowClosed (true); MyMainWidget *mainWidget = new MyMainWidget(); mainWidget->show(); return app.exec(); } #include "modal.moc"