Results 1 to 10 of 10

Thread: General (Q)Vector questions

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default General (Q)Vector questions

    Hi!
    I want to manage several dynamically created QSliders in a QVector.
    So we are talking of
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyWidget();
    7. ~MyWidget();
    8.  
    9. private:
    10. QVector<QSlider> m_sliders;
    11. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt Code:
    1. MyWidget::MyWidget()
    2. {
    3. for (int i=0 ; i<5 ; i++)
    4. {
    5. m_sliders.append(QSlider(Qt::Horizontal, this));
    6. m_sliders[i].setRange(0,100);
    7. m_sliders[i].setPageStep(20);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    But how can I remove them???

    Thanks for your hints in advance,
    regards

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: General (Q)Vector questions

    Quote Originally Posted by olidem View Post
    But how can I remove them???
    iterator QVector::erase ( iterator pos )

  3. #3
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: General (Q)Vector questions

    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.

    Qt Code:
    1. m_sliders.append(QSlider(Qt::Horizontal, this));
    To copy to clipboard, switch view to plain text mode 

    does not compile.

    I think I have to use pointers.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: General (Q)Vector questions

    Quote Originally Posted by olidem View Post
    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)

  5. #5
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: General (Q)Vector questions

    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: General (Q)Vector questions

    Guessing they are on a layout just call removeWidget. for a QList it would be:
    Qt Code:
    1. // int i is the index to delete
    2. // QList<QSlider*> list;
    3. // QLayout *mylayout;
    4. myLayout->removeWidget(list.at(i));
    5. delete(list.takeAt(i));
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: General (Q)Vector questions

    Quote Originally Posted by Lykurg View Post
    Guessing they are on a layout just call removeWidget. for a QList it would be:
    Qt Code:
    1. // QLayout *mylayout;
    2. myLayout->removeWidget(list.at(i));
    To copy to clipboard, switch view to plain text mode 
    But this is just in case that I use a Layout manager, right?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: General (Q)Vector questions

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    olidem (16th March 2009)

  10. #9
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: General (Q)Vector questions

    Quote Originally Posted by wysota View Post
    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

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: General (Q)Vector questions

    Quote Originally Posted by olidem View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Compile general C++ program from Qt
    By srinivasj in forum Qt Programming
    Replies: 3
    Last Post: 30th December 2008, 08:19
  2. [java] general object questions
    By mickey in forum General Programming
    Replies: 1
    Last Post: 26th November 2008, 23:15
  3. Little confusion Regarding general Initilizaion..!!!
    By kunalnandi in forum General Programming
    Replies: 19
    Last Post: 11th October 2008, 12:29
  4. Little confusion Regarding general Initilizaion.. :confused:
    By kunalnandi in forum General Programming
    Replies: 2
    Last Post: 19th September 2008, 10:06
  5. General Question about Qt4 Model/View
    By reed in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2008, 15:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.