Results 1 to 17 of 17

Thread: QLineEdit && QLabel

  1. #1
    Join Date
    Nov 2006
    Posts
    96

    Default QLineEdit && QLabel

    Hey...

    So far I've created QLabel + a couple of QLineEdit's and now I'm curious: How can I get the user-input ?

    Example: Let's say there are 2 QLineEdit's and user enters 14 in the first one and 16 in the second one. I wish to get both numbers and display 14+16=30 on the screen by QLabel. How can this be done?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit && QLabel

    QLineEdit emits signals when user edits it and you can connect to one of them, for example QLineEdit::textChanged() or QLineEdit::editingFinished().

    You will know how to do it, if you go through this tutorial: http://doc.trolltech.com/4.1/tutorial-t1.html

  3. #3
    Join Date
    Nov 2006
    Posts
    96

    Default Re: QLineEdit && QLabel

    Look: here's the code:

    #include <qapplication.h>
    #include <qwidget.h>
    #include <qlineedit.h>
    #include <qlabel.h>


    class mojClass : public QWidget
    {
    public:
    mojClass();
    private:
    QLineEdit *lineedit1;
    QLabel *label1;
    QLabel *label2;
    QLabel *label3;
    };

    mojClass::mojClass()
    {
    setGeometry(100,100,300,200);

    lineedit1 = new QLineEdit(this);
    lineedit1->setGeometry(110,10,50,20);
    lineedit1->setEchoMode(QLineEdit::Normal);
    lineedit1->setMaxLength(6);

    label1 = new QLabel(this);
    label1->setGeometry(10,10,100,20);
    label1->setText("Enter something: ");

    label2 = new QLabel(this);
    label2->setGeometry(10,40,100,20);
    label2->setText("You've entered: ");

    label3 = new QLabel(this);
    label3->setGeometry(120,40,100,20);

    connect(lineedit1,SIGNAL(textChanged()),label3,SLO T(setText()));
    }

    int main(int argc, char **argv)
    {
    QApplication a(argc,argv);
    mojClass objekt;
    a.setMainWidget(&objekt);
    objekt.show();
    a.exec();
    }

    And here's the error:

    QObject::connect: No such signal QLineEdit::textChanged()
    QObject::connect: (sender name: 'unnamed')
    QObject::connect: (receiver name: 'unnamed')
    What am I missing?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit && QLabel

    You have to add parameter types, like this:
    Qt Code:
    1. connect( lineedit1, SIGNAL( textChanged( const QString& ) ), label3, SLOT( setText( const QString& ) ) );
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2006
    Posts
    96

    Default Re: QLineEdit && QLabel

    Thank you very much, it works

    I have some other problems. How can I add QLineEdit(this); into a loop. So there would be 1 QLineEdit displayed where the userwould put in a number 25 (just an example) and then 25 more QLineEdit's would be displayed?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit && QLabel

    Quote Originally Posted by eleanor View Post
    I have some other problems. How can I add QLineEdit(this); into a loop. So there would be 1 QLineEdit displayed where the userwould put in a number 25 (just an example) and then 25 more QLineEdit's would be displayed?
    Read this: http://doc.trolltech.com/4.1/tutorial-t6.html

    You probably want to use QVBoxLayout instead of QGridLayout (and don't forget to invoke show() on each widget, if you create them after their parent was shown). You can use QList to store pointers to line edits, so that you can delete them when the user wants to reduce their number.

  7. #7
    Join Date
    Nov 2006
    Posts
    96

    Default Re: QLineEdit && QLabel

    How can I accept the number from QLineEdit and then multiply it by 4 and then output it with QLabel?

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QLineEdit && QLabel

    Quote Originally Posted by eleanor View Post
    How can I accept the number from QLineEdit and then multiply it by 4 and then output it with QLabel?
    To restrict the user to input only numbers, you can use a QIntValidator:
    Qt Code:
    1. QValidator *validator = new QIntValidator(100, 999, this);
    2. QLineEdit *edit = new QLineEdit(this);
    3. // the edit lineedit will only accept integers between 100 and 999
    4. edit->setValidator(validator);
    To copy to clipboard, switch view to plain text mode 
    or you can use input mask:
    Qt Code:
    1. QLineEdit *edit = new QLineEdit(this);
    2. // the edit lineedit will only accept 0-3 digit chars
    3. edit->setInputMask("000");
    To copy to clipboard, switch view to plain text mode 

    For multiplying, you need a custom slot:
    Qt Code:
    1. // class declaration
    2. class mojClass : public ...
    3. {
    4. ...
    5. private slots:
    6. void customSlot(const QString& input);
    7. ...
    8. };
    9.  
    10. // class implementation
    11. mojClass::mojClass()
    12. {
    13. ...
    14. connect(lineedit1,SIGNAL(textChanged(const QString&)),this,SLO T(customSlot(const QString&)));
    15. }
    16.  
    17. // the custom slot
    18. void mojClass::customSlot(const QString& input)
    19. {
    20. int num = 4 * input.toInt();
    21. label3->setNum(num);
    22. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. #9
    Join Date
    Nov 2006
    Posts
    96

    Default Re: QLineEdit && QLabel

    This does not work:

    Qt Code:
    1. #include <qapplication.h>
    2. #include <qwidget.h>
    3. #include <qlineedit.h>
    4. #include <qlabel.h>
    5. #include <qlayout.h>
    6. #include <qvalidator.h>
    7.  
    8.  
    9. class mojClass : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. mojClass();
    14. private:
    15. QLineEdit *lineedit1;
    16. QLabel *label1;
    17. QLabel *label2;
    18. QLabel *label3;
    19. QValidator *validator;
    20. private slots:
    21. void customSlot(const QString &input);
    22.  
    23. };
    24.  
    25. void mojClass::customSlot(const QString &input)
    26. {
    27. int num = 4 * input.toInt();
    28. label3->setNum(num);
    29. }
    30.  
    31.  
    32. mojClass::mojClass()
    33. {
    34. setGeometry(100,100,300,200);
    35.  
    36. validator = new QIntValidator(100,99999,this);
    37. lineedit1 = new QLineEdit(this);
    38. lineedit1->setGeometry(110,10,50,20);
    39. lineedit1->setEchoMode(QLineEdit::Normal);
    40. lineedit1->setMaxLength(6);
    41. lineedit1->setValidator(validator);
    42.  
    43. label1 = new QLabel(this);
    44. label1->setGeometry(10,10,100,20);
    45. label1->setText("Enter something: ");
    46.  
    47. label2 = new QLabel(this);
    48. label2->setGeometry(10,40,100,20);
    49. label2->setText("You've entered: ");
    50.  
    51. label3 = new QLabel(this);
    52. label3->setGeometry(120,40,100,20);
    53.  
    54. connect(lineedit1,SIGNAL(textChanged(const QString &)),this,SLOT(customSlot(const QString &)));
    55. }
    56.  
    57. int main(int argc, char **argv)
    58. {
    59. QApplication a(argc,argv);
    60. mojClass objekt;
    61. a.setMainWidget(&objekt);
    62. objekt.show();
    63. a.exec();
    64. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 5th November 2006 at 23:23. Reason: changed [quote] to [code]

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit && QLabel

    To make it work you have to add Q_OBJECT macro like this:
    Qt Code:
    1. class mojClass : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. ...
    To copy to clipboard, switch view to plain text mode 
    Each class that defines new slots or signals must contain Q_OBJECT macro. Also remember to rerun qmake if you add that macro somewhere.

    If you have placed everything in a single file, say somefile.cpp, you have to add #include "somefile.moc" at the end and rerun qmake. This isn't necessary if you have placed class definition in a header file.

  11. #11
    Join Date
    Nov 2006
    Posts
    96

    Default Re: QLineEdit && QLabel

    Aha thank you.

    If I want to use 3 files, is this correct:

    poskus.h
    Qt Code:
    1. #idndef POSKUS_H
    2. #define POSKUS_H
    3.  
    4. #include <qwidget.h>
    5.  
    6. class mojClass : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. mojClass();
    11. private:
    12. QLineEdit *lineedit1;
    13. QLabel *label1;
    14. QLabel *label2;
    15. QLabel *label3;
    16. QValidator *validator;
    17. private slots:
    18. void customSlot(const QString &input);
    19.  
    20. };
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 


    poskus.cpp
    Qt Code:
    1. #include "poskus.h"
    2. #include <qlineedit.h>
    3. #include <qlabel.h>
    4. #include <qlayout.h>
    5. #include <qvalidator.h>
    6.  
    7. void mojClass::customSlot(const QString &input)
    8. {
    9. int num = 4 * input.toInt();
    10. label3->setNum(num);
    11. }
    12.  
    13.  
    14. mojClass::mojClass()
    15. {
    16. setGeometry(100,100,300,200);
    17.  
    18. validator = new QIntValidator(100,99999,this);
    19. lineedit1 = new QLineEdit(this);
    20. lineedit1->setGeometry(110,10,50,20);
    21. lineedit1->setEchoMode(QLineEdit::Normal);
    22. lineedit1->setMaxLength(6);
    23. lineedit1->setValidator(validator);
    24.  
    25. label1 = new QLabel(this);
    26. label1->setGeometry(10,10,100,20);
    27. label1->setText("Enter something: ");
    28.  
    29. label2 = new QLabel(this);
    30. label2->setGeometry(10,40,100,20);
    31. label2->setText("You've entered: ");
    32.  
    33. label3 = new QLabel(this);
    34. label3->setGeometry(120,40,100,20);
    35.  
    36. connect(lineedit1,SIGNAL(textChanged(const QString &)),this,SLOT(customSlot(const QString &)));
    37. }
    To copy to clipboard, switch view to plain text mode 


    main.cpp
    Qt Code:
    1. #include <qapplication.h>
    2.  
    3. #include "poskus.h"
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication a(argc,argv);
    8. mojClass objekt;
    9. a.setMainWidget(&objekt);
    10. objekt.show();
    11. a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit && QLabel

    Quote Originally Posted by eleanor View Post
    If I want to use 3 files, is this correct:
    It looks OK.

  13. #13
    Join Date
    Nov 2006
    Posts
    96

    Default Re: QLineEdit && QLabel

    Hey.

    How can I write this line:
    Qt Code:
    1. connect(lineedit1,SIGNAL(textChanged(const QString &)),this,SLOT(customSlot(const QString &)));
    To copy to clipboard, switch view to plain text mode 

    using pointers not references?

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit && QLabel

    Quote Originally Posted by eleanor View Post
    How can I write this line: [...] using pointers not references?
    You can't, because QLineEdit emits textChanged( const QString& ), not textChanged( const QString * ).

  15. #15
    Join Date
    Nov 2006
    Posts
    96

    Default Re: QLineEdit && QLabel

    Hey. I've done everything as you've told me and it does not work.

    This is the error:

    QObject::connect: No such slot QLabel::customSlot(const QString&)
    QObject::connect: (sender name: 'unnamed')
    QObject::connect: (receiver name: 'unnamed')
    Qt Code:
    1. #include <qapplication.h>
    2. #include <qwidget.h>
    3. #include "mojClass.h"
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication a(argc,argv);
    8. mojClass objekt;
    9. a.setMainWidget(&objekt);
    10. objekt.show();
    11. a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <qwidget.h>
    2. #include <qlineedit.h>
    3. #include <qlabel.h>
    4.  
    5.  
    6. class mojClass : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. mojClass();
    11. private:
    12. QLineEdit *lineedit1;
    13. QLabel *label1;
    14. QLabel *label2;
    15. QLabel *label3;
    16. QLabel *label4;
    17. private slots:
    18. void customSlot(const QString &input);
    19. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mojClass.h"
    2.  
    3.  
    4. mojClass::mojClass()
    5. {
    6. setGeometry(100,100,300,200);
    7.  
    8. lineedit1 = new QLineEdit(this);
    9. lineedit1->setGeometry(110,10,50,20);
    10. lineedit1->setEchoMode(QLineEdit::Normal);
    11. lineedit1->setMaxLength(6);
    12.  
    13. label1 = new QLabel(this);
    14. label1->setGeometry(10,10,100,20);
    15. label1->setText("Enter something: ");
    16.  
    17. label2 = new QLabel(this);
    18. label2->setGeometry(10,40,100,20);
    19. label2->setText("You've entered: ");
    20.  
    21. label3 = new QLabel(this);
    22. label3->setGeometry(120,70,100,20);
    23.  
    24. label4 = new QLabel(this);
    25. label4->setGeometry(10,100,100,20);
    26.  
    27. connect(lineedit1,SIGNAL(textChanged(const QString &)),label4,SLOT(customSlot(const QString &)));
    28.  
    29. }
    30.  
    31. void mojClass::customSlot(const QString &input) {
    32. int number = input.toInt() * 4;
    33. label4->setNum(number);
    34. }
    To copy to clipboard, switch view to plain text mode 


    Can you help me?

  16. #16
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QLineEdit && QLabel

    Did you remember to rerun qmake after adding the Q_OBJECT macro, as jacek told you to..?
    J-P Nurmi

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit && QLabel

    Quote Originally Posted by eleanor View Post
    connect(lineedit1,SIGNAL(textChanged(const QString &)),label4,SLOT(customSlot(const QString &)));
    It should be:
    Qt Code:
    1. connect( lineedit1, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot(const QString &)) );
    To copy to clipboard, switch view to plain text mode 
    Because you have defined customSlot() slot in mojClass, not in QLabel.

Similar Threads

  1. [SOLVED] subclassing qlabel
    By mickey in forum Newbie
    Replies: 10
    Last Post: 4th June 2008, 14:43
  2. QT4 layout of complex dialog is very slow
    By cboles in forum Qt Programming
    Replies: 15
    Last Post: 28th April 2006, 19:57
  3. table in QLabel
    By Pan Wojtas in forum Qt Programming
    Replies: 19
    Last Post: 13th February 2006, 11:37
  4. a box around QLineEdit?
    By GreyGeek in forum Qt Tools
    Replies: 13
    Last Post: 8th February 2006, 15:40
  5. QLabel with HTML-style formatting docs?
    By Everall in forum Qt Programming
    Replies: 6
    Last Post: 7th February 2006, 20:01

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.