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. [SOLVED] Simple SetText example
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Simple SetText example

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 5.3k 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.
  • D Offline
    D Offline
    DeanQt
    wrote on last edited by
    #1

    I have two line edit widgets. Upon pressing enter on the one, I want the other line edit to update its value:

    I tried the following:
    @ connect (ui ->lineEdit, SIGNAL (returnPressed()), ui->lineEdit_2, SLOT (setText("Hello")));@

    But it does not work?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      No it won't, you can't pass values in the connect statement.

      Have look at this "chapter":http://qt-project.org/doc/qt-4.8/signalsandslots.html of the documentation to see how it works

      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
      • D Offline
        D Offline
        DeanQt
        wrote on last edited by
        #3

        [quote author="SGaist" date="1388266058"]Hi,

        No it won't, you can't pass values in the connect statement.

        Have look at this "chapter":http://qt-project.org/doc/qt-4.8/signalsandslots.html of the documentation to see how it works[/quote]

        How then do I update the lineEdit_2 everytime I change lineEdit if I cant use the connect functionality?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          panosk
          wrote on last edited by
          #4

          Hi,

          You can use a signal/slot connection. Just create your own slot:
          @
          private slots:
          void mySlot() {
          //If you like, do some checks for empty string, etc
          ui->lineEdit_2->setText("Hello");
          }
          @

          Then do the connection like this:

          @
          connect (ui ->lineEdit, SIGNAL (returnPressed()), this, SLOT (mySlot()));
          @

          EDIT: I complicated your problem with no reason :-)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DeanQt
            wrote on last edited by
            #5

            [quote author="panosk" date="1388334136"]Hi,

            You can use a signal/slot connection. Just create your own slot:
            @
            private slots:
            void mySlot() {
            //If you like, do some checks for empty string, etc
            ui->lineEdit_2->setText("Hello");
            }
            @

            Then do the connection like this:

            @
            connect (ui ->lineEdit, SIGNAL (returnPressed()), this, SLOT (mySlot()));
            @

            EDIT: I complicated your problem with no reason :-)[/quote]

            I get the error "expected qualifier-id before 'private' when I make a private slot in the source code.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Seba84
              wrote on last edited by
              #6

              Can we see the complete code?

              Are you sure you are not missing something in the line before the private slot declaration? For example parenthesis, semicolon or class with no header?
              Did you include the Q_OBJECT macro inside the class?

              1 Reply Last reply
              0
              • P Offline
                P Offline
                panosk
                wrote on last edited by
                #7

                The
                @
                private slots:
                @

                part belongs to the header, not the .cpp file. If you already have it in the header, then you probably have a typo (missing bracket, parenthesis, etc).

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DeanQt
                  wrote on last edited by
                  #8

                  [quote author="panosk" date="1388347381"]The
                  @
                  private slots:
                  @

                  part belongs to the header, not the .cpp file. If you already have it in the header, then you probably have a typo (missing bracket, parenthesis, etc). [/quote]

                  Same error when including it in the header file:

                  Header:

                  @#ifndef MAINWINDOW_H
                  #define MAINWINDOW_H

                  #include <QMainWindow>
                  #include <QTableWidget>
                  #include "Equations.h"

                  namespace Ui {
                  class MainWindow;
                  }

                  class MainWindow : public QMainWindow
                  {
                  Q_OBJECT

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

                  private:
                  Ui::MainWindow *ui;
                  };

                  private slots:

                  #endif // MAINWINDOW_H
                  @

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    panosk
                    wrote on last edited by
                    #9

                    It's out of the class definition and you forgot the actual slot function.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Seba84
                      wrote on last edited by
                      #10

                      :) Try putting private slots inside the class definition. Like this:

                      @class MainWindow : public QMainWindow
                      {
                      Q_OBJECT

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

                      private:
                      Ui::MainWindow *ui;

                      private slots:
                      void mySlot() {
                      //If you like, do some checks for empty string, etc
                      ui->lineEdit_2->setText("Hello");
                      }

                      }; @

                      EDIT: panosk, we are really syncronized... :) :)

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        DeanQt
                        wrote on last edited by
                        #11

                        This time I get the error
                        invalid use of incomplete type 'class Ui::MainWindow'
                        and
                        forward declaration of 'class Ui::MainWindow'

                        on lines 9 and 26 respectively.

                        @#ifndef MAINWINDOW_H
                        #define MAINWINDOW_H

                        #include <QMainWindow>
                        #include <QTableWidget>
                        #include "Equations.h"

                        namespace Ui {
                        class MainWindow;
                        }

                        class MainWindow : public QMainWindow
                        {
                        Q_OBJECT

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

                        private:
                        Ui::MainWindow *ui;

                        private slots:
                        void mySlot() {
                        //If you like, do some checks for empty string, etc
                        ui->lineEdit_2->setText("Hello");
                        }
                        };

                        #endif // MAINWINDOW_H
                        @

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          panosk
                          wrote on last edited by
                          #12

                          Use only the definition in the header and put the implementation in your source file.

                          This goes in the header:
                          @
                          private slots:
                          void mySlot();
                          @

                          And this goes to the .cpp file:

                          @
                          void MainWindow::mySlot() {
                          ui->lineEdit_2->setText("Hello");
                          }
                          @

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            DeanQt
                            wrote on last edited by
                            #13

                            Great, it works, thanks.

                            Question though. On line 16, why must I use "this" as the third connect parameter instead of "lineEdit_2"?

                            @#include "mainwindow.h"
                            #include "ui_mainwindow.h"
                            #include <QTableWidget>
                            #include "stdio.h"

                            #include <Equations.h>

                            using namespace std;

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

                            connect (ui ->lineEdit, SIGNAL (editingFinished()), this, SLOT (mySlot()));
                            

                            }

                            MainWindow::~MainWindow()
                            {

                            delete ui;
                            

                            }

                            void MainWindow::mySlot() {
                            ui->lineEdit_2->setText("Hello");
                            }
                            @

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              You don't need it in this case, it's just using another overload of the connect function. All version of the connect function where not present in QObject at the same time, so older user are generally using the form with 4 arguments

                              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
                              • D Offline
                                D Offline
                                DeanQt
                                wrote on last edited by
                                #15

                                If I replace "this" with ui->linEdit_2, the slot doesnt trigger. How does one explain this?

                                1 Reply Last reply
                                0
                                • P Offline
                                  P Offline
                                  panosk
                                  wrote on last edited by
                                  #16

                                  The slot is a member of your MainWindow class and it's not a member of QLineEdit's class (lineEdit_2 is a QLineEdit).

                                  The connect statement connects a signal from an object A to a slot of an object B. In your case, you connect the signal returnPressed() which indeed is a member of a QLineEdit object (your lineEdit) and the slot mySlot() which indeed is a member of your current (this) MainWindow object.

                                  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