<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[App crashes when using findChild&lt;&gt; in Qt]]></title><description><![CDATA[<p dir="auto">Nothing is there in my .ui file. In <code>mainwindow.cpp</code> I'm just adding one widget <code>Test</code> inside which I'm adding one label <code>TLabel</code><br />
mainwindow.cpp:</p>
<pre><code>#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);
    Test = new QWidget(this);
    TLayout = new QVBoxLayout(Test);
    TLayout-&gt;setContentsMargins(0,0,0,0);
    TLayout-&gt;setSpacing(2);
    TLabel = new QLabel(Test);
    TLabel-&gt;setObjectName("MyLabel");
    TLabel-&gt;setText("Done Task!!");
    TLayout-&gt;addWidget(TLabel);
    Test-&gt;setLayout(TLayout);

    MainWindow* app = qobject_cast&lt;MainWindow*&gt;(QApplication::instance());
    QWidget* list1 = app-&gt;Test-&gt;findChild&lt;QWidget*&gt;("MyLabel");
    qDebug()&lt;&lt;list1;

}

MainWindow::~MainWindow()
{
    delete ui;
}
</code></pre>
<p dir="auto">When I'm trying to access <code>TLabel</code> using findChild&lt;&gt; method, it shows:</p>
<pre><code>Desktop_Qt_5_15_2_MSVC2019_64bit-Debug\debug\untitled.exe ...
11:56:32: The program has unexpectedly finished.
11:56:32: The process was ended forcefully.
11:56:32: C:\Users\Meera\OneDrive\Desktop\build-untitled-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug\debug\untitled.exe crashed.
</code></pre>
]]></description><link>https://forum.qt.io/topic/127332/app-crashes-when-using-findchild-in-qt</link><generator>RSS for Node</generator><lastBuildDate>Thu, 18 Jun 2026 17:27:32 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/127332.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 06 Jun 2021 06:28:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Wed, 09 Jun 2021 07:26:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/meera-hadid">@<bdi>Meera-Hadid</bdi></a> said in <a href="/post/663676">App crashes when using findChild&lt;&gt; in Qt</a>:</p>
<blockquote>
<p dir="auto">MainWindow* MainWindow::Get() {<br />
MainWindow* app = qobject_cast&lt;MainWindow*&gt;(QApplication::instance());<br />
return app;<br />
}</p>
<p dir="auto">void MainWindow::on_pushButton_4_clicked()<br />
{<br />
Form* temp = new Form(this);<br />
temp-&gt;show();<br />
}</p>
</blockquote>
<p dir="auto">You should listen to the others, but I will still answer to the approach you are trying so far. First of all: Drop <code>MainWindow::Get()</code>! There is no way around this. A <code>MainWindow</code> is a <code>MainWindow</code> and a <code>QApplication</code> is a <code>QApplication</code>. Casting between the two will never work. In Qt you have a <code>QApplication</code> object and usually a main window. You could also have no main window or several. So, it is up to you to make main window known to others.</p>
<p dir="auto">The worst solution to this is to use the Singleton pattern. Currently, there is a trend to discourage the Singleton pattern in any form whatsoever in C++. Though this would certainly solve your problem as long as you only have a single main window. (The reason why the use of Singletons is discouraged is because of testing.)</p>
<p dir="auto">A slightly better solution is that you already pass <code>this</code> (which is a <code>MainWindow</code>) as parent to your <code>Form</code>'s constructor. If the <code>Form</code> constructor correctly calls is superclass <code>QObject</code> (or whatever Qt class it's derived from), you can use (within <code>Form</code>) <code>qobject_cast&lt;MainWindow*&gt;(this-&gt;parent())</code>. This is not a nice solution, though (still it works). One way to make this better is to change your <code>Form</code> constructor to take a <code>MainWindow*</code> as parent instead of any <code>QObject*</code>/<code>QWidget*</code>. This would make sure that the cast will not fail. Even better would be that you then also directly store the <code>MainWindow*</code> as a member variable and use this instead of <code>parent()</code>. The general scheme of this approach is: Use the constructor of <code>Form</code> to hand down the <code>MainWindow</code> directly. Let the <code>Form</code> know about its <code>MainWindow</code>. This is the easiest way to access your <code>MainWindow</code>. This eliminates the use of <code>MainWindow::Get()</code> altogether. When you create your <code>Form</code> object you will always have the correct <code>MainWindow</code> at hand to pass it down.</p>
<p dir="auto">As others have mentioned, this is not the best approach as now <code>Form</code> and <code>MainWindow</code> are highly coupled. Using signals and slots we can easily make everything work together perfectly.<br />
Step 1: (I assume that in the end the tool button should be visually located within the Form.) Create the tool button inside the Form. Have a signal <code>void Form::fit_to_window_button_pressed()</code> and connect the tool buttons <code>triggered()</code> signal with this new signal. Whenever you click on the tool button, <code>Form</code> will emit a signal. (You can connect signals to signals.)<br />
Step 2: When you create a <code>Form</code> in <code>MainWindow</code> you can connect the new signal from <code>Form</code> with whatever you like. Since you already have <code>actionFit_to_window</code> (and it might be triggered in other ways as well) I suggest that to connect to its trigger slot:</p>
<pre><code>Form* temp = new Form(this);
QObject::connect(temp, SIGNAL(fit_to_window_button_pressed()),
                 this-&gt;actionFit_to_window, SLOT(trigger()));
