How to acess value of lineedit in main window class to another class of widget



  • i have one class main window and by drag and drop i have created a widget and promoted it to another class.
    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.
    please help me out as i am new to qt.how can i access the value of mainwindow class to the widget class

    any help will be appreciated.i am doing it lyk this:

    .h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtGui>

    namespace Ui {
    class MainWindow;
    }

    class MyWidget : public QWidget
    {
    Q_OBJECT

    public:
    MyWidget(QWidget *parent = 0);

    private:
    QPushButton *myPushBtn;
    };

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private slots:
    void on_pushButton_2_clicked();

    private:
    Ui::MainWindow *ui;
    MyWidget *wid;
    };

    #endif // MAINWINDOW_H
    @

    .cpp

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MyWidget::MyWidget(QWidget *parent):
    QWidget(parent)
    {
    }

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    wid = ui->widget;
    }

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

    void MainWindow::on_pushButton_2_clicked()
    {

    }
    @

    please help me by adding in codes



  • @
    QString MyWidget::theValue() const {
    return ui->myPushBtn->text();
    }
    @



  • 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.

    Your missing in your constructor...or wherever fits:
    @
    connect(ui->button, SIGNAL(clicked()), this, SLOT(on_pushButton_2_clicked()));
    @

    And in your class public function that is located in your 'promoted' class:
    @
    QString Widget::getButtonText()
    {
    return ui->pushButton->text();
    }
    @



  • thankyou sir for your reply.
    but still i am facing problem.i added the above codes in my MyWidget class and getting error that ui is undeclared.
    i am doing it like this:
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtGui>

    namespace Ui {
    class MainWindow;
    }

    class MyWidget : public QWidget
    {
    Q_OBJECT

    public:
    MyWidget(QWidget *parent = 0);
    void paintEvent(QPaintEvent *);

     void drawship(QPainter &paint);
     bool m_flag ;
    

    QString getButtonText();

    private:

    };

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    bool m_flag ;
    void lineedit();
    MainWindow(QWidget *parent = 0);
    Ui::MainWindow *ui;
    ~MainWindow();

    private slots:
    void on_pushButton_2_clicked();

    private:

    MyWidget *wid;
    

    };

    #endif // MAINWINDOW_H@

    .cpp

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<QPainter.h>
    #include<QPen>
    #include<math.h>
    #include<QTimer>
    #include<QMouseEvent>
    #include<QPointF>
    #include<QLayout>

    int w=530;// width of the ploting window
    int h=570;//hieght of the plotting window
    int Y ; //y coordinates of the ship
    int X ; //x coordinates of the ship

    MyWidget::MyWidget(QWidget *parent):
    QWidget(parent)
    {
    }

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    m_flag=false;

    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(on_pushButton_2_clicked()));
    ui->setupUi(this);
    wid = ui->widget;

    }

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

    void MyWidget ::paintEvent(QPaintEvent *)
    {

    // dimensions of plotting window
    int x=0;
    int y=0;
    int w=530;
    int h=570;

    QPainter painter(this);
    painter.drawRect(x,y,w,h);

    QPainter paint(this);

    // setting coordinates system of plotting window
    paint.setViewport(x,y,w,h);
    paint.setWindow(-w/2,-h/2,w,h);
    paint.scale(1,-1);

    paint.setPen(Qt::blue);
    drawship(paint);
    }

    void MainWindow::lineedit()
    {

    if(m_flag==true)
    

    {
    QString px,py,pz;
    px=ui->lineEdit->text();

      X=px.toInt();
    
      py=ui->lineEdit_2->text();
      Y= py.toInt();
    

    }
    }

    void MyWidget::drawship(QPainter &paint)
    {
    paint.drawRect(X,Y,50,40);
    update();

    }
    QString MyWidget::getButtonText()
    {
    return ui->pushButton_2->text();
    }

    void MainWindow::on_pushButton_2_clicked()
    {
    m_flag = true;
    update();
    }
    @
    please tell me where i am getting wrong


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.