Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So I managed to fix the problem with platform depended code. I created Focuser class with code in the .mm file and that contained Objective-C code that called Cocoa.</p> <p>focuser.h</p> <pre><code>#ifndef FOCUSER_H #define FOCUSER_H #include &lt;QWidget&gt; class Focuser { QWidget *widget; public: Focuser(QWidget *); void show(); void focus(); }; #endif // FOCUSER_H </code></pre> <p>focuser_mac.mm</p> <pre><code>#include "focuser.h" #import &lt;Cocoa/Cocoa.h&gt; Focuser::Focuser(QWidget *w) { this-&gt;widget = w; } void Focuser::show() { this-&gt;widget-&gt;show(); this-&gt;focus(); } void Focuser::focus() { [NSApp activateIgnoringOtherApps:YES]; this-&gt;widget-&gt;activateWindow(); this-&gt;widget-&gt;raise(); } </code></pre> <p>focuser.cpp</p> <pre><code>#include "focuser.h" Focuser::Focuser(QWidget *w) { this-&gt;widget = w; } void Focuser::show() { this-&gt;widget-&gt;show(); this-&gt;focus(); } void Focuser::focus() { this-&gt;widget-&gt;activateWindow(); this-&gt;widget-&gt;raise(); } </code></pre> <p>So we've got one class that takes QWidget in the constructor and has two public methods, one show that shows the widget and focus that focuses the widget. Then we have two definitions of the class, one for Mac OS X in <strong>focuser_mac.mm</strong> and one in <strong>focuser.cpp</strong> for any other operating system. The one for the mac additionally calls</p> <pre><code>[NSApp activateIgnoringOtherApps:YES]; </code></pre> <p>Now, in order for it to compile as it should add this to your <strong>.pro</strong> file:</p> <pre><code>HEADERS += focuser.h mac { OBJECTIVE_SOURCES += focuser_mac.mm } linux|win32 { SOURCES += focuser.cpp } </code></pre> <p>When we're done, just add this code where you need your application to be focused:</p> <pre><code>QWidget *w = new QWidget(); // ... Focuser f(w); w.show(); // The widget will now be shown and focused by default. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload