42 #include <QApplication>
44 #include <QCalendarWidget>
46 #include <QDesktopWidget>
48 #include <QDialogButtonBox>
50 #include <QPushButton>
52 #include <QTextDocument>
54 #include <QToolButton>
55 #include <QTreeWidget>
66 void createAndControl();
69 QComboBox* theClassCombo;
71 QStringList theClassNames;
72 QObject* theControlledObject;
77 theClassCombo =
new QComboBox(
this);
78 QToolButton* button =
new QToolButton(
this);
80 QDialogButtonBox* buttonBox =
new QDialogButtonBox(
this);
82 connect(button, SIGNAL(clicked()),
this, SLOT(createAndControl()));
83 connect(buttonBox, SIGNAL(rejected()),
this, SLOT(reject()));
85 button->setText(tr(
"Create And Control"));
86 buttonBox->setStandardButtons(QDialogButtonBox::Close);
88 QVBoxLayout* layout =
new QVBoxLayout(
this);
89 QHBoxLayout* internalLayout =
new QHBoxLayout();
90 internalLayout->addWidget(theClassCombo);
91 internalLayout->addWidget(button);
92 layout->addLayout(internalLayout);
93 layout->addWidget(theController);
94 layout->addWidget(buttonBox);
96 theClassNames.append(QLatin1String(
"QWidget"));
97 theClassNames.append(QLatin1String(
"QPushButton"));
98 theClassNames.append(QLatin1String(
"QDialogButtonBox"));
99 theClassNames.append(QLatin1String(
"QTreeWidget"));
100 theClassNames.append(QLatin1String(
"QCalendarWidget"));
101 theClassNames.append(QLatin1String(
"QAction"));
102 theClassNames.append(QLatin1String(
"QTimeLine"));
103 theClassNames.append(QLatin1String(
"QTextDocument"));
105 theClassCombo->addItems(theClassNames);
110 if (theControlledObject)
112 delete theControlledObject;
117 MyController::createAndControl()
119 QObject* newObject = 0;
120 QString className = theClassNames.at(theClassCombo->currentIndex());
122 if (className == QLatin1String(
"QWidget"))
124 newObject =
new QWidget();
126 else if (className == QLatin1String(
"QPushButton"))
128 newObject =
new QPushButton();
130 else if (className == QLatin1String(
"QDialogButtonBox"))
132 newObject =
new QDialogButtonBox();
134 else if (className == QLatin1String(
"QTreeWidget"))
136 newObject =
new QTreeWidget();
138 else if (className == QLatin1String(
"QCalendarWidget"))
140 newObject =
new QCalendarWidget();
142 else if (className == QLatin1String(
"QAction"))
144 newObject =
new QAction(0);
146 else if (className == QLatin1String(
"QTimeLine"))
148 newObject =
new QTimeLine();
150 else if (className == QLatin1String(
"QTextDocument"))
152 newObject =
new QTextDocument();
160 QWidget* newWidget = qobject_cast<QWidget*>(newObject);
164 QRect r = newWidget->geometry();
165 r.setSize(newWidget->sizeHint());
166 r.setWidth(qMax(r.width(), 150));
167 r.setHeight(qMax(r.height(), 50));
168 r.moveCenter(QApplication::desktop()->geometry().
center());
169 newWidget->setGeometry(r);
170 newWidget->setWindowTitle(tr(
"Controlled Object: %1").arg(className));
174 if (theControlledObject)
176 delete theControlledObject;
179 theControlledObject = newObject;
180 theController->
setObject(theControlledObject);
186 QApplication app(argc, argv);
191 int ret = app.exec();