Opening dynamically created buttons



  • In my qt c++ application I create buttons dynamically based on the contents of a QStringList(i.e number of buttons is equal to the number of elements in the QStringlist and the text of the buttons are the elements in the list).

    following is my code
    #include "dialog.h"
    #include "ui_dialog.h"
    #include "QFrame"
    #include "QLabel"
    #include "QPushButton"

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),

    ui(new Ui::Dialog)
    

    {
    ui->setupUi(this);

    }

    Dialog::~Dialog()
    {
    delete ui;
    }

    void Dialog::createButtons(){

    for(int i=0;i<List.size();i++){
        f1 = new QFrame();
    
        a= new QPushButton();
    
        a->setText(List[i]);
    
    
        ui->horizontalLayout->addWidget(a);
    }
    

    }

    void Dialog::on_pushButton_clicked()
    {
    createButtons()

    }

    Here "List"is the respective QStringList that I used!

    when I call the createButtons() method in a button click as shown in my code the buttons are dynamically created!

    When I select each dynamically created button and click that button I want to display an interface( let us assume it to be a blank interface for the moment). How can I achieve it?



  • You need to connect each Button to a Slot, as Example. The Slot opens a new Window, if you mean that with Interface.

    connect(button, SIGNAL(clicked()), this, SLOT(mySlot()));
    

    Is that what you mean?



  • @Fuel I want a way to distinguish the buttons that I created dynamically from one another! Since they are created dynamically I cant right click the buttons and select goto slot option like normal ordinary buttons that we add statically


  • Qt Champions 2017

    @Kushan
    The connect statement @Fuel shows is how you would
    make the same as right clicking in Designer.
    ( sort of, as Designer makes a special name and its found at runtime and hooked up)

    So you hook up each button to different slots and hence different things will happen when you click it.

    If you have many , it might be awkward creating that many slots.

    You can also use
    sender() inside a slot to know whom sent it. But the answer is just some Buttons so
    in what way do you need to distinguish the buttons ?



  • @mrjj Thanx where should I place

    connect(button, SIGNAL(clicked()), this, SLOT(mySlot()));

    this code?


  • Qt Champions 2017

    @Kushan

    Just after
    a= new QPushButton();
    connect(a, SIGNAL(clicked()), this, SLOT(mySlot()));

    Note that you should create a slot in Dialog .h
    called
    void mySlot();
    and in Dialog.cpp

    void mySlot() {
    }

    Just like when you rightclick,
    Designer adds such function to the class.h and in .cpp



  • @mrjj Thanx! But the problem I face is if 2 buttons that are dynamically created and I want one to perform a specific function when it is clicked and the other button to perform another action when its clicked! how can I achieve it? Here "a"is cmmon to both in such a scenario


  • Qt Champions 2017

    @Kushan
    Hi
    you could simply make another slot
    and connect to that instead.
    connect(a, SIGNAL(clicked()), this, SLOT(mySlot2()));

    But how do you know what the button you are creating should do ?
    its it part of the data you read in ?



  • @mrjj The buttons are displaying the names of elements in the qstringlist! Each element has a method name! so when I click a button a method resembling that method name shouldget executed!

    eg-List<<"Run"<< "Stop"

    so 2 buttons are created displaying Run and Stop! so when I click the button displaying with the word "Run" the Run() method should execute and when I click the button displaying the word "Stop" the Stop() method should execute.


  • Moderators



  • @jsulm thanx bro :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.