Results 1 to 5 of 5

Thread: Qt 4.8: How to use setText() of QLineEdit.

  1. #1
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Qt 4.8: How to use setText() of QLineEdit.

    I recently migrated from Qt 3 to Qt4.8 ( I know huge jump.) Was restricted to work on qt3 before. Anyway, I was trying out this particular program, where I am creating two QLineEdit boxes and trying to to get the text of l1 to l2 (l1 and l2 bring the names of the QLineEdit respectively.) For this I was created the signal and slots but when I compile I get this error "Undefined reference to setText()". I was using the signal textChanged() of l1 and slot of l2 - setText(). Think anyone can help me on this?



    I think I am just missing something. Got gambled with all the qt3 and 4 concepts. Please pardon my poor code and concepts. Patience would be appriciated. Thanks.
    try1.cpp
    Qt Code:
    1. #include<QtGui>
    2. #include<QDialog>
    3. #include<QLineEdit>
    4. #include<QLabel>
    5. #include<QPushButton>
    6. #include "try1.h"
    7. #include "ui_try1.h"
    8.  
    9. try1::try1(QWidget *parent):QDialog(parent), ui(new Ui::Dialog)
    10. {
    11. //ui->setupUi(this);
    12. connect(ui->l1,SIGNAL(textChanged(const QString &)),ui->l2,SLOT(setText(const QString &)) );
    13. //connect(ui->Quit,SIGNAL(clicked()),this,SLOT(close()));
    14. }
    To copy to clipboard, switch view to plain text mode 
    try1.h
    Qt Code:
    1. #ifndef TRY1_H
    2. #define TRY1_H
    3. #include "ui_try1.h"
    4. #include<QDialog>
    5. #include<QLineEdit>
    6. #include<QLabel>
    7. #include<QPushButton>
    8. //class ;
    9. //class QLineEdit;
    10. //class QPushButton;
    11.  
    12. namespace Ui
    13. {
    14. class Dialog;
    15. }
    16.  
    17.  
    18. class try1:public QDialog
    19. {
    20. Q_OBJECT
    21. public:
    22. try1(QWidget *parent=0);
    23. private:
    24. Ui::Dialog *ui;
    25. signals:
    26. void textChanged(const QString &text);
    27. // void clicked();
    28. private slots:
    29. void setText(const QString &text);
    30. //void close();
    31. private:
    32. QLabel *l1;
    33. QLabel *l2;
    34. QLineEdit *t1;
    35. QLineEdit *t2;
    36. QPushButton *Quit;
    37. };
    38.  
    39. #endif
    To copy to clipboard, switch view to plain text mode 

    main.cpp (init)
    Qt Code:
    1. #include<QtGui>
    2. #include<QApplication>
    3. #include<QDialog>
    4. #include "try1.h"
    5.  
    6. int main(int argc,char *argv[])
    7. {
    8. QApplication app(argc,argv);
    9. try1 d;
    10. d.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Also attaching the zip file of the project. And again really sorry for messy code.
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.8: How to use setText() of QLineEdit.

    Hi,
    Please use this code.
    try1.cpp
    Qt Code:
    1. #include "try1.h"
    2.  
    3. try1::try1(QWidget *parent):QDialog(parent), ui(new Ui::Dialog)
    4. {
    5. ui->setupUi(this);
    6. connect(ui->l1, SIGNAL(textChanged(const QString &)), ui->l2,SLOT(settext(const QString &)) );
    7. connect(this, SIGNAL(settText(QString)), ui->l2, SLOT(setText(QString)));
    8. }
    9.  
    10. void try1::settext(const QString a)
    11. {
    12. emit settText(a);
    13. }
    To copy to clipboard, switch view to plain text mode 

    try1.h
    Qt Code:
    1. #ifndef TRY1_H
    2. #define TRY1_H
    3. #include "ui_try1.h"
    4. #include<QDialog>
    5. #include<QLineEdit>
    6. #include<QLabel>
    7. #include<QPushButton>
    8.  
    9.  
    10. namespace Ui
    11. {
    12. class Dialog;
    13. }
    14.  
    15.  
    16. class try1:public QDialog
    17. {
    18. Q_OBJECT
    19. public:
    20. try1(QWidget *parent=0);
    21.  
    22. private:
    23. Ui::Dialog *ui;
    24.  
    25. signals:
    26. void settText(QString);
    27.  
    28. private slots:
    29. void settext(const QString a);
    30.  
    31. private:
    32. QLabel *l1;
    33. QLabel *l2;
    34. QLineEdit *t1;
    35. QLineEdit *t2;
    36. QPushButton *Quit;
    37. };
    38.  
    39. #endif
    To copy to clipboard, switch view to plain text mode 

    Best regards,

    Myta

  3. #3
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Qt 4.8: How to use setText() of QLineEdit.

    Thanks for replying Myta. But I dont understand what you are trying to do a bit more explanation could have helped.

    From what I guessed - you are creating a seperate settext ( with a small t?) or is it a typo? Why the need to create a seperate signal?

    Quote Originally Posted by myta212 View Post
    Hi,
    Please use this code.
    try1.cpp
    Qt Code:
    1. #include "try1.h"
    2.  
    3. try1::try1(QWidget *parent):QDialog(parent), ui(new Ui::Dialog)
    4. {
    5. ui->setupUi(this);
    6. connect(ui->l1, SIGNAL(textChanged(const QString &)), ui->l2,SLOT(settext(const QString &)) );
    7. connect(this, SIGNAL(settText(QString)), ui->l2, SLOT(setText(QString)));
    8. }
    9.  
    10. void try1::settext(const QString a)
    11. {
    12. emit settText(a);
    13. }
    To copy to clipboard, switch view to plain text mode 

    try1.h
    Qt Code:
    1. #ifndef TRY1_H
    2. #define TRY1_H
    3. #include "ui_try1.h"
    4. #include<QDialog>
    5. #include<QLineEdit>
    6. #include<QLabel>
    7. #include<QPushButton>
    8.  
    9.  
    10. namespace Ui
    11. {
    12. class Dialog;
    13. }
    14.  
    15.  
    16. class try1:public QDialog
    17. {
    18. Q_OBJECT
    19. public:
    20. try1(QWidget *parent=0);
    21.  
    22. private:
    23. Ui::Dialog *ui;
    24.  
    25. signals:
    26. void settText(QString);
    27.  
    28. private slots:
    29. void settext(const QString a);
    30.  
    31. private:
    32. QLabel *l1;
    33. QLabel *l2;
    34. QLineEdit *t1;
    35. QLineEdit *t2;
    36. QPushButton *Quit;
    37. };
    38.  
    39. #endif
    To copy to clipboard, switch view to plain text mode 

    Best regards,

    Myta

  4. #4
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.8: How to use setText() of QLineEdit.

    Hi,
    I am try to separate signal and slot from QLineEdit 1 and QLineEdit2.
    This is more simple than above code. You dont need to add signal/slot setText in your header file. Because this function exist in QLineEdit function.
    Your code is right, but You must remove const variable in your cpp code. Please check this code :

    try1.cpp
    Qt Code:
    1. #include "try1.h"
    2.  
    3. try1::try1(QWidget *parent):QDialog(parent), ui(new Ui::Dialog)
    4. {
    5. ui->setupUi(this);
    6. connect(ui->l1, SIGNAL(textChanged(QString)), ui->l2, SLOT(setText(QString)) );
    7. }
    To copy to clipboard, switch view to plain text mode 

    try1.h
    Qt Code:
    1. #ifndef TRY1_H
    2. #define TRY1_H
    3. #include "ui_try1.h"
    4. #include<QDialog>
    5. #include<QLineEdit>
    6. #include<QLabel>
    7. #include<QPushButton>
    8.  
    9.  
    10. namespace Ui
    11. {
    12. class Dialog;
    13. }
    14.  
    15.  
    16. class try1:public QDialog
    17. {
    18. Q_OBJECT
    19. public:
    20. try1(QWidget *parent=0);
    21.  
    22. private:
    23. Ui::Dialog *ui;
    24.  
    25. private:
    26. QLabel *l1;
    27. QLabel *l2;
    28. QLineEdit *t1;
    29. QLineEdit *t2;
    30. QPushButton *Quit;
    31. };
    32.  
    33. #endif
    To copy to clipboard, switch view to plain text mode 

    Best regards,

    Myta

  5. The following user says thank you to myta212 for this useful post:

    amitahire (26th June 2012)

  6. #5
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Qt 4.8: How to use setText() of QLineEdit.

    Thanks a lot Myta. Its works. So if the signal and slots are predefined you dont have to mention them in the .h file? As soon as I removed them it was working.

    Thanks again.

    Quote Originally Posted by myta212 View Post
    Hi,
    I am try to separate signal and slot from QLineEdit 1 and QLineEdit2.
    This is more simple than above code. You dont need to add signal/slot setText in your header file. Because this function exist in QLineEdit function.
    Your code is right, but You must remove const variable in your cpp code. Please check this code :

    try1.cpp
    Qt Code:
    1. #include "try1.h"
    2.  
    3. try1::try1(QWidget *parent):QDialog(parent), ui(new Ui::Dialog)
    4. {
    5. ui->setupUi(this);
    6. connect(ui->l1, SIGNAL(textChanged(QString)), ui->l2, SLOT(setText(QString)) );
    7. }
    To copy to clipboard, switch view to plain text mode 

    try1.h
    Qt Code:
    1. #ifndef TRY1_H
    2. #define TRY1_H
    3. #include "ui_try1.h"
    4. #include<QDialog>
    5. #include<QLineEdit>
    6. #include<QLabel>
    7. #include<QPushButton>
    8.  
    9.  
    10. namespace Ui
    11. {
    12. class Dialog;
    13. }
    14.  
    15.  
    16. class try1:public QDialog
    17. {
    18. Q_OBJECT
    19. public:
    20. try1(QWidget *parent=0);
    21.  
    22. private:
    23. Ui::Dialog *ui;
    24.  
    25. private:
    26. QLabel *l1;
    27. QLabel *l2;
    28. QLineEdit *t1;
    29. QLineEdit *t2;
    30. QPushButton *Quit;
    31. };
    32.  
    33. #endif
    To copy to clipboard, switch view to plain text mode 

    Best regards,

    Myta

Similar Threads

  1. Replies: 6
    Last Post: 5th December 2020, 00:11
  2. Replies: 1
    Last Post: 12th October 2010, 23:20
  3. settext
    By seltra in forum Newbie
    Replies: 4
    Last Post: 3rd October 2010, 15:52
  4. Problem with QLineEdit and setText
    By Elmo23x in forum Qt Programming
    Replies: 8
    Last Post: 12th April 2007, 13:35
  5. Replies: 1
    Last Post: 26th November 2006, 10:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.