Results 1 to 20 of 27

Thread: Dynamically created buttons.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamically created buttons.

    Quote Originally Posted by Tomasz View Post
    It will clear also verticalLayout?
    No, it won't.
    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.


  2. #2
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Dynamically created buttons.

    hi,
    void QVector::clear ()
    Removes all the elements from the vector and releases the memory used by the vector.
    u need to remove the widgets from the layout.
    use
    Qt Code:
    1. void QLayout::removeWidget ( QWidget * widget )
    To copy to clipboard, switch view to plain text mode 

    hope it helps
    Thnks
    Bala

  3. #3
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically created buttons.

    So, it is as I thought. But what if I do something like that:

    Qt Code:
    1. QVector<QPushButton*> btn;
    2.  
    3. btn.clear();
    4.  
    5. for(int j=0; j<HOW_MANY_BUTTONS; j++)
    6. {
    7. b->setGeometry(0,0,40,40);
    8. btn << b;
    9. ui->verticalLayout->addWidget(btn[j],Qt::AlignVCenter);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Can I then remove item from layout using:

    Qt Code:
    1. ui->verticalLayout->removeWidget(btn[i]);
    To copy to clipboard, switch view to plain text mode 

    Or maybe different pointer points on that widget?

    thanks in advance
    best regards
    Tomasz

  4. #4
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically created buttons.

    Can I then remove item from layout using ui->verticalLayout->removeWidget(btn[i]);
    yes surely u can

    Or maybe different pointer points on that widget?
    no its the same pointer

    hope it helps
    Bala

  5. The following user says thank you to BalaQT for this useful post:

    Tomasz (29th November 2010)

  6. #5
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically created buttons.

    But if I remove that widget from layout, pointer stays in vector? And I can use it again or just remove it from vector too?

    thanks in advance
    best regards
    Tomasz

  7. #6
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically created buttons.

    But if I remove that widget from layout, pointer stays in vector?
    yes pointer stays in vector, since u didnt remove it from Vector.

    And I can use it again
    yes, surely u can use it again.

    or just remove it from vector too?
    if u dont want that widget then remove it from vector too..

    layout and vector are different...

    for ur understanding ... vector is holding the widget pointer.
    that widget will be added to the layout by addWidget
    and removed by removeWidget

    hope it helps
    Bala

  8. The following user says thank you to BalaQT for this useful post:

    Tomasz (29th November 2010)

  9. #7
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically created buttons.

    One more question to end that thread. I've got my buttons, now I'm connecting them to QSignalMapper:

    Qt Code:
    1. QVector<QPushButton*> btn;
    2.  
    3. btn.clear();
    4.  
    5. for(int j=0; j<HOW_MANY_BUTTONS; j++)
    6. {
    7. b->setGeometry(0,0,40,40);
    8. btn << b;
    9. ui->verticalLayout->addWidget(btn[j],Qt::AlignVCenter);
    10. connect(btn[j], SIGNAL(clicked()), sigMap, SLOT(map()));
    11. sigMap->setMapping(btn[j], j);
    12. }
    13.  
    14. connect(sigMap, SIGNAL(mapped(int)), this, SLOT(func(int)));
    To copy to clipboard, switch view to plain text mode 

    What will now happen with QSignalMapper if I remove buttons from layout and vector? Should I clean somehow signal mapper? What happen with all connections?

    thanks in advance
    best regards
    Tomasz

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

    Default Re: Dynamically created buttons.

    If you "remove" buttons from the layout and vector nothing will happen and connections to the mapper will still be maintained. But if you delete the buttons, all their connections will be broken.

    From a technical point of view your vector doesn't hold buttons, it holds pointers to buttons. So you are not removing buttons from the vector (or the layout as a matter of fact) but pointers to buttons.
    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.


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

    Tomasz (29th November 2010)

  12. #9
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically created buttons.

    So the safest way to do the cleaning is to: remove widgets from layout, then DELETE all created buttons with:

    Qt Code:
    1. delete(btn[x]); // btn is vector with pointers
    To copy to clipboard, switch view to plain text mode 

    and at the end - i can safely clear vector, and then connections will be broken, and QSignalMapper will be 'clean'? Yes? And then I can create everything again?

    thanks in advance
    best regards
    Tomasz

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

    Default Re: Dynamically created buttons.

    It's enough to delete the buttons. They will be taken out of layouts.

    Qt Code:
    1. qDeleteAll(btn);
    2. btn.clear();
    To copy to clipboard, switch view to plain text mode 
    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.


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

    Tomasz (29th November 2010)

  15. #11
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically created buttons.

    One more question - should I use disconnect() to disconnect all signals (from created buttons) from slots.

    thanks in advance
    best regards
    Tomasz

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

    Default Re: Dynamically created buttons.

    Quote Originally Posted by Tomasz View Post
    One more question - should I use disconnect() to disconnect all signals (from created buttons) from slots.
    Why don't you try it first with and without disconnecting and then answer your own question?
    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.


  17. #13
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Dynamically created buttons.

    hi,
    should I use disconnect() to disconnect all signals
    u should disconnect all the buttons.
    one button's disconnect will disconnect only that signal.

    hope it helps
    Bala

Similar Threads

  1. Dynamically creating buttons from Xml file
    By chandru@080 in forum Newbie
    Replies: 5
    Last Post: 25th November 2010, 10:34
  2. PaintEvent() in a dynamically created and destroyed objet not working.
    By savaliya_ambani in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 20th October 2010, 08:49
  3. Check has created with new
    By wirasto in forum Qt Programming
    Replies: 5
    Last Post: 16th March 2010, 16:02
  4. QMainWindow ignores dynamically created, floating QDockWidget
    By stefanadelbert in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2010, 01:06
  5. Line not being created
    By Kapil in forum Newbie
    Replies: 4
    Last Post: 30th March 2006, 06:49

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.