Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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");
        pLineEditPortNumber->setInputMask("99999");
        QPushButton *pPushButtonStart = new QPushButton("Start");
    
    
        QVBoxLayout *pVBoxLayoutMain = new QVBoxLayout;
        pVBoxLayoutMain->addWidget(pLineEditPortNumber);
        pVBoxLayoutMain->addWidget(pPushButtonStart);
        widget.setLayout(pVBoxLayoutMain);
    
        MyServer Server;
        Server.StartServer();
    
        widget.show();
        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

    @asttekin

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


  • thank you so much.


Log in to reply