]> vaikene.ee Git - evaf/commitdiff
Updated tutorial to reflect latest iSdiWindow interface changes.
authorEnar Väikene <enar@vaikene.net>
Wed, 28 Sep 2011 06:23:48 +0000 (09:23 +0300)
committerEnar Väikene <enar@vaikene.net>
Wed, 28 Sep 2011 06:23:48 +0000 (09:23 +0300)
www/pswgen10.html

index 616e3216ca62c501fd6049c157ea2300690ba3d0..bef0c7b1c9a3e7f610c72ca1520aed17446e89e4 100644 (file)
@@ -115,13 +115,17 @@ Module<span class="hl opt">::~</span><span class="hl kwd">Module</span><span cla
             <li>Queries and stores interfaces implemented in other modules. The <tt>iGenerator</tt> interface must be implemented;
             the <tt>iStorage</tt> interface is optional and we can work without it.</li>
             <li>Gets the main window interface <tt>iSdiWindow</tt>.</li>
-            <li>Creates the layout for the main window widget and adds our widgets to the layout.</li>
+            <li>Adds our widgets to the main window layout.</li>
             <li>Connects signals on widgets to slots in this class.</li>
             <li>Uses actions <tt>QAction</tt> to add keyboard shortcuts -- <tt>Enter</tt> key generates a password and <tt>Esc</tt> key
             quits the application.</li>
             <li>Sets the <tt>mReady</tt> flag to true when all this is done.</li>
         </ol>
 
+        <p>The main window has an empty <tt>QVBoxLayout</tt> layout and we could add our widgets directly to this layout, but for keyboard
+        shortcuts to work properly, we need a widget that fills the main window. We create a master widget, add it to the main window
+        layout and then fill the master widget with other widgets.</p>
+
         <pre class="hl"><span class="hl kwb">bool</span> Module<span class="hl opt">::</span><span class="hl kwd">init</span><span class="hl opt">(</span>QString <span class="hl kwb">const</span> <span class="hl opt">&amp;</span> args<span class="hl opt">)</span>
 <span class="hl opt">{</span>
     <span class="hl kwd">Q_UNUSED</span><span class="hl opt">(</span>args<span class="hl opt">);</span>
@@ -134,12 +138,15 @@ Module<span class="hl opt">::~</span><span class="hl kwd">Module</span><span cla
     <span class="hl kwa">if</span> <span class="hl opt">(!</span>mStorage<span class="hl opt">)</span>
         <span class="hl kwd">EVAF_WARNING</span><span class="hl opt">(</span><span class="hl str">&quot;No iStorage interface&quot;</span><span class="hl opt">);</span>
 
-    <span class="hl slc">// Get the main window interface and fill it with widgets</span>
+    <span class="hl slc">// Get the main window interface and fill it with the widgets</span>
     SdiWindow<span class="hl opt">::</span>iSdiWindow <span class="hl opt">*</span> win <span class="hl opt">=</span> evafQueryInterface<span class="hl opt">&lt;</span>SdiWindow<span class="hl opt">::</span>iSdiWindow<span class="hl opt">&gt;(</span><span class="hl str">&quot;iSdiWindow&quot;</span><span class="hl opt">);</span>
     <span class="hl kwd">EVAF_TEST_X</span><span class="hl opt">(</span>win<span class="hl opt">,</span> <span class="hl str">&quot;No iSdiWindow interface&quot;</span><span class="hl opt">);</span>
 
+    QWidget <span class="hl opt">*</span> masterWidget <span class="hl opt">=</span> <span class="hl kwa">new</span> QWidget<span class="hl opt">;</span>
+    win<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
+
     QVBoxLayout <span class="hl opt">*</span> v <span class="hl opt">=</span> <span class="hl kwa">new</span> QVBoxLayout<span class="hl opt">;</span>
-    win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">()-&gt;</span><span class="hl kwd">setLayout</span><span class="hl opt">(</span>v<span class="hl opt">);</span>
+    masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">setLayout</span><span class="hl opt">(</span>v<span class="hl opt">);</span>
 
     QGridLayout <span class="hl opt">*</span> g <span class="hl opt">=</span> <span class="hl kwa">new</span> QGridLayout<span class="hl opt">;</span>
     v<span class="hl opt">-&gt;</span><span class="hl kwd">addLayout</span><span class="hl opt">(</span>g<span class="hl opt">);</span>
@@ -169,7 +176,7 @@ Module<span class="hl opt">::~</span><span class="hl kwd">Module</span><span cla
     <span class="hl opt">}</span>
     <span class="hl kwd">connect</span><span class="hl opt">(</span>wName<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)));</span>
     g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wName<span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">);</span>
-    win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">()-&gt;</span><span class="hl kwd">setFocusProxy</span><span class="hl opt">(</span>wName<span class="hl opt">);</span>
+    masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">setFocusProxy</span><span class="hl opt">(</span>wName<span class="hl opt">);</span>
 
     l <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QLabel</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;&amp;Length of the password:&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
     l<span class="hl opt">-&gt;</span><span class="hl kwd">setAlignment</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>AlignRight<span class="hl opt">);</span>
