Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Need help in getting data after pressing pushbutton

    Mobile and Embedded
    3
    3
    2869
    Loading More Posts
    • 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.
    • D
      doforumda last edited by

      hi
      i am working on simple example. I have three pushbuttons and one lineEdit. all pushButtons have these Names (zeroButton, oneButton and threeButton). What i want is when the user press 0 then it should display that zero in LineEdit and when user press 1 or 2 then it should appear in lineEdit. the name of lineEdit is display. here is my code but it is not working. when i press any button it does not appear in lineEdit

      dialog.h
      @
      #ifndef DIALOG_H
      #define DIALOG_H

      #include <QDialog>

      namespace Ui {
      class Dialog;
      }

      class Dialog : public QDialog
      {
      Q_OBJECT

      public:
      explicit Dialog(QWidget *parent = 0);
      ~Dialog();
      public slots:
      void changeValue();

      private:
      Ui::Dialog *ui;
      };

      #endif // DIALOG_H
      @

      dialog.cpp
      @
      #include "dialog.h"
      #include "ui_dialog.h"

      Dialog::Dialog(QWidget *parent) :
      QDialog(parent),
      ui(new Ui::Dialog)
      {
      ui->setupUi(this);
      connect(ui->zeroButton, SIGNAL(clicked()), this, SLOT(changeValue()));
      connect(ui->oneButton, SIGNAL(clicked()), this, SLOT(changeValue()));
      connect(ui->twoButton, SIGNAL(clicked()), this, SLOT(changeValue()));
      }

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

      void Dialog::changeValue()
      {
      int numZero = ui->zeroButton->text().toInt();
      int numOne = ui->zeroButton->text().toInt();
      int numTwo = ui->zeroButton->text().toInt();

      QString resultNum = "";
      
      if(numZero)
          ui->display->setText(resultNum.setNum(numZero));
      
      if(numOne)
          ui->display->setText(resultNum.setNum(numOne));
      
      if(numTwo)
          ui->display->setText(resultNum.setNum(numTwo));
      

      }
      @

      main.cpp
      @
      #include <QtGui/QApplication>
      #include "dialog.h"

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      Dialog w;
      w.show();

      return a.exec&#40;&#41;;
      

      }
      @
      how can i achieve this?

      1 Reply Last reply Reply Quote 0
      • A
        alexman last edited by

        Try this code:
        @
        void Dialog::changeValue()
        {
        if ( sender() == ui->zeroButton ) {
        ui->display->setText( ui->zeroButton->text() );
        } else if ( sender() == ui->oneButton ) {
        ui->display->setText( ui->oneButton->text() );
        } else if ( sender() == ui->twoButton ) {
        ui->display->setText( ui->twoButton->text() );
        }
        }
        @

        1 Reply Last reply Reply Quote 0
        • T
          tobias.hunger last edited by

          This looks like a great place to use a "QSignalMapper":http://doc.trolltech.com/4.7/qsignalmapper.html.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post