Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > ba6e5e1a033bd8535c43a771ce407926 > files > 53

lib64cxx-gtk-utils2.2-devel-2.2.3-2.mga4.x86_64.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.4"/>
<title>c++-gtk-utils: Using c++-gtk-utils with Qt</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">c++-gtk-utils
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Using c++-gtk-utils with Qt </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>As mentioned under <a class="el" href="GTK.html">GTK versions</a>, this library can be compiled without GTK+, so that the only dependency is glib. This might be useful if this library is only intended to be used by programs employing some other toolkit (or not using a GUI at all).</p>
<p>From version 4.2, Qt by default uses glib to provide its main loop event dispatching. That means that if Qt has been compiled with this default option, the functions and objects in this library interfacing with the glib main loop (<a class="el" href="namespaceCgu_1_1Callback.html#ab8a459b25b1dba52fd43015c694bab1d">Cgu::Callback::post()</a>, <a class="el" href="classCgu_1_1Notifier.html" title="Provides thread-safe signalling between a worker thread and the main program thread. ">Cgu::Notifier</a>, <a class="el" href="classCgu_1_1Thread_1_1Future.html#aab2d9cd3193eee443e4f6ed1d3ae6c77">Cgu::Thread::Future::when()</a>, <a class="el" href="classCgu_1_1Thread_1_1TaskManager.html#a7a013fd7b21096fa76e39cbe5475d31b">Cgu::Thread::TaskManager::make_task_when()</a>, <a class="el" href="classCgu_1_1Thread_1_1TaskManager.html#abe374ca418bbe53e6c5228ecc1b6a3ac">Cgu::Thread::TaskManager::make_task_when_full()</a>, <a class="el" href="classCgu_1_1Thread_1_1TaskManager.html#a8ebf8f3fb5d9622d5369005c9dd7a14b">Cgu::Thread::TaskManager::make_task_compose()</a>, <a class="el" href="group__timeout.html#ga5ab9e341283a0a166f09f507cddf2817">Cgu::start_timeout()</a>, <a class="el" href="group__timeout.html#ga9a3ea4c3212a7688c9c3f767290a928b">Cgu::start_timeout_seconds()</a> and <a class="el" href="group__io__watch.html#gaa908dd5f3e3fd9e0d2cd9403884cad40">Cgu::start_iowatch()</a>) can be called in Qt programs so as to execute their callbacks in the Qt main loop.</p>
<p>Qt is a nice library but has a number of warts. One of those is its use of unprefixed lower-case macros, and in particular its macro definition of 'emit'. This will cause programs which directly or indirectly use this library's emitter classes to fail to compile.</p>
<p>To avoid this, the symbol QT_NO_EMIT can be defined before including any Qt header files in a source file that uses c++-gtk-utils (or it can be passed using the -D compiler flag). This has the effect of suppressing the 'emit' macro. Qt never requires the 'emit' macro to be used anyway when invoking a Qt signal (its macro defines to nothing), and it is there only for documentation purposes. If such documentation is seen as useful, the Q_EMIT macro can be used instead of emit.</p>
<p>Alternatively, the line:</p>
<div class="fragment"><div class="line">CONFIG += no_keywords</div>
</div><!-- fragment --><p>can be added to the program's project (.pro) file. However this causes more far-reaching macro suppressions (required by, for example, boost). If that option is taken, as well as not using 'emit' (or using Q_EMIT instead), programs must use the macros Q_SIGNALS (or Q_SIGNAL) and Q_SLOTS (or Q_SLOT) instead of the 'signals' and 'slots' macros.</p>
<p>Here is a compilable example which uses Qt and c++-gtk-utils to find the meaning of life, the universe and everything:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;ostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;string&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;time.h&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define QT_NO_EMIT</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#include &lt;QApplication&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;glib.h&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="task__manager_8h.html">c++-gtk-utils/task_manager.h</a>&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="callback_8h.html" title="This file provides classes for type erasure. ">c++-gtk-utils/callback.h</a>&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>Cgu;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]) {</div>
<div class="line"></div>
<div class="line">  g_thread_init(0); <span class="comment">// if glib &lt; 2.32 is used</span></div>
<div class="line">  QApplication app{argc, argv};</div>
<div class="line">  <a class="code" href="classCgu_1_1Thread_1_1TaskManager.html" title="A thread-pool class for managing tasks in multi-threaded programs. ">Thread::TaskManager</a> tm{2};</div>
<div class="line"></div>
<div class="line">  <span class="comment">// execute a callback in the Qt main loop</span></div>
<div class="line">  <a class="code" href="namespaceCgu_1_1Callback.html#ab8a459b25b1dba52fd43015c694bab1d">Callback::post</a>([]() {std::cout &lt;&lt; <span class="stringliteral">&quot;Finding the answer to the Ultimate Question of &quot;</span></div>
<div class="line">                                    <span class="stringliteral">&quot;Life, the Universe, and Everything\n&quot;</span>;});</div>
<div class="line"></div>
<div class="line">  <span class="comment">// execute a task and report the result in the Qt main loop</span></div>
<div class="line">  tm.make_task_when(</div>
<div class="line">    [&amp;app] (<span class="keyword">const</span> std::string&amp; res) {</div>
<div class="line">      std::cout &lt;&lt; res &lt;&lt; std::endl;</div>
<div class="line">      app.exit();</div>
<div class="line">    },</div>
<div class="line">    0, <span class="comment">// Qt main loop</span></div>
<div class="line">    [] () -&gt; std::string {</div>
<div class="line">      <span class="comment">// pretend this is compute intensive!</span></div>
<div class="line">      timespec ts = {0, 100000000};</div>
<div class="line">      nanosleep(&amp;ts, 0);</div>
<div class="line">      <span class="keywordflow">return</span> <span class="stringliteral">&quot;The answer is 42&quot;</span>;</div>
<div class="line">    }</div>
<div class="line">  );</div>
<div class="line"></div>
<div class="line">  app.exec();</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Mon Sep 16 2013 20:45:31 for c++-gtk-utils by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.4
</small></address>
</body>
</html>