Give focus back to the edit window sketcher.

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
paddle
Veteran
Posts: 1413
Joined: Mon Feb 03, 2020 4:47 pm

Give focus back to the edit window sketcher.

Post by paddle »

So working on my tool setting widget, I have a focus issue. When the tool is not active, I want to give back focus to the main view (and not keep the focus on the Task view).
To do so I used in my widget :

Code: Select all

    Gui::MDIView* mdi = Gui::Application::Instance->activeDocument()->getActiveView();
    mdi->setFocus();
Which seems to work. But I have a bug now that seems related and I wonder if this was correct.

Especially since when I'm in edit mode, no tool open (so the widget gives the focus this way) then pressing Q crash gives an illegal storage access error.
So I guess it's giving the focus to the wrong thing where it tries to launch something linked to Q.
Edit: other keys that are not launching a tool in sketcher all gives that errors.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Give focus back to the edit window sketcher.

Post by openBrain »

I would rather run something like

Code: Select all

Gui::MainWindow::getInstance()->centralWidget()->setFocus()
User avatar
paddle
Veteran
Posts: 1413
Joined: Mon Feb 03, 2020 4:47 pm

Re: Give focus back to the edit window sketcher.

Post by paddle »

openBrain wrote: Thu Jan 27, 2022 1:22 pm I would rather run something like

Code: Select all

Gui::MainWindow::getInstance()->centralWidget()->setFocus()
Thanks for the reply !
What includes does that requires?
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Give focus back to the edit window sketcher.

Post by openBrain »

I'd bet for <Gui/MainWindow.h> ;)
User avatar
paddle
Veteran
Posts: 1413
Joined: Mon Feb 03, 2020 4:47 pm

Re: Give focus back to the edit window sketcher.

Post by paddle »

openBrain wrote: Thu Jan 27, 2022 1:36 pm I'd bet for <Gui/MainWindow.h> ;)
Thanks!
Though it doesn't work. ViewProviderSketch doesn't get the keypress anymore.

I also tried

Code: Select all

    Gui::MDIView* mdi = Gui::getMainWindow()->activeWindow();
    mdi->setFocus();
But it's the same as I did before.

I could get more detail on my error. After this setfocus, the SPACE key PRESS event is not received by ViewProviderSketch presskey (RELEASE is still received).

From my understanding this event is caught somewhere to toggle on:off the sketch visibility (combo view/model). So my guess is that the focus is set on something to wide where an eventfilter catches the SPACE PRESS. So focus is not precisely on ViewProviderSketch.

Any idea how to do focus the ViewProviderSketch?
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Give focus back to the edit window sketcher.

Post by openBrain »

Maybe something more specific :

Code: Select all

 Gui::MainWindow::getInstance()->centralWidget()->activeSubwindow()->widget()->setFocus()
Just for test, 'activeSubWindow' return value should be checked I guess. ;)
User avatar
paddle
Veteran
Posts: 1413
Joined: Mon Feb 03, 2020 4:47 pm

Re: Give focus back to the edit window sketcher.

Post by paddle »

openBrain wrote: Thu Jan 27, 2022 2:40 pm Maybe something more specific :

Code: Select all

 Gui::MainWindow::getInstance()->centralWidget()->activeSubWindow()->widget()->setFocus()
Just for test, 'activeSubWindow' return value should be checked I guess. ;)
Unfortunately VS gives me an error :
"class QWidget has no member "activeSubWindow"

Note: with Gui::MainWindow::getInstance()->centralWidget()->setFocus()
The SPACE key RELEASE event is also lost. Even ESCAPE is lost.

Context/reminder : I'm trying to do this from a widget in the Tasks tab of the combo view.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Give focus back to the edit window sketcher.

Post by openBrain »

paddle wrote: Thu Jan 27, 2022 2:52 pm Unfortunately VS gives me an error :
"class QWidget has no member "activeSubWindow"
My mistake. Most probably in C++ '->centralWidget()' return needs to 'qobject_cast' into a QMdiArea. ;)
User avatar
paddle
Veteran
Posts: 1413
Joined: Mon Feb 03, 2020 4:47 pm

Re: Give focus back to the edit window sketcher.

Post by paddle »

openBrain wrote: Thu Jan 27, 2022 3:12 pm
paddle wrote: Thu Jan 27, 2022 2:52 pm Unfortunately VS gives me an error :
"class QWidget has no member "activeSubWindow"
My mistake. Most probably in C++ '->centralWidget()' return needs to 'qobject_cast' into a QMdiArea. ;)
Not sure what this means.

But maybe I'm not asking the correct question actually.
What I need is to give the keyboard events back to ViewProviderSketch. I'm not sure that what I need is to give focus to the main window. What do you think?
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Give focus back to the edit window sketcher.

Post by openBrain »

paddle wrote: Thu Jan 27, 2022 3:18 pm Not sure what this means.
Something like :

Code: Select all

QMdiArea *mdi = qobject_cast<QMdiArea *>(Gui::MainWindow::getInstance()->centralWidget());
if (!mdi) return;
mdi->activeSubWindow()->widget()->setFocus();
But maybe I'm not asking the correct question actually.
What I need is to give the keyboard events back to ViewProviderSketch. I'm not sure that what I need is to give focus to the main window. What do you think?
This is purpose of setFocus(). I tested above method in Python (no need to cast :D) and works.
Post Reply