简体   繁体   中英

How to use connect in Qt to know when the “X” (exit) button is clicked

I'm struggling in figuring out how to use the connect function to recognize when the "x" button at the top left corner has been clicked. The red "x" button that cancels the window.

在此处输入图片说明

I know that I can override the closeEvent to do whatever I want but i need to use the connect function. Let me give you a background of the problem.

I have a function that I am calling from main like this:

ManageEmployeeWindow mng;
int choice = mng.checkChoice();

checkChoice is used to create the window and return an int depending on what button on that window that the user decides to click.

ex- If the user clicks button 1 then the checkChoice will return 1.

Now knowing that I would like to return a number through checkChoice using the connect function to recognize when the button is clicked, how can i do this when the "x" button is clicked?. Is there anyway of doing that?

Btw here is checkChoice just so you can understand what I mean with the connect.

int ManageEmployeeWindow::checkChoice(){

this->exec();
cout << "Inside of the checkChoice" << endl;
QEventLoop waitForResponse;
connect(ui->ManualEmployeeUpdate, SIGNAL (clicked()), &waitForResponse, SLOT(quit()));

connect(ui->DataIntegrationTool, SIGNAL (clicked()), &waitForResponse, SLOT(quit()));

connect(x,SIGNAL(clicked()),this,SLOT(ButtonPressed()));

waitForResponse.exec();

return choice;

}

Please let me hear your thoughts on this.

Thank you and my apoligies if I handed out too much detail

If you want to create a signal when you press the X button you can create the signal and emit it in the closeEvent method.

protected:
    void closeEvent(QCloseEvent * event){
        emit clicked();
        event->ignore();
    }

signals:
    void clicked();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM