Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connection between Qpushbutton with line Edit widgets
Forum Updated to NodeBB v4.3 + New Features

Connection between Qpushbutton with line Edit widgets

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 3.8k 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.
  • C Offline
    C Offline
    cholokid
    wrote on 29 Apr 2014, 20:38 last edited by
    #1

    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
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 Apr 2014, 21:51 last edited by
      #2

      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
      0
      • B Offline
        B Offline
        BlackStar
        wrote on 29 Apr 2014, 22:00 last edited by
        #3

        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
        0
        • C Offline
          C Offline
          cholokid
          wrote on 3 May 2014, 20:31 last edited by
          #4

          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
          0
          • J Offline
            J Offline
            JvdGlind
            wrote on 5 May 2014, 08:04 last edited by
            #5

            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
            0

            1/5

            29 Apr 2014, 20:38

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved