Need help in getting data after pressing pushbutton



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



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


  • Moderators

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


Log in to reply
 

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