Reply
Thread Tools
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#11
Originally Posted by saxen View Post
ok with 2) i've solve problem with >> operator...but anything else is working xD

i'm sure is a reference/pointer stuff. i mean, i'm adding to my QList<Service> a reference Service service in a method called "addItems". Will it be this reference deleted once the addItems is over even if it's now linked in my list?!

so should i use Service * service = new Service() and then add with list->append(*service) ?!
Either you have a list of pointers or a list of what those pointer would have pointed at:

Code:
| pointer | pointer | pointer |

or

| Service | Service | Service |
As Venemo says, saving pointers to memory that you no longer have allocated makes no sense (the first case) but to actually save the Service objects, with everything that's in them (the second case) makes more sense.
 

The Following User Says Thank You to Joorin For This Useful Post:
Venemo's Avatar
Posts: 1,296 | Thanked: 1,773 times | Joined on Aug 2009 @ Budapest, Hungary
#12
Originally Posted by saxen View Post
ok with 2) i've solve problem with >> operator...but anything else is working xD

i'm sure is a reference/pointer stuff. i mean, i'm adding to my QList<Service> a reference Service service in a method called "addItems". Will it be this reference deleted once the addItems is over even if it's now linked in my list?!

so should i use Service * service = new Service() and then add with list->append(*service) ?!
No. Don't get confused with this.
When you append an item to a collection in such a way, or assign it to a variable, it gets copied. (Its copy constructor or operator= gets called.)

Eg.
Code:
MyClass item; //creating a new item
item.stuff = 42;
MyClass item2 = item; //behind the scenes, item2 copies item
item.stuff = 8; //this line doesn't change item2
Note that by default, the MyClass type can't be copied.
You need to tell HOW it is gonna be copied.
That's why there is a copy constructor or an operator=.

Basically,
Code:
item2 = item;
is the same as
Code:
item2.operator=(item);
and only works if there is a
Code:
MyClass::operator=(const MyClass& other) { ... }
method in your class.

And
Code:
MyClass item2 = item;
is the same as
Code:
MyClass item2(item);
and only works if there is a
Code:
MyClass::MyClass(const MyClass& other) { ... }
constructor in your code.

Last edited by Venemo; 2010-06-03 at 23:06.
 

The Following User Says Thank You to Venemo For This Useful Post:
Posts: 124 | Thanked: 10 times | Joined on Jan 2007 @ Italy
#13
Originally Posted by Venemo View Post
No. Don't get confused with this.
When you append an item to a collection in such a way, or assign it to a variable, it gets copied. (Its copy constructor or operator= gets called.)

Eg.
Code:
MyClass item; //creating a new item
item.stuff = 42;
MyClass item2 = item; //behind the scenes, item2 copies item
item.stuff = 8; //this line doesn't change item2
even when i get from collection?!

ok that's the point, i've a list of Service in my main classes. i've to share this list with other classes, like "EditService". how can i pass list, or elements, to other class in order to make them change original list?!

with the old list of pointer it worked well(they actually changed value of original list), but as u said save pointer doesn't make sense. so what i'm supposed to do?!

back with pointer list changing saving function?! or there are other solutions!?
 
Venemo's Avatar
Posts: 1,296 | Thanked: 1,773 times | Joined on Aug 2009 @ Budapest, Hungary
#14
even when i get from collection?!
Yes, exactly.
BTW, Qt's collections give back const references to items stored within them.

Originally Posted by saxen View Post
ok that's the point, i've a list of Service in my main classes. i've to share this list with other classes, like "EditService". how can i pass list, or elements, to other class in order to make them change original list?!
You can pass around a QList<Service>* to achieve that.
Note that QList<Sevice*> is NOT QList<Service>* !!

(BTW, I edited my previous post to make it more clear how C++ works.)
 
Posts: 124 | Thanked: 10 times | Joined on Jan 2007 @ Italy
#15
Originally Posted by Venemo View Post
Yes, exactly.
BTW, Qt's collections give back const references to items stored within them.

You can pass around a QList<Service>* to achieve that.
Note that QList<Sevice*> is NOT QList<Service>* !!

(BTW, I edited my previous post to make it more clear how C++ works.)
yes thanks now is more clear

gri said to not use list in heap, so no QList<Service>* or not!?


if i use it like this(not real code):

main class:
QList<Service> * services;
EditService edit(services);

EditService class:
Service service = services.at(0);
service.changeSomething();


service in QList has not be really changed...am i right!? so the only solution is user a list of pointer
 
Venemo's Avatar
Posts: 1,296 | Thanked: 1,773 times | Joined on Aug 2009 @ Budapest, Hungary
#16
Originally Posted by saxen View Post
service in QList has not be really changed...am i right!? so the only solution is user a list of pointer
Nooo...

Okay. You have a pointer to a list that contains items.

If you give a pointer to someone, it can do whatever it wants to the memory where the pointer points to.

So, basically:
yes, you can change the collection this way.

Again:
Feel free to check how I do this in my own app.
(Hint: CharacterListWindow, EveRefresher, and EveConnector all share a QList<EveCharacter>* and do stuff with it all the time.)
 
Posts: 124 | Thanked: 10 times | Joined on Jan 2007 @ Italy
#17
ok i'll give a look to your app! thanks again, i'll let you know
 
Posts: 124 | Thanked: 10 times | Joined on Jan 2007 @ Italy
#18
i dunno how, but now it works :P
jk, i know how...i've change a bit the original code

Now i've a QList<Service*> services, so no list in the heap(as gri suggested). I still have a list of pointer but change code in order to save object pointed instead of pointer(as it logically should be).

In service class now i've:
Q_DECLARE_METATYPE(Service);
Q_DECLARE_METATYPE(Service*);

the first is necessary to save object:
Code:
 QByteArray array;
       QDataStream data(&array, QIODevice::ReadWrite);
       data << services.size();
       foreach(Service* service, services){
         data << *service;
     }
and load:
Code:
QVariant var = settings->value("services");
        QByteArray array;
        array =var.value<QByteArray>();
        QDataStream data(&array,QIODevice::ReadWrite);
        int size;
        data>> size;
       for(int i=0; i<size;i++){
            Service *service = new Service();
            data >> *service;
            services.append(service);
        }
the second one is to set data in QListWidget:
Code:
 QListIterator<Service*> i(services);
    while(i.hasNext()){
        Service *service = new Service();
        service = i.next();
        QListWidgetItem * item= new QListWidgetItem(service->getName());
        item->setSizeHint(QSize(300, 100));
        item->setTextAlignment(Qt::AlignCenter);
       QVariant var;
        var.setValue(service);
        item->setData(Qt::UserRole, var);
        ui->listWidget->addItem(item);
    }
and share pointer to other classes:
Code:
Service *service= new Service();
    service = (item->data(Qt::UserRole).value<Service*>());
    ConfigWindow *configwindow = new ConfigWindow(service, this);
    configwindow->show();
using a list of pointer i can't use << and >> operator with list, so i've to manage single elements!
maybe it's not pretty elegant, but works well

thanks to all for help

ps. venemo your code will be usefull when i'll get into "user interface", i've seen interesting code about rotation management
 
Posts: 190 | Thanked: 129 times | Joined on Mar 2010 @ Bavaria, Germany
#19
Originally Posted by saxen View Post
i've seen interesting code about rotation management
I don't know what you've seen but you could also take a look at the orientation helper class and example I wrote. Maybe it helps you

https://garage.maemo.org/plugins/scm...ms&view=markup (usage)
https://garage.maemo.org/plugins/scm...ms&view=markup (orientation helper class)

Screenshots:

 
Posts: 124 | Thanked: 10 times | Joined on Jan 2007 @ Italy
#20
really nice code gri, i think i'm gonna try it :P
btw i've a question about ui...how to set window size?! i mean max res is 800*480 or 480*800 but what's about status bar?! :S
 
Reply


 
Forum Jump


All times are GMT. The time now is 06:56.