|
|
2010-06-03
, 22:53
|
|
Posts: 726 |
Thanked: 345 times |
Joined on Apr 2010
@ Sweden
|
#11
|
| The Following User Says Thank You to Joorin For This Useful Post: | ||
|
|
2010-06-03
, 23:02
|
|
|
Posts: 1,296 |
Thanked: 1,773 times |
Joined on Aug 2009
@ Budapest, Hungary
|
#12
|
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) ?!
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
item2 = item;
item2.operator=(item);
MyClass::operator=(const MyClass& other) { ... }
MyClass item2 = item;
MyClass item2(item);
MyClass::MyClass(const MyClass& other) { ... }
| The Following User Says Thank You to Venemo For This Useful Post: | ||
|
|
2010-06-03
, 23:08
|
|
Posts: 124 |
Thanked: 10 times |
Joined on Jan 2007
@ Italy
|
#13
|
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


|
|
2010-06-03
, 23:13
|
|
|
Posts: 1,296 |
Thanked: 1,773 times |
Joined on Aug 2009
@ Budapest, Hungary
|
#14
|
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?!
|
|
2010-06-03
, 23:21
|
|
Posts: 124 |
Thanked: 10 times |
Joined on Jan 2007
@ Italy
|
#15
|
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.)


|
|
2010-06-03
, 23:50
|
|
|
Posts: 1,296 |
Thanked: 1,773 times |
Joined on Aug 2009
@ Budapest, Hungary
|
#16
|
service in QList has not be really changed...am i right!? so the only solution is user a list of pointer
|
|
2010-06-04
, 11:27
|
|
Posts: 124 |
Thanked: 10 times |
Joined on Jan 2007
@ Italy
|
#17
|
|
|
2010-06-04
, 12:23
|
|
Posts: 124 |
Thanked: 10 times |
Joined on Jan 2007
@ Italy
|
#18
|

QByteArray array;
QDataStream data(&array, QIODevice::ReadWrite);
data << services.size();
foreach(Service* service, services){
data << *service;
}
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);
}
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);
}
Service *service= new Service();
service = (item->data(Qt::UserRole).value<Service*>());
ConfigWindow *configwindow = new ConfigWindow(service, this);
configwindow->show();


|
|
2010-06-04
, 14:45
|
|
Posts: 190 |
Thanked: 129 times |
Joined on Mar 2010
@ Bavaria, Germany
|
#19
|


|
|
2010-06-04
, 15:32
|
|
Posts: 124 |
Thanked: 10 times |
Joined on Jan 2007
@ Italy
|
#20
|