QWidget *widget
= form
->findChildren<QWidget
*>
("someobjectname");
QWidget *widget = form->findChildren<QWidget*>("someobjectname");
To copy to clipboard, switch view to plain text mode
If you don't know the object names ahead of time then they should follow some useful pattern to allow you to pick them from the list of all descendent objects: then you know their names.
#include <QtGui>
#include <QUiLoader>
#include <QDebug>
Q_OBJECT
public:
layout->addWidget(button);
central->setLayout(layout);
setCentralWidget(central);
connect(button, SIGNAL(clicked()), SLOT(dump()));
form = loader.load(&file, this);
layout->insertWidget(0, form);
file.close();
}
}
private slots:
void dump() {
Q_ASSERT(form);
// In "natural" order
QList<QLineEdit*> edits = form->findChildren<QLineEdit*>();
qDebug() << edit->objectName() << edit->text();
// Alphabetical order of object name
map.insert(edit->objectName(), edit);
while (i.hasNext()) {
i.next();
qDebug() << i.key() << i.value()->text();
}
}
private:
};
int main(int argc, char *argv[])
{
MainWindow m("test.ui");
m.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QUiLoader>
#include <QDebug>
class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(const QString &fileName, QWidget *p = 0): QMainWindow(p), form(0) {
QWidget *central = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(central);
QPushButton *button = new QPushButton("Dump", central);
layout->addWidget(button);
central->setLayout(layout);
setCentralWidget(central);
connect(button, SIGNAL(clicked()), SLOT(dump()));
QUiLoader loader;
QFile file(fileName);
if (file.open(QIODevice::ReadOnly)) {
form = loader.load(&file, this);
layout->insertWidget(0, form);
file.close();
}
}
private slots:
void dump() {
Q_ASSERT(form);
// In "natural" order
QList<QLineEdit*> edits = form->findChildren<QLineEdit*>();
foreach(QLineEdit* edit, edits)
qDebug() << edit->objectName() << edit->text();
// Alphabetical order of object name
QMap<QString,QLineEdit*> map;
foreach(QLineEdit* edit, edits)
map.insert(edit->objectName(), edit);
QMapIterator<QString,QLineEdit*> i(map);
while (i.hasNext()) {
i.next();
qDebug() << i.key() << i.value()->text();
}
}
private:
QWidget *form;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow m("test.ui");
m.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks