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. My first own Qt app
Forum Updated to NodeBB v4.3 + New Features

My first own Qt app

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 3.5k Views 3 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by
    #1

    Hi all,

    Please look at these files. These are the files of an app that I want to develop and extent it gradually. Now is the first steps.

    This is employee.h:

    #ifndef EMPLOYEE_H
    #define EMPLOYEE_H
    
    #include <QDialog>
    
    class QLabel;
    class QPushButton;
    class QLineEdit;
    
    class Employee : public QDialog
    {
        Q_OBJECT
    public:
        Employee(QWidget* parent = 0);
    private:
        int mySalary;
        QLabel* label;
        QLineEdit* lineEdit;
        QPushButton* show_sl;
        QPushButton* quit;
    };
    
    #endif // EMPLOYEE_H
    

    And this is employee.cpp:

    #include <QtWidgets>
    #include "employee.h"
    
    Employee::Employee(QWidget* parent)
             :QDialog(parent)
    {
         mySalary = 15;
         
         label = new QLabel(tr("mySalary"));
         lineEdit = new QLineEdit;
         show_sl = new QPushButton(tr("Show"));
         quit = new QPushButton(tr("Close"));
    
    
         QHBoxLayout* layout1 =new QHBoxLayout;
         layout1 -> addWidget(label);
         layout1 -> addWidget(lineEdit);
    
         QHBoxLayout* layout2 =new QHBoxLayout;
         layout2 -> addWidget(show_sl);
         layout2 -> addWidget(quit);
    
         QVBoxLayout* vlayout = new QVBoxLayout;
         vlayout -> addLayout(layout1);
         vlayout -> addLayout(layout2);
    
         setLayout(vlayout);
    }
    

    And this is main.cpp:

    #include <QApplication>
    #include "employee.h"
    
    int main(int argc , char* argv[])
    {
        QApplication app(argc, argv);
        Employee* employee = new Employee;
        employee -> show();
        return app.exec();
    }
    

    It runs successfully. Now my question is that, what widgets/functions/slots/signal do I need to add to the code so that when I click the Show button, it shows the value of my_Salary which is 15 in the lineEdit please?
    As I said, I'm going to make the app bigger step-by-step.

    raven-worxR 1 Reply Last reply
    0
    • tomyT tomy

      Hi all,

      Please look at these files. These are the files of an app that I want to develop and extent it gradually. Now is the first steps.

      This is employee.h:

      #ifndef EMPLOYEE_H
      #define EMPLOYEE_H
      
      #include <QDialog>
      
      class QLabel;
      class QPushButton;
      class QLineEdit;
      
      class Employee : public QDialog
      {
          Q_OBJECT
      public:
          Employee(QWidget* parent = 0);
      private:
          int mySalary;
          QLabel* label;
          QLineEdit* lineEdit;
          QPushButton* show_sl;
          QPushButton* quit;
      };
      
      #endif // EMPLOYEE_H
      

      And this is employee.cpp:

      #include <QtWidgets>
      #include "employee.h"
      
      Employee::Employee(QWidget* parent)
               :QDialog(parent)
      {
           mySalary = 15;
           
           label = new QLabel(tr("mySalary"));
           lineEdit = new QLineEdit;
           show_sl = new QPushButton(tr("Show"));
           quit = new QPushButton(tr("Close"));
      
      
           QHBoxLayout* layout1 =new QHBoxLayout;
           layout1 -> addWidget(label);
           layout1 -> addWidget(lineEdit);
      
           QHBoxLayout* layout2 =new QHBoxLayout;
           layout2 -> addWidget(show_sl);
           layout2 -> addWidget(quit);
      
           QVBoxLayout* vlayout = new QVBoxLayout;
           vlayout -> addLayout(layout1);
           vlayout -> addLayout(layout2);
      
           setLayout(vlayout);
      }
      

      And this is main.cpp:

      #include <QApplication>
      #include "employee.h"
      
      int main(int argc , char* argv[])
      {
          QApplication app(argc, argv);
          Employee* employee = new Employee;
          employee -> show();
          return app.exec();
      }
      

      It runs successfully. Now my question is that, what widgets/functions/slots/signal do I need to add to the code so that when I click the Show button, it shows the value of my_Salary which is 15 in the lineEdit please?
      As I said, I'm going to make the app bigger step-by-step.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @tomy
      you could add a method to set your values before you call show()

      Very basic example:

      class Employee : public QDialog
      {
          Q_OBJECT
      public:
          Employee(QWidget* parent = 0);
      
          void setSalary(int value) {
                  mySalary = value;
                  lineEdit->setText( QString::number(value) );
          }
          ...
      };
      
      employee = new Employee;
      employee->setSalary(15);
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      tomyT 1 Reply Last reply
      3
      • raven-worxR raven-worx

        @tomy
        you could add a method to set your values before you call show()

        Very basic example:

        class Employee : public QDialog
        {
            Q_OBJECT
        public:
            Employee(QWidget* parent = 0);
        
            void setSalary(int value) {
                    mySalary = value;
                    lineEdit->setText( QString::number(value) );
            }
            ...
        };
        
        employee = new Employee;
        employee->setSalary(15);
        
        tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by tomy
        #3

        @raven-worx
        Thank you for your reply but I want to use the widgets in my code, and as I said, by clicking the button named Show , its value to be shown in lineEdit .
        Show is in line show_sl = new QPushButton(tr("Show")); in employee.cpp .

        I later develop the code and add some function to be able to set the salary.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          at the bottom of the constructor add
          connect(show_sl,&QPushButton::clicked,[this]()->void{my_Salary->setText( QString::number(mySalary ));});

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • tomyT Offline
            tomyT Offline
            tomy
            wrote on last edited by VRonin
            #5

            What are these??!!
            I'm new in Qt and you write things like &QPushButton::clicked,[this]()->void .... I don't understand these, sorry.

            I think I need a signal and a slot like this:
            I added these to the employee.h

            signals:
                void show_salary();
            private slots:
                void show_salary_in_lineEdit();
            

            And a connect and the definition of show_salary_in_lineEdit() to the employee.cpp this way

            connect(show_sl, SIGNAL(show_salary()), lineEdit,
                        SLOT(show_salary_in_lineEdit()));
            .
            .
            .
            
            void Employee::show_salary_in_lineEdit()
            {
               lineEdit -> setText(QString::number(mySalary));
            }
            

            Now the code runs but clicking the Show button doesn't do anything!

            VRoninV 1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              Hi
              When you use SIGNAL and SLOT macros, you can check if connect works with
              qDebug() << "conn:" << connect ( xxx );
              should return true;

              tomyT 1 Reply Last reply
              1
              • tomyT tomy

                What are these??!!
                I'm new in Qt and you write things like &QPushButton::clicked,[this]()->void .... I don't understand these, sorry.

                I think I need a signal and a slot like this:
                I added these to the employee.h

                signals:
                    void show_salary();
                private slots:
                    void show_salary_in_lineEdit();
                

                And a connect and the definition of show_salary_in_lineEdit() to the employee.cpp this way

                connect(show_sl, SIGNAL(show_salary()), lineEdit,
                            SLOT(show_salary_in_lineEdit()));
                .
                .
                .
                
                void Employee::show_salary_in_lineEdit()
                {
                   lineEdit -> setText(QString::number(mySalary));
                }
                

                Now the code runs but clicking the Show button doesn't do anything!

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @tomy said in My first own Qt app:

                What are these??!!

                That's the Qt5 connection syntax with a lambda

                I think I need a signal and a slot

                You just need a slot

                connect(show_sl, SIGNAL(show_salary())

                show_sl has no signal show_salary hence @mrjj remark warning you that the connect won't work


                this is correct

                void Employee::show_salary_in_lineEdit()
                {
                   lineEdit -> setText(QString::number(mySalary));
                }
                

                now the connect:

                connect(
                show_sl //who sends the signal
                , &QPushButton::clicked // what signal do you want to react to (in this case when the button is clicked)
                ,this // who should catch the signal
                ,&Employee::show_salary_in_lineEdit //what slot should be called
                );
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2
                • M Offline
                  M Offline
                  mlago
                  wrote on last edited by mlago
                  #8

                  Hi,
                  Your connect code not is correc, what you need is connect de Button signal clicked with your Show function,Something like that:

                   connect(show_sl, SIGNAL(clicked(bool)), this, SLOT(show_salary_in_lineEdit()));
                  

                  How to Use QPushButton

                  If you allow me a question, would not it be easier for you to use the graphic editor instead of creating the graphic objects by code?

                  EDIT: I am sorry, I was responding when the @VRonin had already done so.

                  C tomyT 2 Replies Last reply
                  2
                  • M mlago

                    Hi,
                    Your connect code not is correc, what you need is connect de Button signal clicked with your Show function,Something like that:

                     connect(show_sl, SIGNAL(clicked(bool)), this, SLOT(show_salary_in_lineEdit()));
                    

                    How to Use QPushButton

                    If you allow me a question, would not it be easier for you to use the graphic editor instead of creating the graphic objects by code?

                    EDIT: I am sorry, I was responding when the @VRonin had already done so.

                    C Offline
                    C Offline
                    calebhalvy
                    wrote on last edited by
                    #9

                    @mlago,
                    @VRonin is using the new Qt5 Signal/Slot connection syntax, as he linked to here.

                    1 Reply Last reply
                    2
                    • M mlago

                      Hi,
                      Your connect code not is correc, what you need is connect de Button signal clicked with your Show function,Something like that:

                       connect(show_sl, SIGNAL(clicked(bool)), this, SLOT(show_salary_in_lineEdit()));
                      

                      How to Use QPushButton

                      If you allow me a question, would not it be easier for you to use the graphic editor instead of creating the graphic objects by code?

                      EDIT: I am sorry, I was responding when the @VRonin had already done so.

                      tomyT Offline
                      tomyT Offline
                      tomy
                      wrote on last edited by
                      #10

                      @mlago said in My first own Qt app:

                      Hi,
                      Your connect code not is correc, what you need is connect de Button signal clicked with your Show function,Something like that:

                       connect(show_sl, SIGNAL(clicked(bool)), this, SLOT(show_salary_in_lineEdit()));
                      

                      How to Use QPushButton

                      Thank you. I changed the connect the way you showed and the code ran as expected. Thanks again.

                      If you allow me a question, would not it be easier for you to use the graphic editor instead of creating the graphic objects by code?

                      I'm learning Qt using a book and it walks me this way. I will be taught the Designer mode shortly. :)

                      EDIT: I am sorry, I was responding when the @VRonin had already done so.

                      1 Reply Last reply
                      0
                      • mrjjM mrjj

                        Hi
                        When you use SIGNAL and SLOT macros, you can check if connect works with
                        qDebug() << "conn:" << connect ( xxx );
                        should return true;

                        tomyT Offline
                        tomyT Offline
                        tomy
                        wrote on last edited by tomy
                        #11

                        @mrjj said in My first own Qt app:

                        Hi
                        When you use SIGNAL and SLOT macros, you can check if connect works with
                        qDebug() << "conn:" << connect ( xxx );
                        should return true;

                        Thanks. Although the code runs fine now, let me ask how to use that code for example in my code? (instructions)

                        mrjjM 1 Reply Last reply
                        0
                        • tomyT tomy

                          @mrjj said in My first own Qt app:

                          Hi
                          When you use SIGNAL and SLOT macros, you can check if connect works with
                          qDebug() << "conn:" << connect ( xxx );
                          should return true;

                          Thanks. Although the code runs fine now, let me ask how to use that code for example in my code? (instructions)

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @tomy
                          Hi
                          You can use qDebug() for many nice things :)

                          1: #include <QDebug>

                          2: use it

                          qDebug() << "text" << variable << "more text" << some_other_variable ;

                          1 Reply Last reply
                          2
                          • tomyT Offline
                            tomyT Offline
                            tomy
                            wrote on last edited by
                            #13

                            Thank you.

                            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