Important: Please read the Qt Code of Conduct -

How to call pushbutton in main.cpp

  • I want to start the server when I press "Start" button.How to do it?

    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        QWidget widget;
        QLineEdit *pLineEditPortNumber = new QLineEdit;
        pLineEditPortNumber->setPlaceholderText("Enter Port Number");
        QPushButton *pPushButtonStart = new QPushButton("Start");
        QVBoxLayout *pVBoxLayoutMain = new QVBoxLayout;
        MyServer Server;
        return a.exec();

  • Moderators

    hi @asttekin

    Easy enough I think ;-)

    QObject::connect(pPushButtonStart, &QPushButton::clicked, &Server, &MyServer::StartServer);

    as main is not derived from QObject, you have to use the Static connect call.

  • Thanks.Well, how to pass the portNumber to StartServer.

  • Moderators


    QObject::connect(pLineEditPortNumber, &QLineEdit::textEdited, &Server, &MyServer::setPort); // assumption setPort exists and accepts an QString

  • thank you so much.

Log in to reply