Qt Forum

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

    Connection between Qpushbutton with line Edit widgets

    General and Desktop
    4
    5
    2810
    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.
    • C
      cholokid last edited by

      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!!!

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to devnet,

        There's an "example":http://qt-project.org/doc/qt-4.8/widgets-calculator.html in Qt's documentation doing exactly that

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • B
          BlackStar last edited by

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

               connect(oneButton,SIGNAL(clicked()),this,SLOT(concatOne()));
           }
          
          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 !

          1 Reply Last reply Reply Quote 0
          • C
            cholokid last edited by

            the first file, mainwindow.h

            @#ifndef MAINWINDOW_H
            #define MAINWINDOW_H

            #include <QMainWindow>

            namespace Ui {
            class MainWindow;
            }@

            @class MainWindow : public QMainWindow
            {
            Q_OBJECT

            public:@
            @ explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();

            private:
            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;
            w.show();@
            @
            return a.exec();
            }@

            third file, mainwindow.cpp

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

            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
            {
            ui->setupUi(this);@

            @
            }

            MainWindow::~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.

            1 Reply Last reply Reply Quote 0
            • J
              JvdGlind last edited by

              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()
              {
              ui->lineEdit->clear();
              ui->lineEdit->insert(QString('1'));
              }
              @

              Jeffrey VAN DE GLIND
              Principle Consultant @ Nalys
              www.nalys-group.com

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