Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to acess value of lineedit in main window class to another class of widget
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sidharth
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • 8 Offline
      8 Offline
      8majkel8
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        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();
        }
        @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sidharth
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved