Results 1 to 18 of 18

Thread: LineEdit.

  1. #1
    Join Date
    Apr 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default LineEdit.

    Hi,

    I want to create simple program. I want to use lineEdit for inserting a 9-digit number. I've set maxLength = 9.

    Is there any way to make "if" with checking current length of text in lineEdit ? Unfortunately lineEdit->length() doesn't exist.

    When I'm trying to make a QString which will take a value of lineEdit, class isn't looking if QString has length = 9.

    I tried something like that:

    Qt Code:
    1. QString lineeditl;
    2. lineeditl = lineEdit->text(); // i think that line isn't working properly (have no 'if' for reference it)
    3. if(lineeditl.length()==9) {
    4. label_2->setText("Correct.");
    5. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2010
    Posts
    100
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: LineEdit.

    Quote Originally Posted by Rewo View Post
    Hi,

    I want to create simple program. I want to use lineEdit for inserting a 9-digit number. I've set maxLength = 9.

    Is there any way to make "if" with checking current length of text in lineEdit ? Unfortunately lineEdit->length() doesn't exist.

    When I'm trying to make a QString which will take a value of lineEdit, class isn't looking if QString has length = 9.

    I tried something like that:

    Qt Code:
    1. QString lineeditl;
    2. lineeditl = lineEdit->text(); // i think that line isn't working properly (have no 'if' for reference it)
    3. if(lineeditl.length()==9) {
    4. label_2->setText("Correct.");
    5. }
    To copy to clipboard, switch view to plain text mode 
    your code looks correct.

    are you setting the gui properly?

    ui.setupUi(this); //@ the constructor

    and I think you should then use ui.lineEdit->text();

    Do want to check this as the users types or after pushing a button
    if possible place all the code.
    Last edited by ruben.rodrigues; 30th June 2010 at 12:49.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: LineEdit.

    Did you connect the textChanged(QString) signal of the lineEdit with a function/slot that sets the QString lineeditl; ?
    Or does the lineEdit has the text from the beginning? (not the use entering the text)

    Post a compilable example.

  4. #4
    Join Date
    Apr 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Re: LineEdit.

    @ruben.rodrigues : Gui setting is good, don't worry about it.

    @Zlatomir : I tried to use textChanged(QString) signal, but I don't understand why QString parameter is needed. Unfortunately it's not bool signal. LineEdit is empty at start (hasn't got any value).

  5. #5
    Join Date
    Jun 2010
    Posts
    100
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: LineEdit.

    can you post the line where you do the connection to the signal textchannged and the function that is called when the text changes?

  6. #6
    Join Date
    Apr 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Re: LineEdit.

    I can't, because I don't know how to use textChanged. Imo it should be bool singal, dunno what QString parameter needing does here.

  7. #7
    Join Date
    Jun 2010
    Posts
    100
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: LineEdit.

    Quote Originally Posted by Rewo View Post
    I can't, because I don't know how to use textChanged. Imo it should be bool singal, dunno what QString parameter needing does here.
    ah! ok

    at the constructor of your gui class you need to write a line like this:

    connect(lineEdit, SIGNAL(textChanged()), this , SLOT(checkTextLenght());

    define the slot checkTextLenght on your header:

    private slots:
    void checkTextLenght();

    and of course programm the function:

    void YourClass::checkTextLenght()
    {
    your code to check the lenght
    QString lineeditl;
    lineeditl = lineEdit->text(); // i think that line isn't working properly (have no 'if' for reference it)
    if(lineeditl.length()==9) {
    label_2->setText("Correct.");
    }
    }

    Let me know how it works and if the syntax doesn't match is because I don't have qt on my pc.

  8. The following user says thank you to ruben.rodrigues for this useful post:

    Rewo (30th June 2010)

  9. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: LineEdit.

    Quote Originally Posted by Rewo View Post
    @Zlatomir : I tried to use textChanged(QString) signal, but I don't understand why QString parameter is needed. Unfortunately it's not bool signal. LineEdit is empty at start (hasn't got any value).
    The QString parameter it is needed because you need the string.

    Right now your QString remains empty...

    Create a slot that take a QString parameter, and initialise the lineeditl with that QString, and connect the lineEdit textChanged(QString) with the slot you created (so any time the user change the string you will have the lineeditl updated)

  10. #9
    Join Date
    Apr 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Re: LineEdit.

    @ruben.rodrigues

    I did it, but nothing changed.

    In communicates these lines appeared:

    Qt Code:
    1. Object::connect: No such signal QLineEdit::textChanged() in mainwindow.cpp:9
    2. Object::connect: (sender name: 'lineEdit')
    3. Object::connect: (receiver name: 'MainWindow')
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: LineEdit.

    that is because QLineEdit doesn't have a textChanged( VOID) ... it's a textChanged(QString) or QString&

    Post the project...

  12. #11
    Join Date
    Apr 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Re: LineEdit.

    It's my mainwindow.cpp (nothing else is needed, because rest of project is correct)

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QInputDialog>
    4.  
    5. QString lineeditl;
    6.  
    7. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    8. {
    9. setupUi(this);
    10. //connect(pnt,SIGNAL(triggered()),this,SLOT(pnts()));
    11. connect(lineEdit,SIGNAL(textChanged()),this,SLOT(checkTextLength()));
    12. //if(lineeditl[0] == '7') { label_2->setText("Plus ?"); }
    13. }
    14.  
    15. void MainWindow::checkTextLength(){
    16. lineeditl = lineEdit->text();
    17. if(lineeditl.length()==9) {
    18. label_2->setText("Correct.");
    19. }
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. }
    To copy to clipboard, switch view to plain text mode 

    Every time I'm trying to put QString in textChanged, it gives me information that:

    Qt Code:
    1. c:\Qt\4.6.2\include/QtGui/../../src/gui/widgets/qlineedit.h:196: error: 'void QLineEdit::textChanged(const QString&)' is protected
    To copy to clipboard, switch view to plain text mode 

  13. #12
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: LineEdit.

    create a slot in mainWidow something like:
    Qt Code:
    1. //...
    2. public slots:
    3. void setTextInQString (QString inString) {
    4. lineeditl = inString;
    5. }
    To copy to clipboard, switch view to plain text mode 
    And connect the textChanged(QString) with setTextInQString(QString) in mainWindow constructor

    And you will need more signals and slots, because it the constructor of mainWidondow the QString will be empty... Only the connections need to be in the constructor!!!

  14. #13
    Join Date
    Apr 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Re: LineEdit.

    It's hard to understand.

    I did:

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "ui_mainwindow.h"
    6.  
    7. QString lineeditl;
    8.  
    9. class MainWindow : public QMainWindow, Ui::MainWindow {
    10. Q_OBJECT
    11. public:
    12. MainWindow(QWidget *parent = 0);
    13. ~MainWindow();
    14. public slots:
    15. void setTextInQString (QString inString) {
    16. lineeditl = inString;
    17. }
    18. /*protected slots:
    19.   void pnts();
    20.   void checkTextLength();*/
    21. };
    22.  
    23. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QInputDialog>
    4.  
    5. QString lineeditl;
    6.  
    7. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    8. {
    9. setupUi(this);
    10. //connect(pnt,SIGNAL(triggered()),this,SLOT(pnts()));
    11. connect(lineEdit,SIGNAL(textChanged()),this,SLOT(checkTextLength()));
    12. //if(lineeditl[0] == '7') { label_2->setText("Plus ?"); }
    13. connect(lineEdit,textChanged(QString),this,setTextInQString(QString));
    14. }
    15.  
    16. void MainWindow::checkTextLength(){
    17. lineeditl = lineEdit->text();
    18. if(lineeditl.length()==9) {
    19. label_2->setText("Correct.");
    20. }
    21. }
    22.  
    23. MainWindow::~MainWindow()
    24. {
    25. }
    To copy to clipboard, switch view to plain text mode 

    Of course - doesn't work.

  15. #14
    Join Date
    Oct 2009
    Posts
    16
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: LineEdit.

    it should be:

    connect(lineEdit,SIGNAL(textChanged(QString)),this ,SLOT(setTextInQString(QString)));

  16. The following user says thank you to msrihari for this useful post:

    Rewo (30th June 2010)

  17. #15
    Join Date
    Apr 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Re: LineEdit.

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QInputDialog>
    4.  
    5. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    6. {
    7. setupUi(this);
    8. //connect(pnt,SIGNAL(triggered()),this,SLOT(pnts()));
    9. connect(lineEdit,SIGNAL(textChanged()),this,SLOT(checkTextLength()));
    10. //if(lineeditl[0] == '7') { label_2->setText("Plus ?"); }
    11. connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(setTextInQString(QString)));
    12. //,,,SLOT(setTextInQString)
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. }
    18.  
    19. void MainWindow::checkTextLength(){
    20. //lineeditl = lineEdit->text();
    21. if(lineeditl.length()==9) {
    22. label_2->setText("Correct.");
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "ui_mainwindow.h"
    6.  
    7. class MainWindow : public QMainWindow, Ui::MainWindow {
    8. Q_OBJECT
    9. public:
    10. MainWindow(QWidget *parent = 0);
    11. ~MainWindow();
    12. public slots:
    13. void setTextInQString (QString inString) {
    14. lineeditl = inString;
    15. }
    16. protected slots:
    17. /*void pnts();*/
    18. void checkTextLength();
    19. };
    20.  
    21. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Problem:

    Qt Code:
    1. QString lineeditl;
    To copy to clipboard, switch view to plain text mode 

    Where should i define it ? There are errors with multiple definition of this var, wherever I'll put that line.

  18. #16
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: LineEdit.

    You have gone in another direction... if you directly check the QString returned you won't need the extra QString variable.

    I have wrote a little sample project (it's with a QString variable and a Button to check)

    and modified for check directly... (so you have both methods now )

    Advice: Read more on signals and slots
    Attached Files Attached Files
    Last edited by Zlatomir; 30th June 2010 at 14:15.

  19. The following user says thank you to Zlatomir for this useful post:

    Rewo (30th June 2010)

  20. #17
    Join Date
    Apr 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Re: LineEdit.

    This program resolves everything. Thank You very much !
    Last edited by Rewo; 30th June 2010 at 16:26.

  21. #18
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 29 Times in 27 Posts

    Default Re: LineEdit.

    Quote Originally Posted by Rewo View Post
    Hi,

    I want to create simple program. I want to use lineEdit for inserting a 9-digit number. I've set maxLength = 9.

    Is there any way to make "if" with checking current length of text in lineEdit ? Unfortunately lineEdit->length() doesn't exist.
    Why not just use QRegExpValidator? You regex will look like
    Qt Code:
    1. [0-9]{,9}
    To copy to clipboard, switch view to plain text mode 
    I'm a rebel in the S.D.G.

Similar Threads

  1. setFocus() on Lineedit
    By qtuser20 in forum Qt Programming
    Replies: 1
    Last Post: 26th February 2010, 18:54
  2. Help on using QTableWidget with LineEdit.
    By narendra in forum Qt Programming
    Replies: 3
    Last Post: 31st December 2009, 15:30
  3. Validate a value of lineEdit
    By edgar in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2009, 18:22
  4. How to update lineEdit
    By HelloDan in forum Qt Programming
    Replies: 9
    Last Post: 17th February 2009, 07:01
  5. LineEdit with clearbutton ...
    By momesana in forum Qt Programming
    Replies: 3
    Last Post: 13th September 2007, 18:58

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.