Results 1 to 3 of 3

Thread: Managing dynamically created buttons

  1. #1
    Join Date
    Feb 2017
    Posts
    8
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Managing dynamically created buttons

    Hello,
    I created some labels and buttons dynamically. I want to know how can I handle each button when it's clicked.
    I know in a normal way for one button it goes like this:
    Qt Code:
    1. void test::on_btnName_clicked()
    2. {
    3. // Some code here ....
    4. }
    To copy to clipboard, switch view to plain text mode 
    So what abt this case ?
    I have no idea so here's what I've done so far
    test.cpp
    Qt Code:
    1. #include "test.h"
    2. #include "ui_test.h"
    3.  
    4. test::test(QWidget *parent) : QMainWindow(parent), ui(new Ui::test)
    5. {
    6. ui->setupUi(this);
    7. for (int i = 1; i <= 4; i++){
    8.  
    9. for (int j = 1; j <= 4; j++){
    10. QString btnName = "Button "+QString::number(i)+","+QString::number(j);
    11. QString lblName = "Label "+QString::number(i)+","+QString::number(j);
    12. QPushButton* panelButton = new QPushButton(btnName);
    13. QLabel* label = new QLabel(lblName);
    14. panelButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    15. label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    16. ui->PanelButtonGridLayout->addWidget(panelButton,i,4);
    17. if(j<4) {
    18. ui->PanelButtonGridLayout->addWidget(label,i,j);
    19. }
    20. }
    21. }
    22. }
    23.  
    24. test::~test()
    25. {
    26. delete ui;
    27. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2011
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Managing dynamically created buttons

    After you create button, you could create connections between button's clikced and slot which you want to do.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Managing dynamically created buttons


Similar Threads

  1. dynamically created tablewidget name problem
    By nlgootee in forum Qt Programming
    Replies: 9
    Last Post: 21st July 2016, 18:28
  2. Dynamically created buttons to one slot
    By rdkk in forum Qt Programming
    Replies: 11
    Last Post: 6th June 2013, 00:33
  3. use value from dynamically created widget in a slot
    By Cyrebo in forum Qt Programming
    Replies: 55
    Last Post: 23rd April 2013, 15:51
  4. Replies: 12
    Last Post: 24th October 2011, 08:56
  5. Dynamically created buttons.
    By Tomasz in forum Newbie
    Replies: 26
    Last Post: 2nd December 2010, 10:40

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.