Results 1 to 3 of 3

Thread: adding objects

  1. #1
    Join Date
    May 2012
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default adding objects

    I'm currenting writing a mastermind game. currently i'm having problems on the pushbutton for new game.
    there is a spinbox for the secretcode length. and this would send a signal to the secretcode frame slot telling it what length the user wants and when the new game button is pressed the default code would be deleted and the new secret code legth would be generated.
    I've managed to delete the default code, but cant seem to go about adding back the peg objects for the new length.
    I'm getting QLayout: Attempting to add QLayout "" to Secret "", which already has a layout in my terminal whenever i click on new game button. i'm thinking this has to do with me trying to add another layout for the new peg objects when the new game is pressed, but cant seem to figure out how to change this.


    heres my code
    Qt Code:
    1. Secret::Secret(QWidget *parent,int j): QWidget(parent), currentLength(j)
    2. {
    3. QHBoxLayout *layout= new QHBoxLayout(this);
    4. for(int i = 0; i<currentLength; i++)
    5. {
    6. Peg *peg= new Peg(this);
    7. pegv.push_back(peg);
    8. layout->addWidget(peg);
    9. }
    10. }
    11.  
    12. void Secret::changeCode(const QString x)
    13. {
    14. currentLength=x.toInt();
    15. }
    16.  
    17. void Secret::newGame()
    18. {
    19. while(pegv.size()>0)
    20. {
    21. Peg* temp=pegv.back();
    22. delete temp;
    23. pegv.pop_back();
    24. }
    25. QHBoxLayout *layout= new QHBoxLayout(this);
    26. for(int i = 0; i<currentLength; i++)
    27. {
    28. Peg *peg= new Peg(this);
    29. pegv.push_back(peg);
    30. layout->addWidget(peg);
    31. }
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: adding objects

    this->setLayout(0); // I'm guessing that this will remove the previous layout.



    fyi: you shouldn't be deleteing your peg widgets like that. Use deleteLater() instead.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    May 2012
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: adding objects

    this->setLayout(0);
    is giving me this error when i press new game.
    QWidget::setLayout: Cannot set layout to 0
    QLayout: Attempting to add QLayout "" to Secret "", which already has a layout

    I'm wondering if I can just delete the pegs but use the same layout to add new pegs to it?

Similar Threads

  1. Need an advise about adding objects to vector-
    By tonnot in forum General Programming
    Replies: 4
    Last Post: 29th August 2011, 18:10
  2. 3d objects in QT
    By waman.prabhu in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2011, 10:11
  3. Replies: 3
    Last Post: 9th January 2010, 15:47
  4. Replies: 11
    Last Post: 25th February 2009, 17:35
  5. Replies: 7
    Last Post: 18th July 2006, 21:33

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.