@@ -207,15 +214,15 @@ Module<span class="hl opt">::~</span><span class="hl kwd">Module</span><span cla
     <span class="hl kwd">connect</span><span class="hl opt">(</span>wCopy<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">clicked</span><span class="hl opt">()),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">copyClicked</span><span class="hl opt">()));</span>
     h<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wCopy<span class="hl opt">);</span>
 
-    QAction <span class="hl opt">*</span> a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">());</span>
+    QAction <span class="hl opt">*</span> a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
     a<span class="hl opt">-&gt;</span><span class="hl kwd">setShortcut</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>Key_Return<span class="hl opt">);</span>
     <span class="hl kwd">connect</span><span class="hl opt">(</span>a<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">triggered</span><span class="hl opt">()),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">generateClicked</span><span class="hl opt">()));</span>
-    win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">()-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
+    masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
 
-    a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">());</span>
+    a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
     a<span class="hl opt">-&gt;</span><span class="hl kwd">setShortcut</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>Key_Escape<span class="hl opt">);</span>
     <span class="hl kwd">connect</span><span class="hl opt">(</span>a<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">triggered</span><span class="hl opt">()),</span> qApp<span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">quit</span><span class="hl opt">()));</span>
-    win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">()-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
+    masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
 
     mReady <span class="hl opt">=</span> <span class="hl kwa">true</span><span class="hl opt">;</span>
 
@@ -342,8 +349,11 @@ Module<span class="hl opt">::~</span><span class="hl kwd">Module</span><span cla
     SdiWindow<span class="hl opt">::</span>iSdiWindow <span class="hl opt">*</span> win <span class="hl opt">=</span> evafQueryInterface<span class="hl opt">&lt;</span>SdiWindow<span class="hl opt">::</span>iSdiWindow<span class="hl opt">&gt;(</span><span class="hl str">&quot;iSdiWindow&quot;</span><span class="hl opt">);</span>
     <span class="hl kwd">EVAF_TEST_X</span><span class="hl opt">(</span>win<span class="hl opt">,</span> <span class="hl str">&quot;No iSdiWindow interface&quot;</span><span class="hl opt">);</span>
 
+    QWidget <span class="hl opt">*</span> masterWidget <span class="hl opt">=</span> <span class="hl kwa">new</span> QWidget<span class="hl opt">;</span>
+    win<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
+
     QVBoxLayout <span class="hl opt">*</span> v <span class="hl opt">=</span> <span class="hl kwa">new</span> QVBoxLayout<span class="hl opt">;</span>
-    win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">()-&gt;</span><span class="hl kwd">setLayout</span><span class="hl opt">(</span>v<span class="hl opt">);</span>
+    masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">setLayout</span><span class="hl opt">(</span>v<span class="hl opt">);</span>
 
     QGridLayout <span class="hl opt">*</span> g <span class="hl opt">=</span> <span class="hl kwa">new</span> QGridLayout<span class="hl opt">;</span>
     v<span class="hl opt">-&gt;</span><span class="hl kwd">addLayout</span><span class="hl opt">(</span>g<span class="hl opt">);</span>
@@ -373,7 +383,7 @@ Module<span class="hl opt">::~</span><span class="hl kwd">Module</span><span cla
     <span class="hl opt">}</span>
     <span class="hl kwd">connect</span><span class="hl opt">(</span>wName<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)));</span>
     g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wName<span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">);</span>
-    win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">()-&gt;</span><span class="hl kwd">setFocusProxy</span><span class="hl opt">(</span>wName<span class="hl opt">);</span>
+    masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">setFocusProxy</span><span class="hl opt">(</span>wName<span class="hl opt">);</span>
 
     l <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QLabel</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;&amp;Length of the password:&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
     l<span class="hl opt">-&gt;</span><span class="hl kwd">setAlignment</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>AlignRight<span class="hl opt">);</span>
@@ -411,15 +421,15 @@ Module<span class="hl opt">::~</span><span class="hl kwd">Module</span><span cla
     <span class="hl kwd">connect</span><span class="hl opt">(</span>wCopy<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">clicked</span><span class="hl opt">()),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">copyClicked</span><span class="hl opt">()));</span>
     h<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wCopy<span class="hl opt">);</span>
 
-    QAction <span class="hl opt">*</span> a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">());</span>
+    QAction <span class="hl opt">*</span> a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
     a<span class="hl opt">-&gt;</span><span class="hl kwd">setShortcut</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>Key_Return<span class="hl opt">);</span>
     <span class="hl kwd">connect</span><span class="hl opt">(</span>a<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">triggered</span><span class="hl opt">()),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">generateClicked</span><span class="hl opt">()));</span>
-    win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">()-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
+    masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
 
-    a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">());</span>
+    a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
     a<span class="hl opt">-&gt;</span><span class="hl kwd">setShortcut</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>Key_Escape<span class="hl opt">);</span>
     <span class="hl kwd">connect</span><span class="hl opt">(</span>a<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">triggered</span><span class="hl opt">()),</span> qApp<span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">quit</span><span class="hl opt">()));</span>
-    win<span class="hl opt">-&gt;</span><span class="hl kwd">widget</span><span class="hl opt">()-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
+    masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
 
     mReady <span class="hl opt">=</span> <span class="hl kwa">true</span><span class="hl opt">;</span>