PDA

View Full Version : How to go about saving?



TomJoad
5th April 2011, 21:37
I have made a simple interest calculator that takes into account payments made (up to 108). This means that I have 108 payment boxes, and 108 date edits.

I now want to save that information. What would be the best way to go about it? I thought about using Session Management (http://doc.trolltech.com/latest/session.html), but that doesn't appear to be the right answer. The most obvious way to do it would be to take the array information and have it stored as a csv and then have that csv parsed back in when the user wants to load. However, that would require me writing 200+ if statements (unless there is some way to write a for statement that would also change the numbering of the spinboxes and date edits).

Are there any other ways to go about solving this problem?

JohannesMunk
5th April 2011, 23:13
You have manually created 108 spinboxes and date edits?

You would have been much better off to declare them in a QList in the first place. Then you can easily iterate through them.



class MainWindow : public QMainWindow
{
public:
MainWindow() {
QGridLayout* gl = new QGridLayout();
for (int i = 0;i < 108;++i)
{
QSpinBox* sb = new QSpinBox()
payments_sb.append(sb);
gl.addWidget(de,i,0);

QDateEdit* de = new QDateEdit();
dueDates_de.append(de);
gl.addWidget(de,i,1);
}
QWidget* w = new QWidget();
w.setLayout(gl);
setCentralWidget(w);
}
void writeToStream(QDataStream& ds)
{
ds << payments_sb.length();
for (int i=0;i<payments_sb.length();++i)
{
ds << payments_sb.at(i).value() << dueDates_de.at(i) ... ;
}
}
private:
QList<QSpinBox* > payments_sb;
QList<QDateEdit* > dueDates_de;
}

..

Or use a QTableWidget or View with custom delegates for your datatypes.

Joh

TomJoad
6th April 2011, 00:00
Yes, I manually created them using QT Designer. They all go dateEdit_1, dateEdit_2, etc. Is there a way to cycle through them? If not, looks like some crying is in order as I will either have to do it manually, or just rework the program using QList (or some other solution).

(fuck it, I may just write a for statement that will output the needed code to a text document and then copy that into the source...)

JohannesMunk
6th April 2011, 00:19
Ok if you insist you can loop through the children of your mainwidget! But thats not really good design! But definitely better than hard coding!



int i = 1;
QDateEdit *de;
do
{
de = parentWidget->findChild<QDateEdit *>(QString("dateEdit_%1").arg(i));
if (de) {
...
}
++i;
} while (de);
Joh