PDA

View Full Version : Using container class with my own classes.



harvey_slash
1st October 2013, 17:45
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.

Radek
1st October 2013, 18:40
QList and similar classes are template<class T>. For example, your class. You should experience no problems with QList.

ChrisW67
1st October 2013, 21:20
struct Cheque {
...
};
QList<Cheque> cheques;

harvey_slash
2nd October 2013, 04:06
but how do I initialize variables and functions?

Radek
2nd October 2013, 06:05
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.

harvey_slash
2nd October 2013, 07:13
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;

wysota
2nd October 2013, 07:41
list[0].a = 7;
list[0].lol(7);

Radek
2nd October 2013, 07:47
class test
{
public:

int a;

void lol(int t)
{
a=t;
}
};

QList<test> list;
test *pp = new test; // or another instance of test created otherwise

pp->lol(value);
list.append(*pp);

// accessing list

test someitem = list.at(0); // read-only access to list
list[0].lol(othervalue); // read/wrie access to list
list.removeAt(0); // remove item 0 from the list, item 0 gets deleted
someitem = list.takeAt(0); // remove item 0 from the list and return it, item 0 gets deleted from the list, indexes get shifted
list.clear(); // delete all items in list

// etc. etc. See QList documentation

harvey_slash
2nd October 2013, 08:02
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.

wysota
2nd October 2013, 08:07
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?

harvey_slash
2nd October 2013, 08:17
no, I have not.
tell me how to initialize my object with a=5 at 0th element of list.

wysota
2nd October 2013, 08:52
object o;
o.a = 5;
list.append(o);

anda_skoa
2nd October 2013, 10:36
test *pp = new test; // or another instance of test created otherwise


Memory leak and totally unnecessary
Allocate on the stack


test pp;


Cheers,
_

harvey_slash
2nd October 2013, 12:14
thank you. got it working.
understood that its the same thing as using STL

Radek
2nd October 2013, 13:34
No memory leak, anda_skoa but an example. See the comment at the statement.

anda_skoa
2nd October 2013, 17:03
No memory leak, anda_skoa but an example. See the comment at the statement.
new without delete -> leak

Cheers,
_