Ask Your Question
2

How to detect closing of namedWindow by mouse or ALT+F4

asked 2016-10-27 04:06:43 -0600

I have this simple code for showing an image from the command line:

int main(int argc, char* argv[]) {
    int retStatus = -1;

    if(argc < 2) {
        std::cout << "Please provide a path to on image!" << std::endl;
    } else {
        std::string path = argv[1];
        std::ifstream file(path);
        if(!file) {
            std::cout << "It seems that the file " << path <<
                                           " Does not exist!" << std::endl;
        } else { //file exist :)
            cv::namedWindow(path); //autosize is default so no need to provide it
            cv::Mat src = cv::imread(path); //read the file
            if(!src.data) { //check for validity of the file
                std::cout << "File is corrupted!" << std::endl;
            } else {
                cv::imshow(path, src);
                while(cv::waitKey(1) != 27); //if ESC is not pressed
                src.release();
                retStatus = 0;
            }
        }
    }
    return retStatus;
}

It works, but only if I press the ESC key it terminates the program. If I hit the X on the window by mouse or press ALT+F4 the program does not terminate (but the window will be closed!). So how to detect this closing behavior?

edit retag flag offensive close merge delete

Comments

"If I hit the X on the window by mouse or press ALT+F4 the program does not terminate "

this is unfortunately exactly the intended behaviour here.

berak gravatar imageberak ( 2016-10-27 04:20:52 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2016-10-27 06:05:23 -0600

gfx gravatar image
        } else {
            cv::imshow(path, src);
            while(cv::waitKey(1) != 27); //if ESC is not pressed /* if esc is pressed cv window GUI become close */
            src.release(); /* if src is released  the cv window become close */
            retStatus = 0; /* if esc = > retstatus 0 */
        }

You need to show the image in other type of window GUI ... like openGL or Qt, GTK, and use relative command to close the image without closing the program .... If use cv::imshow opencv open cv-window and is not possible to close it without close the main loop ... you need at last 2 loop ... If cv-loop is cloose the secdone (not cv but std c++ for example, or Qt or other ..) survive!! The best approach is open the cv image (with other cv operation) in a thread and show the result in GUI thread of some other tipe of gui support ... In these way You have 2 loop ...

In your case You have only "main" thread .... and if:

        if(!src.data) { //check for validity of the file /* if is false */
            std::cout << "File is corrupted!" << std::endl;
        } else { /* is false in case of esc  => go out of main thread */
            cv::imshow(path, src);
            while(cv::waitKey(1) != 27); //if ESC is not pressed
            src.release();
            retStatus = 0;
        }

I hope I explained myself, my English is faltering ...

Possibility of grabbing "X" on windows depends of your SO ... for example in linux ubuntu, debian (std install) It is possible but not so easy because WM He prefers to have priority control... In windows SO maybe more easy ...

regards giorgio

edit flag offensive delete link more
1

answered 2019-05-08 18:58:57 -0600

Dave92F1 gravatar image

In Python, poll with cv2.getWindowImageRect(windowName). It will return (-1, -1, -1, -1) when the user clicks the window close button.

# check if window was closed or image was resized

xPos, yPos, width, height = cv2.getWindowImageRect(windowName)

if xPos == -1: # if user closed window
    pass # do whatever you want here if the user clicked CLOSE
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-10-27 04:06:43 -0600

Seen: 8,933 times

Last updated: May 08 '19