[SOLVED] Simple SetText example



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


  • Lifetime Qt Champion

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



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



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



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



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



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



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



  • 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");
    }
    @



  • 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");
    }
    @


  • Lifetime Qt Champion

    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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.