Results 1 to 16 of 16

Thread: Using container class with my own classes.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Using container class with my own classes.

    I am working on a check printing software. (it will take input like name, number and amount, and store it , and also print it to a check). I have thought to go about it like this:

    I will make a MY_DATA class which will hold string-name,float-amount,string-data. if the user clicks on add, and save, the object will save to a container class. I will write the container class to a file on the computer, and it will read from it every time it opens.

    Now, which container class to use ? (I read somewhere that QList can be used with custom classes, but I don't know how to use it).

    also, other approaches, and suggestions as to how to get this task done are welcome.

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using container class with my own classes.

    QList and similar classes are template<class T>. For example, your class. You should experience no problems with QList.

  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: Using container class with my own classes.

    Qt Code:
    1. struct Cheque {
    2. ...
    3. };
    4. QList<Cheque> cheques;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Using container class with my own classes.

    but how do I initialize variables and functions?

  5. #5
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using container class with my own classes.

    What do you want to initialize? When the container is created, it is empty. Now, you will add your items to the container. Every time you create an item, you initialize it and then you add the item to the container. But all this is basic C++ and not Qt.

  6. #6
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Using container class with my own classes.

    suppose I have this class :
    class test
    {



    public:
    int a;
    void lol(int t)
    {
    a=t;

    }
    };
    and I use
    QList<test>list;
    how to give the value of test.a at 0 ?
    and, how to use the member functions of the class for the object at 0;

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

    Default Re: Using container class with my own classes.

    Qt Code:
    1. list[0].a = 7;
    2. list[0].lol(7);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using container class with my own classes.

    Qt Code:
    1. class test
    2. {
    3. public:
    4.  
    5. int a;
    6.  
    7. void lol(int t)
    8. {
    9. a=t;
    10. }
    11. };
    12.  
    13. QList<test> list;
    14. test *pp = new test; // or another instance of test created otherwise
    15.  
    16. pp->lol(value);
    17. list.append(*pp);
    18.  
    19. // accessing list
    20.  
    21. test someitem = list.at(0); // read-only access to list
    22. list[0].lol(othervalue); // read/wrie access to list
    23. list.removeAt(0); // remove item 0 from the list, item 0 gets deleted
    24. someitem = list.takeAt(0); // remove item 0 from the list [i]and return it[/i], item 0 gets deleted from the list, indexes get shifted
    25. list.clear(); // delete all items in list
    26.  
    27. // etc. etc. See QList documentation
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Using container class with my own classes.

    the program is crashing.
    :an unhandled win32 exception occurred in untitled.exe[7476]

    I used this code :


    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    class test
    {



    public:
    int a;
    void lol(int t)
    {
    a=t;

    }
    };

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QList<test> list;

    list[4].a=4;






    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    P.S:
    I am using a static build of qt.
    Last edited by harvey_slash; 2nd October 2013 at 08:07. Reason: updated contents

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

    Default Re: Using container class with my own classes.

    Your list is empty and you are trying to access an element at position 4. Have you ever used a list before? E.g. std::list?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Using container class with my own classes.

    no, I have not.
    tell me how to initialize my object with a=5 at 0th element of list.

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

    Default Re: Using container class with my own classes.

    Qt Code:
    1. object o;
    2. o.a = 5;
    3. list.append(o);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. The following user says thank you to wysota for this useful post:

    harvey_slash (2nd October 2013)

  14. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using container class with my own classes.

    Quote Originally Posted by Radek View Post
    Qt Code:
    1. test *pp = new test; // or another instance of test created otherwise
    To copy to clipboard, switch view to plain text mode 
    Memory leak and totally unnecessary
    Allocate on the stack
    Qt Code:
    1. test pp;
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  15. #14
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Using container class with my own classes.

    thank you. got it working.
    understood that its the same thing as using STL

  16. #15
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using container class with my own classes.

    No memory leak, anda_skoa but an example. See the comment at the statement.

  17. #16
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using container class with my own classes.

    Quote Originally Posted by Radek View Post
    No memory leak, anda_skoa but an example. See the comment at the statement.
    new without delete -> leak

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 23rd October 2012, 09:46
  2. Replies: 5
    Last Post: 13th July 2011, 05:23
  3. List-based class container for Gui?
    By naturalpsychic in forum Qt Programming
    Replies: 10
    Last Post: 29th January 2011, 12:59
  4. Replies: 3
    Last Post: 7th August 2009, 10:21
  5. wanted to re-instantiate two classes from a class
    By salmanmanekia in forum General Programming
    Replies: 2
    Last Post: 22nd August 2008, 08: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
  •  
Qt is a trademark of The Qt Company.