<?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[How to acess value of lineedit in main window class to another class of widget]]></title><description><![CDATA[<p dir="auto">i have one class main window and by drag and drop i have created a widget and promoted it to another class.<br />
now i m having my lineedit and pushbutton on mainwindow .i want to access the value of lineedit to the widget class on a click of button.<br />
please help me out as i am new to qt.how can i access the value of mainwindow class to the widget class</p>
<p dir="auto">any help will be appreciated.i am doing it lyk this:</p>
<p dir="auto">.h<br />
@<br />
#ifndef MAINWINDOW_H<br />
#define MAINWINDOW_H</p>
<p dir="auto">#include &lt;QMainWindow&gt;<br />
#include &lt;QtGui&gt;</p>
<p dir="auto">namespace Ui {<br />
class MainWindow;<br />
}</p>
<p dir="auto">class MyWidget : public QWidget<br />
{<br />
Q_OBJECT</p>
<p dir="auto">public:<br />
MyWidget(QWidget *parent = 0);</p>
<p dir="auto">private:<br />
QPushButton *myPushBtn;<br />
};</p>
<p dir="auto">class MainWindow : public QMainWindow<br />
{<br />
Q_OBJECT</p>
<p dir="auto">public:<br />
MainWindow(QWidget *parent = 0);<br />
~MainWindow();</p>
<p dir="auto">private slots:<br />
void on_pushButton_2_clicked();</p>
<p dir="auto">private:<br />
Ui::MainWindow *ui;<br />
MyWidget *wid;<br />
};</p>
<p dir="auto">#endif // MAINWINDOW_H<br />
@</p>
<p dir="auto">.cpp</p>
<p dir="auto">@#include "mainwindow.h"<br />
#include "ui_mainwindow.h"</p>
<p dir="auto">MyWidget::MyWidget(QWidget *parent):<br />
QWidget(parent)<br />
{<br />
}</p>
<p dir="auto">MainWindow::MainWindow(QWidget *parent) :<br />
QMainWindow(parent),<br />
ui(new Ui::MainWindow)<br />
{<br />
ui-&gt;setupUi(this);<br />
wid = ui-&gt;widget;<br />
}</p>
<p dir="auto">MainWindow::~MainWindow()<br />
{<br />
delete ui;<br />
}</p>
<p dir="auto">void MainWindow::on_pushButton_2_clicked()<br />
{</p>
<p dir="auto">}<br />
@</p>
<p dir="auto">please help me by adding in codes</p>
]]></description><link>https://forum.qt.io/topic/28249/how-to-acess-value-of-lineedit-in-main-window-class-to-another-class-of-widget</link><generator>RSS for Node</generator><lastBuildDate>Thu, 12 Nov 2026 15:56:21 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/28249.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 13 Jun 2013 10:19:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to acess value of lineedit in main window class to another class of widget on Fri, 14 Jun 2013 07:26:44 GMT]]></title><description><![CDATA[<p dir="auto">thankyou sir for your reply.<br />
but still i am facing problem.i added the above codes in my MyWidget class and getting error that ui is undeclared.<br />
i am doing it like this:<br />
@#ifndef MAINWINDOW_H<br />
#define MAINWINDOW_H</p>
<p dir="auto">#include &lt;QMainWindow&gt;<br />
#include &lt;QtGui&gt;</p>
<p dir="auto">namespace Ui {<br />
class MainWindow;<br />
}</p>
<p dir="auto">class MyWidget : public QWidget<br />
{<br />
Q_OBJECT</p>
<p dir="auto">public:<br />
MyWidget(QWidget *parent = 0);<br />
void paintEvent(QPaintEvent *);</p>
<pre><code> void drawship(QPainter &amp;paint);
 bool m_flag ;
</code></pre>
<p dir="auto">QString getButtonText();</p>
<p dir="auto">private:</p>
<p dir="auto">};</p>
<p dir="auto">class MainWindow : public QMainWindow<br />
{<br />
Q_OBJECT</p>
<p dir="auto">public:<br />
bool m_flag ;<br />
void lineedit();<br />
MainWindow(QWidget *parent = 0);<br />
Ui::MainWindow *ui;<br />
~MainWindow();</p>
<p dir="auto">private slots:<br />
void on_pushButton_2_clicked();</p>
<p dir="auto">private:</p>
<pre><code>MyWidget *wid;
</code></pre>
<p dir="auto">};</p>
<p dir="auto">#endif // MAINWINDOW_H@</p>
<p dir="auto">.cpp</p>
<p dir="auto">@#include "mainwindow.h"<br />
#include "ui_mainwindow.h"<br />
#include&lt;QPainter.h&gt;<br />
#include&lt;QPen&gt;<br />
#include&lt;math.h&gt;<br />
#include&lt;QTimer&gt;<br />
#include&lt;QMouseEvent&gt;<br />
#include&lt;QPointF&gt;<br />
#include&lt;QLayout&gt;</p>
<p dir="auto">int w=530;// width of the ploting window<br />
int h=570;//hieght of the plotting window<br />
int Y   ; //y coordinates of the ship<br />
int X ; //x coordinates of the ship</p>
<p dir="auto">MyWidget::MyWidget(QWidget *parent):<br />
QWidget(parent)<br />
{<br />
}</p>
<p dir="auto">MainWindow::MainWindow(QWidget *parent) :<br />
QMainWindow(parent),<br />
ui(new Ui::MainWindow)<br />
{<br />
m_flag=false;</p>
<p dir="auto">connect(ui-&gt;pushButton_2, SIGNAL(clicked()), this, SLOT(on_pushButton_2_clicked()));<br />
ui-&gt;setupUi(this);<br />
wid = ui-&gt;widget;</p>
<p dir="auto">}</p>
<p dir="auto">MainWindow::~MainWindow()<br />
{<br />
delete ui;<br />
}</p>
<p dir="auto">void MyWidget ::paintEvent(QPaintEvent *)<br />
{</p>
<p dir="auto">// dimensions of plotting window<br />
int x=0;<br />
int y=0;<br />
int w=530;<br />
int h=570;</p>
<p dir="auto">QPainter painter(this);<br />
painter.drawRect(x,y,w,h);</p>
<p dir="auto">QPainter paint(this);</p>
<p dir="auto">// setting coordinates system of plotting window<br />
paint.setViewport(x,y,w,h);<br />
paint.setWindow(-w/2,-h/2,w,h);<br />
paint.scale(1,-1);</p>
<p dir="auto">paint.setPen(Qt::blue);<br />
drawship(paint);<br />
}</p>
<p dir="auto">void MainWindow::lineedit()<br />
{</p>
<pre><code>if(m_flag==true)
</code></pre>
<p dir="auto">{<br />
QString px,py,pz;<br />
px=ui-&gt;lineEdit-&gt;text();</p>
<pre><code>  X=px.toInt();

  py=ui-&gt;lineEdit_2-&gt;text();
  Y= py.toInt();
</code></pre>
<p dir="auto">}<br />
}</p>
<p dir="auto">void MyWidget::drawship(QPainter &amp;paint)<br />
{<br />
paint.drawRect(X,Y,50,40);<br />
update();</p>
<p dir="auto">}<br />
QString MyWidget::getButtonText()<br />
{<br />
return ui-&gt;pushButton_2-&gt;text();<br />
}</p>
<p dir="auto">void MainWindow::on_pushButton_2_clicked()<br />
{<br />
m_flag = true;<br />
update();<br />
}<br />
@<br />
please tell me where i am getting wrong</p>
]]></description><link>https://forum.qt.io/post/182795</link><guid isPermaLink="true">https://forum.qt.io/post/182795</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 14 Jun 2013 07:26:44 GMT</pubDate></item><item><title><![CDATA[Reply to How to acess value of lineedit in main window class to another class of widget on Thu, 13 Jun 2013 15:39:19 GMT]]></title><description><![CDATA[<p dir="auto">you have to connect your slot to the signal of the button as well...and as he states above, make a public function in your widget class returns the push button text.</p>
<p dir="auto">Your missing in your constructor...or wherever fits:<br />
@<br />
connect(ui-&gt;button, SIGNAL(clicked()), this, SLOT(on_pushButton_2_clicked()));<br />
@</p>
<p dir="auto">And in your class public function that is located in your 'promoted' class:<br />
@<br />
QString Widget::getButtonText()<br />
{<br />
return ui-&gt;pushButton-&gt;text();<br />
}<br />
@</p>
]]></description><link>https://forum.qt.io/post/182710</link><guid isPermaLink="true">https://forum.qt.io/post/182710</guid><dc:creator><![CDATA[vezprog]]></dc:creator><pubDate>Thu, 13 Jun 2013 15:39:19 GMT</pubDate></item><item><title><![CDATA[Reply to How to acess value of lineedit in main window class to another class of widget on Thu, 13 Jun 2013 15:15:34 GMT]]></title><description><![CDATA[<p dir="auto">@<br />
QString MyWidget::theValue() const {<br />
return ui-&gt;myPushBtn-&gt;text();<br />
}<br />
@</p>
]]></description><link>https://forum.qt.io/post/182702</link><guid isPermaLink="true">https://forum.qt.io/post/182702</guid><dc:creator><![CDATA[8majkel8]]></dc:creator><pubDate>Thu, 13 Jun 2013 15:15:34 GMT</pubDate></item></channel></rss>