PDA

View Full Version : General (Q)Vector questions



olidem
16th March 2009, 12:42
Hi!
I want to manage several dynamically created QSliders in a QVector.
So we are talking of


class MyWidget : public QWidget
{
Q_OBJECT

public:
MyWidget();
~MyWidget();

private:
QVector<QSlider> m_sliders;
}


First (general) Question:
Where are the elements of a vector really stored? On the stack or on the heap?

Second question:
I can create sliders via eg.


MyWidget::MyWidget()
{
for (int i=0 ; i<5 ; i++)
{
m_sliders.append(QSlider(Qt::Horizontal, this));
m_sliders[i].setRange(0,100);
m_sliders[i].setPageStep(20);
}
}

But how can I remove them???

Thanks for your hints in advance,
regards

Lykurg
16th March 2009, 12:47
But how can I remove them???
iterator QVector::erase ( iterator pos )

olidem
16th March 2009, 12:54
Hmm, ok, what is the result for the gui?
Shouldn"t I have to deregister the slider? The paint-loop should come to this slider at some point. Also the connected signals and slots should be informed.

Remark: the code above does not even compile.


m_sliders.append(QSlider(Qt::Horizontal, this));

does not compile.

I think I have to use pointers.

Lykurg
16th March 2009, 13:15
I think I have to use pointers.

That's a good decision. I thought you only want to know how to remove something form a QVector. And by the way, why you are using QVector and not g.e. QList?
If working with QVector<QSlider*> be aware of deleting the object with delete before erasing it from the vector.

Edit: And of course you have to delete it yourself from a layout and slots etc... (depending on your code)

olidem
16th March 2009, 13:46
Yes, this was my question.

Except for the connections, which can be disconnected, do I have to register a QSlider somewhere else, or is it enough to delete it?

Lykurg
16th March 2009, 14:00
Guessing they are on a layout just call removeWidget. for a QList it would be:

// int i is the index to delete
// QList<QSlider*> list;
// QLayout *mylayout;
myLayout->removeWidget(list.at(i));
delete(list.takeAt(i));

wysota
16th March 2009, 17:27
Just a side note - you can't have a vector of QSlider as widgets can't be copied. You can only keep pointers to Qt objects in vectors (as you already noticed).

olidem
16th March 2009, 18:19
Just a side note - you can't have a vector of QSlider as widgets can't be copied. You can only keep pointers to Qt objects in vectors (as you already noticed).
Ah, ok. I didn't know that. So, generally speaking, any child classes of QObject cannot be kept in Qts collections? what about std containers? Why exactly can"t they be kept in these containers? The compiler speaks about private (or non-public) copy constuctors.

Thank you for your clarifications in advance,

regards,
olli

olidem
16th March 2009, 18:20
Guessing they are on a layout just call removeWidget. for a QList it would be:


// QLayout *mylayout;
myLayout->removeWidget(list.at(i));

But this is just in case that I use a Layout manager, right?

wysota
16th March 2009, 18:41
So, generally speaking, any child classes of QObject cannot be kept in Qts collections?
Yes, that's correct.


what about std containers?
It's not the limitation of the container, it is a constraint on QObjects, so - no, you can't keep QObjects in std containers.


Why exactly can"t they be kept in these containers? The compiler speaks about private (or non-public) copy constuctors.

As I said, widgets (and QObjects in general) can't be copied (as they contain some unique data that would by definition stop being unique once you copied it) and containers work by storing copies of items. That's why they have private copy constructors and that's why you can't put them in containers.