Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: Dynamically created buttons.

  1. #1
    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 Dynamically created buttons.

    Hello!

    I've got a problem with buttons and pointers. I need to create dynamically something like "array" of buttons on one of my application windows (each time the window opens). Now I've got some code:

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

    It works just fine. But number of buttons is changing, and I don't want to create array of pointers which is bigger or smaller than number of buttons. Any suggestions how should I write function which is creating as many buttons as I need in a moment?

    thanks in advance
    best regards
    Tomasz

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Dynamically created buttons.

    Use a std::vector instead of a fixed-size array. Or, simply let the layout do the bookkeeping, including destruction.

  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.

    Quote Originally Posted by SixDegrees View Post
    Use a std::vector instead of a fixed-size array. Or, simply let the layout do the bookkeeping, including destruction.
    I'll read about std::vector. But what about that layout? How should it work?

    thanks in advance
    best regards
    Tomasz

  4. #4
    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.

    I'm trying to use std::vector but, can I have pointers as elements of vector? How can add buttons to vector?

    thanks in advance
    best regards
    Tomasz

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

    Default Re: Dynamically created buttons.

    Qt Code:
    1. QVector<QPushButton*> btn; // or std::vector
    2.  
    3. for(int j=0; j<HOW_MANY_BUTTONS; j++)
    4. {
    5. b->setGeometry(0,0,40,40);
    6. ui->verticalLayout->addWidget(b,Qt::AlignVCenter);
    7. btn << b;
    8. }
    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.


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

    Tomasz (29th November 2010)

  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.

    hi ,
    u can use QHash too.
    i will use like this ,

    Qt Code:
    1. QHash<int,QPushButton*> btn;
    2.  
    3. int HOW_MANY_BUTTONS=200; //u can change this value dynamically as u wish;
    4. for(int j=0; j<HOW_MANY_BUTTONS; j++)
    5. {
    6. btn[j]=b;
    7. b->setGeometry(0,0,40,40);
    8. ui->verticalLayout->addWidget(b,Qt::AlignVCenter);
    9. }
    To copy to clipboard, switch view to plain text mode 

    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
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Dynamically created buttons.

    Note also, in addition to the above, that a QLayout itself acts much like a vector: it has count(), addItem(), removeItem() and itemAt() functions that might serve just as well as using an external vector to keep track of the items managed by the layout. It isn't really possible to say whether this approach would work without knowing more about your application, but sometimes it allows you to use a single object - the layout - rather than two - the layout plus an external container.

  10. #8
    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.

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. QVector<QPushButton*> btn; // or std::vector
    2.  
    3. for(int j=0; j<HOW_MANY_BUTTONS; j++)
    4. {
    5. b->setGeometry(0,0,40,40);
    6. ui->verticalLayout->addWidget(b,Qt::AlignVCenter);
    7. btn << b;
    8. }
    To copy to clipboard, switch view to plain text mode 
    So, if I want to clear that vector I can use:

    Qt Code:
    1. btn.clear();
    To copy to clipboard, switch view to plain text mode 

    It will clear also verticalLayout?

    thanks in advance
    best regards
    Tomasz

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  12. #10
    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

  13. #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.

    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

  14. #12
    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

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

    Tomasz (29th November 2010)

  16. #13
    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

  17. #14
    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

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

    Tomasz (29th November 2010)

  19. #15
    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

  20. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


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

    Tomasz (29th November 2010)

  22. #17
    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

  23. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


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

    Tomasz (29th November 2010)

  25. #19
    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

  26. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.