0

i have created a button"publish button" which create a new widget with some buttons on it each time i clicked on this button now i wanna to put action listener to the dynamic button which created in each new wedget this code of how my publish button work on click it :

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
          QObject::connect(
          ui->publishbuttontimeline_2, &QPushButton::clicked,
          this, &MainWindow::onAddWidget);
}

this is my constructor which connect the publish button with the method which create new widget and its buttons

this is the code of the method onAddWidget :

void MainWindow::onAddWidget()
{
    //define BoxLayout and each post block
    QVBoxLayout*Layout=qobject_cast<QVBoxLayout*>(ui->scrollareapoststimeline->layout());
    QWidget*postblock=new QWidget();
    QBoxLayout*boxlayout=new QBoxLayout(QBoxLayout::TopToBottom,postblock);

    QPushButton *commentbutton=new QPushButton("comment");

     boxlayout->insertWidget(0,commentbutton);
;
     postblock->setLayout(boxlayout);

    //push the boxlayout which have the post components to the QVBox of the scrollview
    Layout->insertWidget(0,postblock);
}

now i wanna but an action to the "comment button" to open new widget when click on it how can i do that with qt or if i wanna to make another button to remove all the "postblock" what is the code which can i use in the slot

  • Use connect(commentbutton, &QPushButton::released, this, &MainWindow::commentbuttonActionListener); source: https://wiki.qt.io/How_to_Use_QPushButton – user3814613 Jun 03 '21 at 21:01
  • @user3814613 thank you so much, may you help me if i want to delete "postblock" when press the comment button, what is the code should i write in the slot – mohamed nour Jun 03 '21 at 21:33
  • Try Layout()->removeAt(postblock); delete postblock; as per https://stackoverflow.com/a/17719387 – user3814613 Jun 04 '21 at 22:16
  • @user3814613 if i have loop with this method and i have more than one button how to distinguish between them please reply me if you have an idea – mohamed nour Jun 06 '21 at 01:12

0 Answers0