Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Need help in getting data after pressing pushbutton
Forum Updated to NodeBB v4.3 + New Features

Need help in getting data after pressing pushbutton

Scheduled Pinned Locked Moved Mobile and Embedded
3 Posts 3 Posters 3.1k 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.
  • D Offline
    D Offline
    doforumda
    wrote on last edited by
    #1

    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
    0
    • A Offline
      A Offline
      alexman
      wrote on last edited by
      #2

      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
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on last edited by
        #3

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

        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