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