PDA

View Full Version : Resetting a Qwidget



devilj
2nd July 2007, 03:32
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.

http://www.lenzphoto.com/images/sc-1.jpg


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

guilugi
2nd July 2007, 12:47
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 ;-)

devilj
2nd July 2007, 16:42
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.

devilj
2nd July 2007, 21:27
OK this is what I have so far:

I decided to create a test application.

I created a QButtonGroup encompassing 4 checkboxes:


QButtonGroup *buttonGroup = new QButtonGroup(this);
buttonGroup->setExclusive(0);
buttonGroup->addButton(checkBox_1, 1);
buttonGroup->addButton(checkBox_2, 2);
buttonGroup->addButton(checkBox_3, 3);
buttonGroup->addButton(checkBox_4, 4);

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


void Window::reset()
{
for (int i = 1; i < 5; i++)
buttonGroup.button(i)->setCheckState(0);
}

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

guilugi
2nd July 2007, 22:11
Yes, easy one :)

use buttonGroup-> instead of buttonGroup. !

;-)

devilj
2nd July 2007, 22:45
Ok, did that!
Thank you!

Next question?:rolleyes:

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,

wysota
2nd July 2007, 23:07
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).

devilj
3rd July 2007, 00:00
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:

#ifndef WINDOW_H
#define WINDOW_H
#include <QDialog>

class QLabel;
class QCheckBox;
class QPushButton;
class QButtonGroup;

class Window : public QDialog
{
Q_OBJECT

public:
Window(QWidget *parent = 0);

private slots:
void reset();

private:
QPushButton *createButton(const QString &text, const char * member);
QLabel *label;
QButtonGroup *buttonGroup;
QCheckBox *checkBox_1;
QCheckBox *checkBox_2;
QCheckBox *checkBox_3;
QCheckBox *checkBox_4;
QPushButton *resetButton;

};

#endif

window.cpp:

#include <QtGui>
#include "window.h"

Window::Window(QWidget *parent)
: QDialog(parent)
{
resetButton = createButton(tr("Reset"), SLOT(reset()));

QCheckBox *checkBox_1 = new QCheckBox(tr("Backgrounds..."));
QCheckBox *checkBox_2 = new QCheckBox(tr("Cell Phones..."));
QCheckBox *checkBox_3 = new QCheckBox(tr("Automotive..."));
QCheckBox *checkBox_4 = new QCheckBox(tr("Computers..."));

QButtonGroup* buttonGroup = new QButtonGroup(this);
buttonGroup->setExclusive(0);
buttonGroup->addButton(checkBox_1, 1);
buttonGroup->addButton(checkBox_2, 2);
buttonGroup->addButton(checkBox_3, 3);
buttonGroup->addButton(checkBox_4, 4);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(checkBox_1);
layout->addWidget(checkBox_2);
layout->addWidget(checkBox_3);
layout->addWidget(checkBox_4);
layout->addWidget(resetButton);
setLayout(layout);
}

QPushButton *Window::createButton(const QString &text, const char *member)
{
QPushButton *button = new QPushButton(text);
connect(button, SIGNAL(clicked()), this, member);
return button;
}

void Window::reset()
{
for (int i=1;i < 5; i++)
buttonGroup->button(i)->setChecked(0);

}

Finally, a simple main.cpp:


#include <QApplication>
#include "window.h"

int main(int argc, char * argv[])
{
QApplication app(argc, argv);
Window window;
window.show();
return app.exec();
}

Any suggestions?

Thank you very much!

wysota
3rd July 2007, 00:02
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.

devilj
3rd July 2007, 00:29
IT WORKS!!!
Dziekuje, Wysota
and Sto Lat

guilugi
3rd July 2007, 07:16
Huu lol, what the hell does it mean ? :-p

wysota
3rd July 2007, 07:41
IT WORKS!!!
Dziekuje, Wysota
and Sto Lat

Thx.


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

It's "Thank you" and "Live Long" in Polish :)

guilugi
3rd July 2007, 10:32
Okay, thanks for the translation :-D