Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have two straightforward options</p> <p>You can use <a href="http://qt-project.org/doc/qt-4.8/qmetaobject.html#invokeMethod" rel="nofollow"><code>QMetaObject::invokeMethod</code></a> static method to invoke any invokable method, all you need is a <code>QObject*</code> pointer and the method name (and parameters, if there are any). To make method invokable, either use <a href="http://qt-project.org/doc/qt-4.8/qobject.html#Q_INVOKABLE" rel="nofollow"><code>Q_INVOKABLE</code></a> macro in your subclass definition, or simply make the method into a Qt slot. Then you need just the <code>QObject</code> pointer, and can (try to) call any method with any arguments (and get error if no such method exists).</p> <p>Another way is to use <a href="http://qt-project.org/doc/qt-4.8/qobject.html#qobject_cast" rel="nofollow"><code>qobject_cast&lt;&gt;()</code></a>, which tries to cast a <em>QObject</em> subclass pointer to another, and will return <code>nullptr</code>, if it can't be done (object is not of that class). And once you have the right pointer type, just call any method you like. It is much like standard C++ <code>dynamic_cast&lt;&gt;</code>, but works only for QObject subclasses, because it uses the Qt meta-object system and does not depend on C++ RTTI.</p> <p>Note: these two ways are something you should use when there is no nice way to have a separate, GUI-independent way to access the widget. But in your case here, these seem to make things so much simpler, it is justified, as long as you plan to keep the <code>QTabWidget</code> around for the lifetime of the app.</p> <hr> <p>Code example from comment of OP, on using the <code>invokeMethod</code> way to solve this exact problem:</p> <pre><code>QMetaObject::invokeMethod(myTabWidget-&gt;currentWidget(), "callbackFunction"); </code></pre> <p>This has the benefit, that now it is possible to have two <code>QObject</code> subclasses which are otherwise unrelated, and just have invokable method with same name (not overriden version of a common superclass method), and the method can be called. There's no need to have the class definition available at compile time, and <em>there is no need to add a common base class or introduce an abstract interface class and use multiple inheritance</em>. This is basically like duck typing: if <code>QMetaObject::invokeMethod(ptr, "quack")</code>, then it is a duck.</p> <p>Downside is then that <em>there is no compile time type checking</em>, simple typo in the method name string can make the code silently fail. So it's important to think (and implement and comment in code, and test) what to do when the <code>invokeMethod</code> fails at runtime (is it a bug, or a normal situation for some UI states?). But then this applies to (Qt4 syntax of) <code>QObject::connect</code> too, as many Qt programmers have learned the hard way, when slots did not get called because of a typo :-).</p>
    singulars
    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.
 

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