How To Get The Top win

Description

my application have ,many layer win when i click the mouse‘s ’ right button i want to get the top
win and close it

What MCU/Processor/Board and compiler are you using?

HiSilicon ARM

What do you want to achieve?

i want to get the top
win and close it

What have you tried so far?

i try to use lv_obj_get_child(lv_disp_get_scr_act(NULL),NULL); to get the top win but its failed

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

/*You code here*/

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

When I draw a new screen I call the following to destroy the old screen.

lv_obj_clean(lv_scr_act());

This recursively cleans up all child objects starting at the active screen. See here.

in this way ,it can return to the homepage?

It should work. Can you send a short code example to reproduce the issue?

this my right key code,when call lv_win_get_title will cause segment fault

            else if(in.code == BTN_RIGHT)
            {
                if (in.value == 0)
                {
                    lv_indev_get_point(lv_indev_get_act(),&point);                    
                    printf("right key release:x:%#x,y:%#x,CHILD:%d\n",point.x,point.y,lv_obj_count_children(lv_disp_get_scr_act(NULL)));
                    
                }
                else if (in.value == 1)
                {    
                    #if 1
                    lv_obj_t * topwin;                   
                    lv_obj_t * topwinp;
                    topwin = lv_obj_get_child(lv_disp_get_scr_act(NULL),NULL);
                    if(NULL != topwin)
                    {
                        char *ptopwin_title = lv_win_get_title(topwinp);
                        if(NULL !=ptopwin_title)
                        {
                            printf("Get The topwin_title is:%s\n",ptopwin_title);
                            //lv_win_clean(topwin);
                        }
                    }
                    #endif /* 0 */
                    lv_indev_get_point(lv_indev_get_act(),&point); 
                    right_key_cb(&point);
                    printf("right key press:x:%#x,y:%#x,\n",point.x,point.y);
                }
            }

This has a flaw. You have set topwin to the address of the active screen, but then you are getting the title of the window pointed to by topwinp which was never initialized. You are trying to access a pointer which has an undefined value, thus the segfault.

@TangoZhu
Next time, please use the following tags to format your code:

```c
your 
code
comes 
here 
```

and it’s there a way that i can get the top win?

The code you have listed gets the top window, but you have two different pointers and you are using the one that is uninitialized.

Change this:

lv_obj_t * topwin;
lv_obj_t * topwinp;
topwin = lv_obj_get_child(lv_disp_get_scr_act(NULL),NULL);
if(NULL != topwin)
{
    char *ptopwin_title = lv_win_get_title(topwinp);

To this:

lv_obj_t * topwin;
topwin = lv_obj_get_child(lv_disp_get_scr_act(NULL),NULL);
if(NULL != topwin)
{
    char *ptopwin_title = lv_win_get_title(topwin);

sorry its my fault but its still not work,as soon as i call the falling code its segmentfault,
i add these code at my mouse right click

                    static lv_obj_t * topwin;                   
                    topwin = lv_obj_get_child(lv_disp_get_scr_act(NULL),NULL);
                    #if 1
                    if(NULL != topwin)
                    {
                        char *ptopwin_title = lv_win_get_title(topwin);
                        if(NULL !=ptopwin_title)
                        {
                            printf("Get The topwin_title is\n");
                            //lv_win_clean(topwin);
                        }
                        else
                        {
                            printf("Get The topwin_title is NULL\n");
                        }
                    }
                    #endif /* 0 */

What line does it crash on? There is not a lot of information for us to work with.

char *ptopwin_title = lv_win_get_title(topwin);

It’s likely that topwin is not actually pointing to a window object like you think. Can you try printing the value of buf.type[0] after a call to lv_obj_get_type(topwin)?

Is there a reason why you retrieve the top window through lv_obj_get_child(lv_disp_get_scr_act(NULL),NULL), rather than just updating the value of topwin when it is created? What you are doing here seems a bit awkward.

1 Like

the following code solve my problem Thank you @tarvik

                  lv_obj_t * topwin; 
                    lv_obj_type_t objbuf;
                    topwin = lv_obj_get_child(lv_disp_get_scr_act(NULL),NULL);
                    lv_obj_get_type(topwin, &objbuf);                    
                    if(strncmp(*objbuf.type,"lv_win",strlen("lv_win")) == 0)
                    {

                        char *ptopwin_title = lv_win_get_title(topwin);
                        if(NULL !=ptopwin_title)
                        {
                            printf("Get The topwin_title is:%s\n",ptopwin_title);
                            lv_obj_del(topwin);
                        }

                    }                    
                    else
                    {
                        lv_indev_get_point(lv_indev_get_act(),&point); 
                        right_key_cb(&point);
                        printf("right key press:x:%#x,y:%#x,\n",point.x,point.y);
                    }
```c