Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to dynamically add QPushButton in QStackedWidget?



  • QStackedWidget is already complete by QT creator.
    but I want add QPushButton in QStackedWidget as coding(C++).
    What I want to do is add a new button to the bottom of the current screen
    when I press a button that already exists in the QStackedWidget.

    Below are some of the sources.

    =====================================================

    QPushButton* nextButton = new QPushButton();
    nextButton->setText(QString::fromLocal8Bit("next"));
    nextButton->setGeometry(QRect(QPoint(1150, 620), QSize(93, 28)));
    connect(nextButton, SIGNAL(clicked()), this, SLOT(readCustomerGraph()));
    ui.StackedWidget->addWidget(nextButton);
    nextButton->show();

    =====================================================

    When I do this, the button is visible but cannot be clicked.

    I hope for help.
    thank you.


  • Lifetime Qt Champion

    Hi and welcome to the forums.

    if possible you should use the new connect syntax as that tells if a connection cannot be made at compile time.
    https://wiki.qt.io/New_Signal_Slot_Syntax

    in any case,
    the addwidget you are using is for adding new pages!
    Is that what you want ? Its very odd to use a button as a page.

    if you mean to insert into the existing pages,
    then you can access that widget via
    ui->page
    or ui->page_2
    etc
    You can see names in the Designer.

    But often you want such buttons outside of the page and under the stackedwidget as you want them always there.


Log in to reply