Results 1 to 17 of 17

Thread: QLineEdit && QLabel

Hybrid View

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

    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.

  2. #2
    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 

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

    Default Re: QLineEdit && QLabel

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

Similar Threads

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