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












0















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.



enter image description here



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










share|improve this question

























  • In the meantime, if anyone responds within the next half hour please keep in mind that i will be on the road. I will respond as soon as I can. which should be in half an hour

    – ismail yussuf
    Apr 4 '17 at 1:47






  • 1





    Could you explain yourself better?

    – eyllanesc
    Apr 4 '17 at 3:47











  • What you want is a signal that tells you when the x button is pressed ?, if you press that button closes the window or not?

    – eyllanesc
    Apr 4 '17 at 3:49











  • What if they use a keyboard shortcut instead of clicking the x?

    – MrEricSir
    Apr 4 '17 at 5:12
















0















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.



enter image description here



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










share|improve this question

























  • In the meantime, if anyone responds within the next half hour please keep in mind that i will be on the road. I will respond as soon as I can. which should be in half an hour

    – ismail yussuf
    Apr 4 '17 at 1:47






  • 1





    Could you explain yourself better?

    – eyllanesc
    Apr 4 '17 at 3:47











  • What you want is a signal that tells you when the x button is pressed ?, if you press that button closes the window or not?

    – eyllanesc
    Apr 4 '17 at 3:49











  • What if they use a keyboard shortcut instead of clicking the x?

    – MrEricSir
    Apr 4 '17 at 5:12














0












0








0








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.



enter image description here



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










share|improve this question
















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.



enter image description here



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







c++ qt button qt5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 3:12









eyllanesc

84.5k103562




84.5k103562










asked Apr 4 '17 at 1:44









ismail yussufismail yussuf

277




277













  • In the meantime, if anyone responds within the next half hour please keep in mind that i will be on the road. I will respond as soon as I can. which should be in half an hour

    – ismail yussuf
    Apr 4 '17 at 1:47






  • 1





    Could you explain yourself better?

    – eyllanesc
    Apr 4 '17 at 3:47











  • What you want is a signal that tells you when the x button is pressed ?, if you press that button closes the window or not?

    – eyllanesc
    Apr 4 '17 at 3:49











  • What if they use a keyboard shortcut instead of clicking the x?

    – MrEricSir
    Apr 4 '17 at 5:12



















  • In the meantime, if anyone responds within the next half hour please keep in mind that i will be on the road. I will respond as soon as I can. which should be in half an hour

    – ismail yussuf
    Apr 4 '17 at 1:47






  • 1





    Could you explain yourself better?

    – eyllanesc
    Apr 4 '17 at 3:47











  • What you want is a signal that tells you when the x button is pressed ?, if you press that button closes the window or not?

    – eyllanesc
    Apr 4 '17 at 3:49











  • What if they use a keyboard shortcut instead of clicking the x?

    – MrEricSir
    Apr 4 '17 at 5:12

















In the meantime, if anyone responds within the next half hour please keep in mind that i will be on the road. I will respond as soon as I can. which should be in half an hour

– ismail yussuf
Apr 4 '17 at 1:47





In the meantime, if anyone responds within the next half hour please keep in mind that i will be on the road. I will respond as soon as I can. which should be in half an hour

– ismail yussuf
Apr 4 '17 at 1:47




1




1





Could you explain yourself better?

– eyllanesc
Apr 4 '17 at 3:47





Could you explain yourself better?

– eyllanesc
Apr 4 '17 at 3:47













What you want is a signal that tells you when the x button is pressed ?, if you press that button closes the window or not?

– eyllanesc
Apr 4 '17 at 3:49





What you want is a signal that tells you when the x button is pressed ?, if you press that button closes the window or not?

– eyllanesc
Apr 4 '17 at 3:49













What if they use a keyboard shortcut instead of clicking the x?

– MrEricSir
Apr 4 '17 at 5:12





What if they use a keyboard shortcut instead of clicking the x?

– MrEricSir
Apr 4 '17 at 5:12












1 Answer
1






active

oldest

votes


















0














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();





share|improve this answer
























  • Ok i understand what you're saying. But usually doesn't the connect button take in connect(button name, signal, slot). what would the button name be?

    – ismail yussuf
    Apr 4 '17 at 13:11











  • There is no name or signal associated with that button.

    – eyllanesc
    Apr 4 '17 at 13:14











  • Ok so could you please explain what would I pass in connect(). What would i pass in?

    – ismail yussuf
    Apr 4 '17 at 13:16











  • Sorry for the inconvenience, I'm still trying to get started in Qt

    – ismail yussuf
    Apr 4 '17 at 13:17











  • The connection is like any signal: connect({sender}, SIGNAL(cliked()), {receiver}, SLOT({slot receiver}));

    – eyllanesc
    Apr 4 '17 at 13:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43196983%2fhow-to-use-connect-in-qt-to-know-when-the-x-exit-button-is-clicked%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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();





