<?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[[SOLVED]Where are wrong,I need operate UI widget in my.cpp]]></title><description><![CDATA[<p dir="auto">I had add a listwidget in mainwindow.ui,Then i want to add item in testcalss.cpp by SIGNAL/SLOT,but i <a href="http://failed.My" target="_blank" rel="noopener noreferrer nofollow ugc">failed.My</a> code are followed,where are wrong?Any help will very appreciated.</p>
<p dir="auto">mainwindow.h</p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void additem(const QString&amp; text);
    //void getadd(const QString&amp; text);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
</code></pre>
<p dir="auto">mainwindow.cpp</p>
<pre><code>#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "testclass.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);
    ui-&gt;listWidget-&gt;addItem("text");

    testclass t;
    t.em();
}

void MainWindow::additem(const QString&amp; text)
{
    ui-&gt;listWidget-&gt;addItem("text");
    ui-&gt;listWidget-&gt;addItem(text);
}

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

</code></pre>
<p dir="auto">testclass.h</p>
<pre><code>#ifndef TESTCLASS_H
#define TESTCLASS_H

#include &lt;QObject&gt;
#include &lt;mainwindow.h&gt;

class testclass : public QObject
{
    Q_OBJECT
public:
    explicit testclass(QObject *parent = 0);
    ~testclass();

    void em();

signals:
    void add(const QString&amp; text);

public slots:

private:
    //MainWindow *mainwindow;
};

#endif // TESTCLASS_H
</code></pre>
<p dir="auto">testclass.cpp</p>
<pre><code>#include "testclass.h"

testclass::testclass(QObject *parent) : QObject(parent)
{
    connect(this,SIGNAL(add(const QString&amp;)),parent,SLOT(additem(const QString&amp;)));
}

void testclass::em()
{
    emit add("simulation");
}

testclass::~testclass()
{

}
</code></pre>
]]></description><link>https://forum.qt.io/topic/54790/solved-where-are-wrong-i-need-operate-ui-widget-in-my-cpp</link><generator>RSS for Node</generator><lastBuildDate>Tue, 17 Nov 2026 00:54:14 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/54790.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 01 Jun 2015 13:37:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED]Where are wrong,I need operate UI widget in my.cpp on Thu, 04 Jun 2015 05:00:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mcosta">@<bdi>mcosta</bdi></a> Thank you.I have many mistake in my code,like what you said.</p>
]]></description><link>https://forum.qt.io/post/276649</link><guid isPermaLink="true">https://forum.qt.io/post/276649</guid><dc:creator><![CDATA[Season02]]></dc:creator><pubDate>Thu, 04 Jun 2015 05:00:05 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED]Where are wrong,I need operate UI widget in my.cpp on Mon, 01 Jun 2015 15:53:22 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">the problem is that you create the <code>testsclass</code> instance <code>t</code> inside the <code>MainWindow</code> constructor and when it goes out of the scope it is destroyed (also invalidating connections).<br />
Also you used the <code>parent</code> in the <code>connect</code> function but you never set it.</p>
<p dir="auto">You could define <code>t</code> as member of <code>MainWindow</code> or, better, in the <code>main()</code> function.</p>
]]></description><link>https://forum.qt.io/post/276250</link><guid isPermaLink="true">https://forum.qt.io/post/276250</guid><dc:creator><![CDATA[mcosta]]></dc:creator><pubDate>Mon, 01 Jun 2015 15:53:22 GMT</pubDate></item></channel></rss>