...
// and just like you have it already
QObject::connect(this-&gt;actionFit_to_window, SIGNAL(triggered()),
                 q, SLOT(fitSliceToBackground()));
</code></pre>
<p dir="auto">The order of the <code>connect</code> statements does not matter. Neither does it matter that all objects exist before you can do any of the connects. Only the objects which are used as sender and receiver in this particular connect statement need to be created by that time. Chaining signals and slots is a good way to decouple several classes in Qt. Nobody has to know much about the other. (<code>Form</code> does not know anything about <code>MainWindow</code> and <code>MainWindow</code> doesn't care that you are using a <code>QToolButton</code> inside <code>Form</code>. You could easily change the tool button to something different later.)</p>
<p dir="auto">BTW, you should switch to the new connect syntax which will fail at compile time instead of at runtime:</p>
<pre><code>QObject::connect(temp, &amp;Form::fit_to_window_button_pressed,
                 this-&gt;actionFit_to_window, &amp;QAction::trigger);
//and
QObject::connect(this-&gt;actionFit_to_window, &amp;QAction::triggered,
                 q, &amp;qMRMLSliceControllerWidget::fitSliceToBackground);</code></pre>
]]></description><link>https://forum.qt.io/post/664107</link><guid isPermaLink="true">https://forum.qt.io/post/664107</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Wed, 09 Jun 2021 07:26:16 GMT</pubDate></item><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Sun, 06 Jun 2021 13:10:09 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
If you really insist on calling to MainWindow  then<br />
you could define a new public signal in New Form.</p>
<p dir="auto">then connect<br />
this new signal to q fitSliceToBackground slot in mainwindow</p>
<p dir="auto">Then your NEW button can trigger the same as FitToWindowToolButton from the other form.</p>
<pre><code>void MainWindow::on_pushButton_4_clicked()
{
    Form* temp = new Form(this);   
    QObject::connect(temp, SIGNAL(MySignal()),q, SLOT(fitSliceToBackground()));
    temp-&gt;show();
}
</code></pre>
<p dir="auto">then in Form .h</p>
<p dir="auto">signals:<br />
void MySignal();</p>
<p dir="auto">in form.cpp<br />
for the new toolbutton clicked</p>
<pre><code>
void Form::on_newToolButton_clicked()
{
    emit MySignal(); // send signal to main
}

</code></pre>
]]></description><link>https://forum.qt.io/post/663705</link><guid isPermaLink="true">https://forum.qt.io/post/663705</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 06 Jun 2021 13:10:09 GMT</pubDate></item><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Sun, 06 Jun 2021 13:06:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/meera-hadid">@<bdi>Meera-Hadid</bdi></a><br />
But that's precisely the wrong thing to do.  If you really have a <code>QToolbar</code> which can only function if it calls something in <code>MainWindow</code> then you cannot/should not attempt to use it in <code>Form</code>.</p>
<blockquote>
<p dir="auto">a function called <code>fitSliceToBackground()</code> which is linked with other function in <code>mainwindow.cpp</code></p>
</blockquote>
<p dir="auto">Glancing I cannot see what in <code>void qMRMLSliceControllerWidget::fitSliceToBackground()</code> is linked to anything in <code>mainwindow.cpp</code>.</p>
<p dir="auto">In any case it's up to you refactor as necessary.  You cannot/must not do what you are seeming to want to do.</p>
<p dir="auto">On top of everything else, as <a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> observed you can neither directly copy a <code>QToolbar</code> or <code>QToolbutton</code>, nor can you "re-use" an existing one outside of their parent.</p>
]]></description><link>https://forum.qt.io/post/663703</link><guid isPermaLink="true">https://forum.qt.io/post/663703</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 06 Jun 2021 13:06:02 GMT</pubDate></item><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Sun, 06 Jun 2021 12:49:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> <a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<p dir="auto">code for QToolButton in <code>mainwindow.cpp</code> file:</p>
<pre><code>this-&gt;FitToWindowToolButton = new QToolButton(q);
  this-&gt;FitToWindowToolButton-&gt;setObjectName("FitToWindowToolButton");
  this-&gt;FitToWindowToolButton-&gt;setAutoRaise(true);
  this-&gt;FitToWindowToolButton-&gt;setDefaultAction(this-&gt;actionFit_to_window);
  this-&gt;FitToWindowToolButton-&gt;setFixedSize(15, 15);

QObject::connect(this-&gt;actionFit_to_window, SIGNAL(triggered()),
                   q, SLOT(fitSliceToBackground()));

void qMRMLSliceControllerWidget::fitSliceToBackground()
{
  Q_D(qMRMLSliceControllerWidget);
  d-&gt;SliceLogic-&gt;StartSliceNodeInteraction(vtkMRMLSliceNode::ResetFieldOfViewFlag);
  d-&gt;SliceLogic-&gt;FitSliceToAll();
  d-&gt;MRMLSliceNode-&gt;UpdateMatrices();
  d-&gt;SliceLogic-&gt;EndSliceNodeInteraction();
}
</code></pre>
<p dir="auto">As you can see the action of this <code>QToolButton</code> is linked with a function called <code>fitSliceToBackground()</code>  which is linked with other function in <code>mainwindow.cpp</code> so it becomes difficult for me to creat a <code>QToolButton</code> in <code>Form.cpp</code> from scratch, hence I just want to export this button to <code>Form.cpp</code>.</p>
]]></description><link>https://forum.qt.io/post/663702</link><guid isPermaLink="true">https://forum.qt.io/post/663702</guid><dc:creator><![CDATA[Meera Hadid]]></dc:creator><pubDate>Sun, 06 Jun 2021 12:49:10 GMT</pubDate></item><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Sun, 06 Jun 2021 11:17:34 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/meera-hadid">@<bdi>Meera-Hadid</bdi></a><br />
Sounds a bit fishy, if whatever you mean by "all of it's dependencies are defined in <code>mainwindow.cpp</code> file" implies that the <code>QToolButton</code> will not function standalone in <code>Form</code> without having the <code>MainWindow</code> in existence.  In theory we like <code>Form</code> to be a standalone whatever-it-is, so it could be used even if you didn't have the main window.  That would call for some sort of reorganization like making it its own widget or putting whatever code it needs into a shared file.</p>
<p dir="auto">Having said that, from where you are now still get rid of <code>MainWindow* MainWindow::Get()</code> method/approach.  If really necessary have the <code>MainWindow</code> create the copy of the <code>QToolbar</code> and pass it to <code>Form</code> (might be via the constructor, might be via a <code>set()</code>ter method exported from <code>Form</code>).  Or pass whatever parameters are necessary to create the <code>QToolbar</code> in <code>Form</code>.  Either way make <code>MainForm</code> export something to <code>Form</code> instead of having <code>Form</code> try to access <code>MainWindow</code>.</p>
]]></description><link>https://forum.qt.io/post/663694</link><guid isPermaLink="true">https://forum.qt.io/post/663694</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 06 Jun 2021 11:17:34 GMT</pubDate></item><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Sun, 06 Jun 2021 11:11:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/meera-hadid">@<bdi>Meera-Hadid</bdi></a><br />
Hi</p>
<ul>
<li>I want to make copy of this QToolButton in form.cpp</li>
</ul>
<p dir="auto">you cannot copy widgets. if you assigned the button from mainwindow to "form", it will be torn out of the<br />
mainwindow and moved into form.</p>
<ul>
<li>it's dependencies are defined in mainwindow.cpp</li>
</ul>
<p dir="auto">What would those be ?</p>
<p dir="auto">Did you set it up in Designer and want to reuse this setup in a new widget ?</p>
<p dir="auto">Or do you really want the button in Form to call functions in mainwindow ?</p>
]]></description><link>https://forum.qt.io/post/663692</link><guid isPermaLink="true">https://forum.qt.io/post/663692</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 06 Jun 2021 11:11:56 GMT</pubDate></item><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Sun, 06 Jun 2021 10:59:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
I have a QToolButton in my <code>mainwindow.ui</code> file, all of it's dependencies are defined in <code>mainwindow.cpp</code> file. I want to make copy of this QToolButton in <code>form.cpp</code> which is not directly related to <code>mainwindow.cpp</code>.</p>
<p dir="auto">So is there a solution for this problem.</p>
]]></description><link>https://forum.qt.io/post/663691</link><guid isPermaLink="true">https://forum.qt.io/post/663691</guid><dc:creator><![CDATA[Meera Hadid]]></dc:creator><pubDate>Sun, 06 Jun 2021 10:59:16 GMT</pubDate></item><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Sun, 06 Jun 2021 09:18:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/meera-hadid">@<bdi>Meera-Hadid</bdi></a><br />
Having some other widget (<code>Form</code>) attempting to access a <code>QMainWindow</code>, or attempting to access anything on that main window, is indicative of a bad approach.  Descendant widgets should not know, or need to now, about ancestor widgets.  <code>QWidget* myList = MainWindow::Get()-&gt;Test-&gt;findChild&lt;QWidget*&gt;();</code> is not a good approach, and should not be necessary.</p>
<p dir="auto">Why does <code>Form</code> need to do this, what is it trying to achieve and why?  If anything <code>MainWindow</code> should be doing this work.  Instead <code>MainWindow</code> might pass the required widget to <code>Form</code>, or <code>Form</code> might emit a signal which <code>MainWindow</code> has a slot onto to do its work.  Redesign your code to take a different approach.</p>
]]></description><link>https://forum.qt.io/post/663684</link><guid isPermaLink="true">https://forum.qt.io/post/663684</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 06 Jun 2021 09:18:24 GMT</pubDate></item><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Sun, 06 Jun 2021 08:51:07 GMT]]></title><description><![CDATA[<p dir="auto">Hey, I'm new to Qt can you please demonstrate the code or give any references for this problem.</p>
]]></description><link>https://forum.qt.io/post/663681</link><guid isPermaLink="true">https://forum.qt.io/post/663681</guid><dc:creator><![CDATA[Meera Hadid]]></dc:creator><pubDate>Sun, 06 Jun 2021 08:51:07 GMT</pubDate></item><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Sun, 06 Jun 2021 08:30:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/meera-hadid">@<bdi>Meera-Hadid</bdi></a> said in <a href="/post/663676">App crashes when using findChild&lt;&gt; in Qt</a>:</p>
<blockquote>
<p dir="auto">My main aim is I want to access the widgets of the <code>mainwindow.ui</code> file in other class(<code>form.cxx</code>)</p>
<p dir="auto">mainwindow.cxx:</p>
<pre><code>MainWindow* MainWindow::Get() {
    MainWindow* app = qobject_cast&lt;MainWindow*&gt;(QApplication::instance());
    return app;
}

</code></pre>
</blockquote>
<p dir="auto">Moving this code from the constructor to a member function doesn't change the fact that the QApplication instance is not a MainWindow. Using qobject_cast to attempt to turn it into one will always return a null pointer.</p>
<blockquote>
<p dir="auto">when I click on the <code>on_pushButton_4</code> button the window of the form.ui file doesn't open<br />
it shows error:</p>
<pre><code>13:47:20: The program has unexpectedly finished.
13:47:20: The process was ended forcefully.
13:47:20: C:\Users\Meera\OneDrive\Desktop\build-untitled-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug\debug\untitled.exe crashed.
</code></pre>
<p dir="auto">the error is because of this <code> QWidget* myList = MainWindow::Get()-&gt;Test-&gt;findChild&lt;QWidget*&gt;();</code> line and I don't know any other alternative way for this problem.</p>
</blockquote>
<p dir="auto">If there is only one MainWindow, track that instance in a variable and use it directly. If there can be multiple instances, iterating through QApplication::topLevelWidgets() is an option.</p>
]]></description><link>https://forum.qt.io/post/663678</link><guid isPermaLink="true">https://forum.qt.io/post/663678</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Sun, 06 Jun 2021 08:30:50 GMT</pubDate></item><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Sun, 06 Jun 2021 08:24:30 GMT]]></title><description><![CDATA[<p dir="auto">My main aim is I want to access the widgets of the <code>mainwindow.ui</code> file in other class(<code>form.cxx</code>)</p>
<p dir="auto">mainwindow.cxx:</p>
<pre><code>#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
#include "form.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);
    Test = new QWidget(this);
    TLayout = new QVBoxLayout(Test);
    TLayout-&gt;setContentsMargins(0,0,0,0);
    TLayout-&gt;setSpacing(2);
    TLabel = new QLabel(Test);
    TLabel-&gt;setObjectName("MyLabel");
    TLabel-&gt;setText("Done Task!!");
    TLayout-&gt;addWidget(TLabel);
    Test-&gt;setLayout(TLayout);
}

MainWindow::~MainWindow()
{
    delete ui;
}

MainWindow* MainWindow::Get() {
    MainWindow* app = qobject_cast&lt;MainWindow*&gt;(QApplication::instance());
    return app;
}

void MainWindow::on_pushButton_4_clicked()
{
    Form* temp = new Form(this);
    temp-&gt;show();
}
</code></pre>
<p dir="auto">form.cxx:</p>
<pre><code>#include "form.h"
#include "ui_form.h"
#include "mainwindow.h"
#include "QDebug"
Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui-&gt;setupUi(this);
    QWidget* myList = MainWindow::Get()-&gt;Test-&gt;findChild&lt;QWidget*&gt;();
    qDebug() &lt;&lt;"myList: "&lt;&lt;myList;
}

Form::~Form()
{
    delete ui;
}
</code></pre>
<p dir="auto">when I click on the <code>on_pushButton_4</code> button the window of the form.ui file doesn't open<br />
it shows error:</p>
<pre><code>13:47:20: The program has unexpectedly finished.
13:47:20: The process was ended forcefully.
13:47:20: C:\Users\Meera\OneDrive\Desktop\build-untitled-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug\debug\untitled.exe crashed.
</code></pre>
<p dir="auto">the error is because of this <code> QWidget* myList = MainWindow::Get()-&gt;Test-&gt;findChild&lt;QWidget*&gt;();</code> line and I don't know any other alternative solution for this problem.</p>
]]></description><link>https://forum.qt.io/post/663676</link><guid isPermaLink="true">https://forum.qt.io/post/663676</guid><dc:creator><![CDATA[Meera Hadid]]></dc:creator><pubDate>Sun, 06 Jun 2021 08:24:30 GMT</pubDate></item><item><title><![CDATA[Reply to App crashes when using findChild&lt;&gt; in Qt on Sun, 06 Jun 2021 07:03:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/meera-hadid">@<bdi>Meera-Hadid</bdi></a> said in <a href="/post/663667">App crashes when using findChild&lt;&gt; in Qt</a>:</p>
<blockquote>
<p dir="auto">Nothing is there in my .ui file. In <code>mainwindow.cpp</code> I'm just adding one widget <code>Test</code> inside which I'm adding one label <code>TLabel</code><br />
mainwindow.cpp:</p>
<pre><code></code></pre>
</blockquote>
<blockquote>
<p dir="auto">MainWindow::MainWindow(QWidget *parent)<br />
: QMainWindow(parent)<br />
, ui(new Ui::MainWindow)<br />
{</p>
</blockquote>
<p dir="auto">...</p>
<blockquote>
<pre><code>MainWindow* app = qobject_cast&lt;MainWindow*&gt;(QApplication::instance());
QWidget* list1 = app-&gt;Test-&gt;findChild&lt;QWidget*&gt;("MyLabel");
</code></pre>
</blockquote>
<p dir="auto">...</p>
<blockquote>
<pre><code></code></pre>
</blockquote>
<p dir="auto">MainWindow appears to be derived from QMainWindow. QApplication::instance() returns a QApplication *. Therefore, qobject_cast&lt;MainWindow*&gt;(QApplication::instance()) will return <code>nullptr</code>.<br />
(nullptr)-&gt;Test is an invalid dereference, leading to:</p>
<blockquote>
<pre><code>Desktop_Qt_5_15_2_MSVC2019_64bit-Debug\debug\untitled.exe ...
11:56:32: The program has unexpectedly finished.
11:56:32: The process was ended forcefully.
11:56:32: C:\Users\Meera\OneDrive\Desktop\build-untitled-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug\debug\untitled.exe crashed.
</code></pre>
</blockquote>
<p dir="auto">Perhaps <code>this-&gt;Test-&gt;findChild()</code>, or <code>Test-&gt;findChild()</code> is what you're looking for.</p>
]]></description><link>https://forum.qt.io/post/663671</link><guid isPermaLink="true">https://forum.qt.io/post/663671</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Sun, 06 Jun 2021 07:03:23 GMT</pubDate></item></channel></rss>