share|improve this answer
























  • Ok i understand what you're saying. But usually doesn't the connect button take in connect(button name, signal, slot). what would the button name be?

    – ismail yussuf
    Apr 4 '17 at 13:11











  • There is no name or signal associated with that button.

    – eyllanesc
    Apr 4 '17 at 13:14











  • Ok so could you please explain what would I pass in connect(). What would i pass in?

    – ismail yussuf
    Apr 4 '17 at 13:16











  • Sorry for the inconvenience, I'm still trying to get started in Qt

    – ismail yussuf
    Apr 4 '17 at 13:17











  • The connection is like any signal: connect({sender}, SIGNAL(cliked()), {receiver}, SLOT({slot receiver}));

    – eyllanesc
    Apr 4 '17 at 13:19
















0














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();





share|improve this answer
























  • Ok i understand what you're saying. But usually doesn't the connect button take in connect(button name, signal, slot). what would the button name be?

    – ismail yussuf
    Apr 4 '17 at 13:11











  • There is no name or signal associated with that button.

    – eyllanesc
    Apr 4 '17 at 13:14











  • Ok so could you please explain what would I pass in connect(). What would i pass in?

    – ismail yussuf
    Apr 4 '17 at 13:16











  • Sorry for the inconvenience, I'm still trying to get started in Qt

    – ismail yussuf
    Apr 4 '17 at 13:17











  • The connection is like any signal: connect({sender}, SIGNAL(cliked()), {receiver}, SLOT({slot receiver}));

    – eyllanesc
    Apr 4 '17 at 13:19














0












0








0







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();





share|improve this answer













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();






share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 4 '17 at 3:55









eyllanesceyllanesc

84.5k103562




84.5k103562













  • Ok i understand what you're saying. But usually doesn't the connect button take in connect(button name, signal, slot). what would the button name be?

    – ismail yussuf
    Apr 4 '17 at 13:11











  • There is no name or signal associated with that button.

    – eyllanesc
    Apr 4 '17 at 13:14











  • Ok so could you please explain what would I pass in connect(). What would i pass in?

    – ismail yussuf
    Apr 4 '17 at 13:16











  • Sorry for the inconvenience, I'm still trying to get started in Qt

    – ismail yussuf
    Apr 4 '17 at 13:17











  • The connection is like any signal: connect({sender}, SIGNAL(cliked()), {receiver}, SLOT({slot receiver}));

    – eyllanesc
    Apr 4 '17 at 13:19



















  • Ok i understand what you're saying. But usually doesn't the connect button take in connect(button name, signal, slot). what would the button name be?

    – ismail yussuf
    Apr 4 '17 at 13:11











  • There is no name or signal associated with that button.

    – eyllanesc
    Apr 4 '17 at 13:14











  • Ok so could you please explain what would I pass in connect(). What would i pass in?

    – ismail yussuf
    Apr 4 '17 at 13:16











  • Sorry for the inconvenience, I'm still trying to get started in Qt

    – ismail yussuf
    Apr 4 '17 at 13:17











  • The connection is like any signal: connect({sender}, SIGNAL(cliked()), {receiver}, SLOT({slot receiver}));

    – eyllanesc
    Apr 4 '17 at 13:19

















Ok i understand what you're saying. But usually doesn't the connect button take in connect(button name, signal, slot). what would the button name be?

– ismail yussuf
Apr 4 '17 at 13:11





Ok i understand what you're saying. But usually doesn't the connect button take in connect(button name, signal, slot). what would the button name be?

– ismail yussuf
Apr 4 '17 at 13:11













There is no name or signal associated with that button.

– eyllanesc
Apr 4 '17 at 13:14





There is no name or signal associated with that button.

– eyllanesc
Apr 4 '17 at 13:14













Ok so could you please explain what would I pass in connect(). What would i pass in?

– ismail yussuf
Apr 4 '17 at 13:16





Ok so could you please explain what would I pass in connect(). What would i pass in?

– ismail yussuf
Apr 4 '17 at 13:16













Sorry for the inconvenience, I'm still trying to get started in Qt

– ismail yussuf
Apr 4 '17 at 13:17





Sorry for the inconvenience, I'm still trying to get started in Qt

– ismail yussuf
Apr 4 '17 at 13:17













The connection is like any signal: connect({sender}, SIGNAL(cliked()), {receiver}, SLOT({slot receiver}));

– eyllanesc
Apr 4 '17 at 13:19





The connection is like any signal: connect({sender}, SIGNAL(cliked()), {receiver}, SLOT({slot receiver}));

– eyllanesc
Apr 4 '17 at 13:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43196983%2fhow-to-use-connect-in-qt-to-know-when-the-x-exit-button-is-clicked%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen