How to make a QVector of a class usable for ui?
First things first: I'm very new in Qt, so it definately can be a stupid question.
I tried to make a QVector that contains Objects of a specific class. Problem here:
- I cant append dinamically new items (or I am just too stupid) like
myVector.append( new myClass(this) );
- I cant use the items from the QVector to setup ui like this:
ui->myGraph->addTab( myVector.last(), "xyz" );
(I also tried to do this stuff with a QVector of pointers etc. but I just got more problems)
Thanks for answers!
You can store pointers to objects this way. In example:
MyClass *myObj = new MyClass(this); myVector.append(myObj);
However the question is: what exactly would you like to get from 2)? I mean - what's your use case?
@Niagarer You should show more code: what is myVector exactly?
"I cant append dinamically new items (or I am just too stupid) like" - what exactly is the problem/error?
This post is deleted!
Thanks for the answer!
The problem here is, that I want to add new items dynamically. When I click a button, it should add a new item.
because of that, I tried to do it the way I showed above (with no specific name, because I dont want to change an existing item, I want to create a new one).
But I still can not append it to my vector. it says:
error: no matching function for call to 'QVector<myClass>::append(myClass*&)' myClasses.append(testMyClass);
The second problem is, that it says
error: no matching function for call to 'QTabWidget::addTab(myClass&, QString&)' ui->Graph->addTab(myClasses.last(), "xyz");
when I try to use the last item of myClasses to set the widget (myClass is a widgetClass) of a new tab.
@Niagarer you haven't jet shown us, how you create/define your QVector.
something like this:
QVector<QWidget*> myVector; QWidget *w = new QWidget(..); myVector.append(w);
Yes, I'm sorry.
myVector is a vector of myClass objects.
myClass is a custom QWidget Class.
I want to create a new object of myClass when I add a new tab in the window and store it in the QVector.
You cannot store the actual widget in the list. must be with new and pointer.
myVector is a vector of myClass objects
then why are you trying to append pointers?
It should be:
as @J-Hilk said.
Be aware that you should really use pointers as QObjects are not copyable.
Ok, thank you all!
So, as you told me, I just had to use pointers.
When I useed normal objects (no pointers), I was not able to add the new item to the vector and use it as widget for the new tab.
The tab takes pointers, I'm sorry, I didn't see that (but, yup it makes sense).
So now, it looks like:
QVector<myClass*> myClasses; myClass *testMyClass = new myClass(this); myClasses->append(testMyClass); ui->Graph->addTab( myClasses.last(), "xyz");
and it works yaay!
Thanks for all answers!