41 #include <QApplication>
43 #include <QDialogButtonBox>
47 #include <QToolButton>
48 #include <QPushButton>
50 #include <QTreeWidget>
52 #include <QDesktopWidget>
53 #include <QTextDocument>
54 #include <QCalendarWidget>
65 void createAndControl();
67 QComboBox* theClassCombo;
69 QStringList theClassNames;
70 QObject* theControlledObject;
74 : QDialog(parent), theControlledObject(0)
76 theClassCombo =
new QComboBox(
this);
77 QToolButton* button =
new QToolButton(
this);
79 QDialogButtonBox* buttonBox =
new QDialogButtonBox(
this);
81 connect(button, SIGNAL(clicked()),
this, SLOT(createAndControl()));
82 connect(buttonBox, SIGNAL(rejected()),
this, SLOT(reject()));
84 button->setText(tr(
"Create And Control"));
85 buttonBox->setStandardButtons(QDialogButtonBox::Close);
87 QVBoxLayout* layout =
new QVBoxLayout(
this);
88 QHBoxLayout* internalLayout =
new QHBoxLayout();
89 internalLayout->addWidget(theClassCombo);
90 internalLayout->addWidget(button);
91 layout->addLayout(internalLayout);
92 layout->addWidget(theController);
93 layout->addWidget(buttonBox);
95 theClassNames.append(QLatin1String(
"QWidget"));
96 theClassNames.append(QLatin1String(
"QPushButton"));
97 theClassNames.append(QLatin1String(
"QDialogButtonBox"));
98 theClassNames.append(QLatin1String(
"QTreeWidget"));
99 theClassNames.append(QLatin1String(
"QCalendarWidget"));
100 theClassNames.append(QLatin1String(
"QAction"));
101 theClassNames.append(QLatin1String(
"QTimeLine"));
102 theClassNames.append(QLatin1String(
"QTextDocument"));
104 theClassCombo->addItems(theClassNames);
109 if (theControlledObject)
111 delete theControlledObject;
115 void MyController::createAndControl()
117 QObject* newObject = 0;
118 QString className = theClassNames.at(theClassCombo->currentIndex());
120 if (className == QLatin1String(
"QWidget"))
122 newObject =
new QWidget();
124 else if (className == QLatin1String(
"QPushButton"))
126 newObject =
new QPushButton();
128 else if (className == QLatin1String(
"QDialogButtonBox"))
130 newObject =
new QDialogButtonBox();
132 else if (className == QLatin1String(
"QTreeWidget"))
134 newObject =
new QTreeWidget();
136 else if (className == QLatin1String(
"QCalendarWidget"))
138 newObject =
new QCalendarWidget();
140 else if (className == QLatin1String(
"QAction"))
142 newObject =
new QAction(0);
144 else if (className == QLatin1String(
"QTimeLine"))
146 newObject =
new QTimeLine();
148 else if (className == QLatin1String(
"QTextDocument"))
150 newObject =
new QTextDocument();
158 QWidget* newWidget = qobject_cast<QWidget*>(newObject);
162 QRect r = newWidget->geometry();
163 r.setSize(newWidget->sizeHint());
164 r.setWidth(qMax(r.width(), 150));
165 r.setHeight(qMax(r.height(), 50));
166 r.moveCenter(QApplication::desktop()->geometry().center());
167 newWidget->setGeometry(r);
168 newWidget->setWindowTitle(tr(
"Controlled Object: %1").arg(className));
172 if (theControlledObject)
174 delete theControlledObject;
177 theControlledObject = newObject;
178 theController->
setObject(theControlledObject);
181 int main(
int argc,
char** argv)
183 QApplication app(argc, argv);
188 int ret = app.exec();