PDA

View Full Version : getting QGraphichItem objct from custom class



szisziszilvi
23rd May 2011, 11:00
Hi,

I have a class which contains two objects whose classes are both derived from QGraphicsItem. I would like to reach them outside without setting these members public. In codes speaking it is something like this:


class myClass
{
public: // ... and here I need some functions like getVar1() or getVat2()
private:
// ...
c1 * var1; // c1 : public QGraphicsItem
c2 * var2; // c2 : public QGraphicsItem
}


It could be also fine to get them in a QGraphichScene object, but a function like below does not work because the copy constructor is private.


QGraphicsScene myClass::getMyScene()
{
QGraphicsScene temp;
temp.addItem(var1);
temp.addItem(var2);
return temp;
}


I've also tried this:


void setMyScene(QGraphicsScene & sc)
{
sc.addItem(var1);
}

but this does not seem to do anything to a "QGraphicsScene scene" variable. Frankly speaking it work like this:


QGraphicsScene * scene = new QGraphicsScene;
di->setMyScene(scene[0]);
ui->graphicsView->setScene(scene);

But this solution does not really look correct to me. Well, works, but still...
What would you suggest?

high_flyer
23rd May 2011, 16:00
I would like to reach them outside without setting these members public.
If there is only one place, or one class that needs to access these privates, then you can use the friend keyword for that class.
If you need to access it from more than that, you probably have a design error, and should not use private there, or in turn, rethink why you need to access these privates from outside.
EDIT: for some reason I thought your getter and setter method should be private.
After reading again I see that only your member are private, so you can disregard the above statement.


QGraphicsScene myClass::getMyScene()
{
QGraphicsScene temp;
temp.addItem(var1);
temp.addItem(var2);
return temp;
}

I could not make any sense of the above code.
You are talking about retrieving QGraphicsObject 's but return a temp scene??

Even more anigmatic is your second code segment with setMyScene().



QGraphicsScene * scene = new QGraphicsScene;
di->setMyScene(*scene /*scene[0]*/); //what is 'di'?
ui->graphicsView->setScene(scene); //that line of code is the only one making any sense in your code segments so far

Maybe if you explain what it is you are trying to achieve we can help you more.

szisziszilvi
24th May 2011, 08:03
yes, you are right, I didn't explain enough. "di" is a myClass object. "Outside" means basically "anything out of the class", in my application this would be like in codes above, so something else should get the private variables.
So the code is like this:



class c1 : public QGraphicsItem
{
//...
}
class c2 : public QGraphicsItem
{
//...
}

class myClass
{
private:
c1* var1;
c2* var2;
public:
myClass();
~myClass();
// ... (other public functions)
void setMyScene(QGraphicsScene & qgs); // this one works as shown above
};


and where I have a myClass object:



QGraphicsScene * scene = new QGraphicsScene;
myClass * mc = new myClass;
// ... setting var1 and var2
mc->setMyScene(*scene); // works
ui->graphicsView->setScene(scene);


What do I minunderstand in the preveously shown getMyScene?
Anyway there should be a better way.

high_flyer
24th May 2011, 08:48
"Outside" means basically "anything out of the class",
Then create a public setter and getter for your private members, which I guess is what you have tried to do in the code snippets you posted.

But as I said, you code and what you write in words does not match, so I have hard time following what it is you want to do.
Assuming that what you want is:

I have a class which contains two objects whose classes are both derived from QGraphicsItem. I would like to reach them outside without setting these members public.

Then something like the following would do the job:


class myClass
{
public: // ... and here I need some functions like getVar1() or getVat2()
const c1 & getC1()const
{
return var1;
}

const c2 & getC1()const
{
return var2;
}

void setC1(const c1 &otherC1)
{
var1 = otherC1;
}

void setC2(const c2 &otherC2)
{
var2 = otherC2;
}
private:
// ...
c1 * var1; // c1 : public QGraphicsItem
c2 * var2; // c2 : public QGraphicsItem
}

szisziszilvi
24th May 2011, 10:33
I highly appreciate your effort trying to help me.
Your solution almost works, but gives this error message:
" invalid initalization of redference of type 'cont c1&' from expression of type 'c1* const' "

These are ok:
const c1 * getC1() const { return val1;}
const c1 & getC1() const { return *val1;}

Which is better?

high_flyer
24th May 2011, 10:58
Your solution almost works, but gives this error message:
" invalid initalization of redference of type 'cont c1&' from expression of type 'c1* const' "
Sorry, I missed that your variables are pointers.


These are ok:
const c1 * getC1() const { return val1;}
const c1 & getC1() const { return *val1;}
In this case the first option is better, since you can return a NULL pointer if it is not initialized.
The second option prerequisites that the pointer is initialized, which is dangerous.

P.S
This is basic C++.
Be sure to learn C++ first, before working with Qt, you will constantly stumble on such problems.

wysota
24th May 2011, 11:03
Please post threads that are related to C++ syntax and design in "General Programming" and not in "Qt Programming" forum, even though you are using some Qt class in your code.

szisziszilvi
24th May 2011, 12:38
yes, you are perfectly right about this, but my problem raised whilst using qt-related classes and I tried the pasics in C++ and the error messages suggested me that the point would be around some feature of the Qt-class QGraphicsView from which I derived some other classes.

wysota
24th May 2011, 14:59
If your issue remains valid when replacing "QGraphicsScene" with "Foo" and "QGraphicsItem" with "Bar" then it is not a Qt-related issue and belongs to the General Programming forum.