PDA

View Full Version : Correct way to clear a form?



Walter
9th August 2009, 17:22
What is the correct way to clear a complete form?

Example: QMainWindow containing several QLineEdit and one QTableView.

Using this code I can clear all QLineEdits:

foreach(QLineEdit* widget, findChildren<QLineEdit*>())
{
widget->clear();
}

But: How to do it for QTableView (without resetting the model)?

And isn't there a better way to accomplish it? E.g. a single signal?

Walter
11th August 2009, 11:18
Apparently another one of these threads where I'm the only one having such questions... :D

ChrisW67
14th August 2009, 22:51
Given that the QTableView faithfully displays what's in the underlying model it seems that the only way to clear it would be to empty or disconnect the model.

QTableWidget has the invitingly named QTableWidget::clear() and QTableWidget::clearContents() slots, although you could iterate with QTableWidget::setItem()

Walter
15th August 2009, 07:17
I find it a bit cumbersome that I have to do this by hand with a foreach for every type of widget and that there isn't a simple signal or a function to clear all childs...

Boron
15th August 2009, 13:22
How would you clear a QPushButton?
As buttons are probably the most often used elements in forms what would you expect if Qt would offer something like YourForm.clear() (assuming this would recursively go through all form elements and call their clear() method). Or if QWidget offered a pure virtual clear() method, that every derived class had to implement?

A QLineEdit can be cleared. Sure. Also a QTextBox, QLabel any many more. But a QPushButton or a QSlider?
Should these widget implement an empty clear() method? Just that their parent widget can call clear()?

If you design a form your know all the elements in it, just because YOU designed it :).
I would implement something like this:
YourForm::clear() {
ui.lineEdit1->clear();
ui.lineEdit2->clear();
ui.plainTextEdit->clear();
...
}

Walter
16th August 2009, 06:53
How would you clear a QPushButton?

I never said something about a button and I'm pretty sure that you are familiar with the "if" command so you are able not to clear a button :D
I only say class->inherits().


If you design a form your know all the elements in it, just because YOU designed it :).

Are you kidding? Have you never worked in a team? Did you never design a class?

Boron
16th August 2009, 14:56
What I wanted to say is that there cannot be a general way to clear a form that may contain any kind of widget, because there may be widget that cannot be cleared. I took a button as example for those widgets.

Walter
16th August 2009, 18:54
The function in my self developed small Qt class library does exactly that by


foreach(QLineEdit* widget, findChildren<qLineEdit*>())
{
if( !firsteditablewidget) firsteditablewidget = widget;
modified = true;
widget->clear();
}
foreach(ecTextEdit* widget, findChildren<ecTextEdit*>())
{
widget->clear();
}
foreach(QTableView* widget, findChildren<QTableView*>())
{
QAbstractItemModel * abstractmodel = widget->model();
if( abstractmodel )
{
if( abstractmodel->inherits( "QSqlQueryModel"))
{
QSqlQueryModel *model = (QSqlQueryModel *)abstractmodel;
model->clear();
.....


All I wanted to know if there is a better way to achieve this...