Connection between Qpushbutton with line Edit widgets

  • My problem is that, i want to create a simple calculator as a desktop application,
    so , i have already create a Qpushbuttons with label 1,2,3,4,5 and so on . So if user click the button as signal, so the line Edit widget receive the corresponding number that is clicked(display that number).
    please help me!!!

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    There's an "example": in Qt's documentation doing exactly that

  • be the class CalculatorUi
    @class CalculatorUi : public Widget
    QPushButton* oneButton;
    QLineEdit* displayScreen;
    public :
    CalculatorUi(QWidget* parent) : QWidget(parent),oneButton(new QPushButton("1")),displayScreen(new QLineEdit())
    QVBoxLayout* layout (new QVBoxLayout);

    private slots :
      void concatOne()
          displayScreen->setText(displayScreen->text() + "1");


    This is just an example of how you can do it, hope it helped you,
    we have a button wich is done for 1, and the display screen wich is of class QLineEdit, i created a private slot called concatOne and i connected it to the signal clicked of the button, so when you click on this button it append "1" to the value that you have into the screen !

  • the first file, mainwindow.h

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;

    @class MainWindow : public QMainWindow

    @ explicit MainWindow(QWidget *parent = 0);

    Ui::MainWindow *ui;

    #endif // MAINWINDOW_H@

    second file, main.cpp
    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    MainWindow w;;@
    return a.exec();

    third file, mainwindow.cpp

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

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)


    delete ui;@

    fourth file, mainwindiw.ui
    this file , i have inserted the qpushbutton and line Edit widget, by draging.(graphically), so need to connenct the qpushbutton with line Edit, means that, if i clicked the button , line Edit show, the number to corresponding button, , i want to create a simple calculator. i am a beginer in qt programming.

  • Assuming your QLineEdit is called 'lineEdit' and your QPushButton is called 'pushButton', adding the following to MainWindow will get you started:

    @void MainWindow::on_pushButton_clicked()

Log in to reply

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