Results 1 to 4 of 4

Thread: How to go about saving?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to go about saving?

    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?

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to go about saving?

    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.

    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. public:
    4. MainWindow() {
    5. QGridLayout* gl = new QGridLayout();
    6. for (int i = 0;i < 108;++i)
    7. {
    8. QSpinBox* sb = new QSpinBox()
    9. payments_sb.append(sb);
    10. gl.addWidget(de,i,0);
    11.  
    12. QDateEdit* de = new QDateEdit();
    13. dueDates_de.append(de);
    14. gl.addWidget(de,i,1);
    15. }
    16. QWidget* w = new QWidget();
    17. w.setLayout(gl);
    18. setCentralWidget(w);
    19. }
    20. void writeToStream(QDataStream& ds)
    21. {
    22. ds << payments_sb.length();
    23. for (int i=0;i<payments_sb.length();++i)
    24. {
    25. ds << payments_sb.at(i).value() << dueDates_de.at(i) ... ;
    26. }
    27. }
    28. private:
    29. QList<QSpinBox* > payments_sb;
    30. QList<QDateEdit* > dueDates_de;
    31. }
    32.  
    33. ..
    To copy to clipboard, switch view to plain text mode 
    Or use a QTableWidget or View with custom delegates for your datatypes.

    Joh

  3. #3
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to go about saving?

    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...)
    Last edited by TomJoad; 5th April 2011 at 23:06.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to go about saving?

    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!

    Qt Code:
    1. int i = 1;
    2. do
    3. {
    4. de = parentWidget->findChild<QDateEdit *>(QString("dateEdit_%1").arg(i));
    5. if (de) {
    6. ...
    7. }
    8. ++i;
    9. } while (de);
    To copy to clipboard, switch view to plain text mode 
    Joh

Similar Threads

  1. Saving QImage
    By thgis in forum Qt Programming
    Replies: 5
    Last Post: 1st November 2010, 09:01
  2. Saving a QtFile
    By deeee in forum Qt Programming
    Replies: 4
    Last Post: 27th May 2010, 00:51
  3. Saving as XML
    By rakkar in forum Newbie
    Replies: 2
    Last Post: 30th October 2009, 17:17
  4. Saving QTextEdit
    By lixo1 in forum Qt Programming
    Replies: 5
    Last Post: 22nd March 2009, 16:51
  5. Saving settings to XML
    By arturo182 in forum Qt Programming
    Replies: 2
    Last Post: 8th March 2009, 11:10

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
  •  
Qt is a trademark of The Qt Company.