Results 1 to 13 of 13

Thread: Resetting a Qwidget

  1. #1
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Resetting a Qwidget

    Hello,

    Is there a way to do a "Global" reset on a QWidget that contains many checkboxes and lineEdits. I have included a screen shot of a form that I am working on.




    The form enters info into a MYSQL database regarding information on a group of photographic images. The application allows for a user to select a directory of images and to go through this list and make entries into a database concerning each image. What I want to do is allow the user to select a number of keywords as well as other data about the image and when s/he clicks next, the data is sent to the database and the form is reset so the next image can be processed. What I am wondering is if I am able to create a widget that holds all these sub widgets(checkboxes, line edits, etc.) and after the "Next" button is pressed and info processed, that a reset signal could be emited to reset the form. I could send a rest that is coded into every widget, but there has to be a way to send one reset signal to reset the whole lot. That is why I was thinking of gathering all the checkboxes and line edits in one qWidget and only having to reset that one parent widget? Is this possible and what would this look like? Also, the reset button button on the bottom of the form would also emit this reset.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resetting a Qwidget

    I see QButtonGroup that could be a solution : you store every button you want in it, with an exclusive id.

    Your reset() signal could be received by the QButtonGroup object : then you would just have to check / uncheck all the children with buttons(), that give you a list of all the children.

    Hope this helps ;-)

  3. The following user says thank you to guilugi for this useful post:

    devilj (2nd July 2007)

  4. #3
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resetting a Qwidget

    Thanks, guilugi,

    I will check that out, it sounds like exactly what I need. I'm only entering data into the database and not handling data. Someone on another board suggested taking a look at the model/view architecture of Qt4. This only made me more confused. I will post results.

    Thanks again.

  5. #4
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resetting a Qwidget

    OK this is what I have so far:

    I decided to create a test application.

    I created a QButtonGroup encompassing 4 checkboxes:

    Qt Code:
    1. QButtonGroup *buttonGroup = new QButtonGroup(this);
    2. buttonGroup->setExclusive(0);
    3. buttonGroup->addButton(checkBox_1, 1);
    4. buttonGroup->addButton(checkBox_2, 2);
    5. buttonGroup->addButton(checkBox_3, 3);
    6. buttonGroup->addButton(checkBox_4, 4);
    To copy to clipboard, switch view to plain text mode 

    I also created a "reset" button to call a "reset()" function:

    Qt Code:
    1. void Window::reset()
    2. {
    3. for (int i = 1; i < 5; i++)
    4. buttonGroup.button(i)->setCheckState(0);
    5. }
    To copy to clipboard, switch view to plain text mode 

    When I compile I get:

    window.cpp: In member function ‘void Window::reset()’:
    window.cpp:48: error: request for member ‘button’ in ‘((Window*)this)->Window::buttonGroup’, which is of non-class type ‘QButtonGroup*’

    Any Thoughts?

    Thanks

  6. #5
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resetting a Qwidget

    Yes, easy one

    use buttonGroup-> instead of buttonGroup. !

    ;-)

  7. #6
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resetting a Qwidget

    Ok, did that!
    Thank you!

    Next question?

    I placed the QButtonGroup into my layout:

    layout->addWidget(buttonGroup);

    I can't do this can I? Because in Qt4 the ButtonGroup is not a Widget anymore, am I correct? Should I then just place the checkbox buttons as if they are separate widgets?

    Thanks,

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Resetting a Qwidget

    You are correct. QButtonGroup in Qt4 is just a virtual entity, so you can place its buttons wherever you want (I think they might even have different parents).

  9. #8
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resetting a Qwidget

    Great, everything compiles but when I execute, and try the reset button, it kills the application. The error I get is:

    Segmentation fault (core dumped)

    This is a simple test, here is my code:
    window.h:
    Qt Code:
    1. #ifndef WINDOW_H
    2. #define WINDOW_H
    3. #include <QDialog>
    4.  
    5. class QLabel;
    6. class QCheckBox;
    7.  
    8. class Window : public QDialog
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. Window(QWidget *parent = 0);
    14.  
    15. private slots:
    16. void reset();
    17.  
    18. private:
    19. QPushButton *createButton(const QString &text, const char * member);
    20. QLabel *label;
    21. QButtonGroup *buttonGroup;
    22. QCheckBox *checkBox_1;
    23. QCheckBox *checkBox_2;
    24. QCheckBox *checkBox_3;
    25. QCheckBox *checkBox_4;
    26. QPushButton *resetButton;
    27.  
    28. };
    29.  
    30. #endif
    To copy to clipboard, switch view to plain text mode 

    window.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include "window.h"
    3.  
    4. Window::Window(QWidget *parent)
    5. : QDialog(parent)
    6. {
    7. resetButton = createButton(tr("Reset"), SLOT(reset()));
    8.  
    9. QCheckBox *checkBox_1 = new QCheckBox(tr("Backgrounds..."));
    10. QCheckBox *checkBox_2 = new QCheckBox(tr("Cell Phones..."));
    11. QCheckBox *checkBox_3 = new QCheckBox(tr("Automotive..."));
    12. QCheckBox *checkBox_4 = new QCheckBox(tr("Computers..."));
    13.  
    14. QButtonGroup* buttonGroup = new QButtonGroup(this);
    15. buttonGroup->setExclusive(0);
    16. buttonGroup->addButton(checkBox_1, 1);
    17. buttonGroup->addButton(checkBox_2, 2);
    18. buttonGroup->addButton(checkBox_3, 3);
    19. buttonGroup->addButton(checkBox_4, 4);
    20.  
    21. QVBoxLayout *layout = new QVBoxLayout;
    22. layout->addWidget(checkBox_1);
    23. layout->addWidget(checkBox_2);
    24. layout->addWidget(checkBox_3);
    25. layout->addWidget(checkBox_4);
    26. layout->addWidget(resetButton);
    27. setLayout(layout);
    28. }
    29.  
    30. QPushButton *Window::createButton(const QString &text, const char *member)
    31. {
    32. QPushButton *button = new QPushButton(text);
    33. connect(button, SIGNAL(clicked()), this, member);
    34. return button;
    35. }
    36.  
    37. void Window::reset()
    38. {
    39. for (int i=1;i < 5; i++)
    40. buttonGroup->button(i)->setChecked(0);
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 

    Finally, a simple main.cpp:

    Qt Code:
    1. #include <QApplication>
    2. #include "window.h"
    3.  
    4. int main(int argc, char * argv[])
    5. {
    6. QApplication app(argc, argv);
    7. Window window;
    8. window.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Any suggestions?

    Thank you very much!

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Resetting a Qwidget

    You are declaring local variables representing the checkboxes and the group that shadow the ones that are declared in Window. Thus you use uninitialized pointers.

  11. #10
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resetting a Qwidget

    IT WORKS!!!
    Dziekuje, Wysota
    and Sto Lat

  12. #11
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resetting a Qwidget

    Huu lol, what the hell does it mean ? :-p

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Resetting a Qwidget

    Quote Originally Posted by devilj View Post
    IT WORKS!!!
    Dziekuje, Wysota
    and Sto Lat
    Thx.

    Quote Originally Posted by guilugi View Post
    Huu lol, what the hell does it mean ? :-p
    It's "Thank you" and "Live Long" in Polish

  14. #13
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resetting a Qwidget

    Okay, thanks for the translation :-D

Similar Threads

  1. Replies: 4
    Last Post: 24th April 2007, 13:18
  2. Replies: 3
    Last Post: 8th March 2007, 14:54
  3. Transparent QWidget on QGLWidget
    By showhand in forum Qt Programming
    Replies: 2
    Last Post: 27th November 2006, 01:00
  4. Replies: 1
    Last Post: 2nd May 2006, 21:11
  5. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 22:59

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.