Results 1 to 8 of 8

Thread: Correct way to clear a form?

  1. #1
    Join Date
    Jun 2007
    Location
    Austria (Europe)
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Correct way to clear a form?

    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:
    Qt Code:
    1. foreach(QLineEdit* widget, findChildren<QLineEdit*>())
    2. {
    3. widget->clear();
    4. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jun 2007
    Location
    Austria (Europe)
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Correct way to clear a form?

    Apparently another one of these threads where I'm the only one having such questions...

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Correct way to clear a form?

    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()

  4. #4
    Join Date
    Jun 2007
    Location
    Austria (Europe)
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Correct way to clear a form?

    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...

  5. #5
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Correct way to clear a form?

    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:
    Qt Code:
    1. YourForm::clear() {
    2. ui.lineEdit1->clear();
    3. ui.lineEdit2->clear();
    4. ui.plainTextEdit->clear();
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2007
    Location
    Austria (Europe)
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Correct way to clear a form?

    Quote Originally Posted by Boron View Post
    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
    I only say class->inherits().

    Quote Originally Posted by Boron View Post
    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?

  7. #7
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Correct way to clear a form?

    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.

  8. #8
    Join Date
    Jun 2007
    Location
    Austria (Europe)
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Correct way to clear a form?

    The function in my self developed small Qt class library does exactly that by

    Qt Code:
    1. foreach(QLineEdit* widget, findChildren<qLineEdit*>())
    2. {
    3. if( !firsteditablewidget) firsteditablewidget = widget;
    4. modified = true;
    5. widget->clear();
    6. }
    7. foreach(ecTextEdit* widget, findChildren<ecTextEdit*>())
    8. {
    9. widget->clear();
    10. }
    11. foreach(QTableView* widget, findChildren<QTableView*>())
    12. {
    13. QAbstractItemModel * abstractmodel = widget->model();
    14. if( abstractmodel )
    15. {
    16. if( abstractmodel->inherits( "QSqlQueryModel"))
    17. {
    18. QSqlQueryModel *model = (QSqlQueryModel *)abstractmodel;
    19. model->clear();
    20. .....
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. How to get a reference for a control of the main form?
    By thiforums in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2009, 17:20
  2. Replies: 1
    Last Post: 19th March 2009, 09:41
  3. Help me to load one form over another form PushButton
    By wagmare in forum Qt Programming
    Replies: 7
    Last Post: 26th November 2008, 16:11
  4. visible main form?
    By triperzonak in forum Newbie
    Replies: 1
    Last Post: 12th June 2008, 09:00
  5. Calling a new form from current form
    By webgenius in forum Qt Programming
    Replies: 7
    Last Post: 8th April 2007, 19:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.