Results 1 to 12 of 12

Thread: how to get the text from a LineEdit using QT??

  1. #1
    Join Date
    Jul 2013
    Posts
    25
    Qt products
    Qt5
    Platforms
    Windows

    Default how to get the text from a LineEdit using QT??

    I want to display the text in the LineEdit using Qt.
    but when i use the code an empty string wiil be displayed.
    The following is my code.

    Qt Code:
    1. if( QPushButton *button = (QPushButton *)sender())
    2. {// it's a "QPushButton", do something with pb here
    3. QFrame* popup1 = new QFrame(this, Qt::Tool | Qt::Window );
    4. popup1->setWindowTitle("Rename the folder");
    5. QLineEdit *lb=new QLineEdit(popup1);
    6. connect( lb, SIGNAL( returnPressed() ), lb, SLOT( hide() ) );
    7. lb->setFocus();
    8. lb->show();
    9. lb->move(QCursor::pos());
    10. qDebug()<<lb->text();
    11. button->setText(lb->text());
    12. button->setObjectName(lb->text());
    13.  
    14. } }
    To copy to clipboard, switch view to plain text mode 
    Can any one help me.
    Thanks in advance.
    Last edited by high_flyer; 4th September 2013 at 12:18. Reason: code tags

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get the text from a LineEdit using QT??

    Qt Code:
    1. if( QPushButton *button = (QPushButton *)sender())
    2. {// it's a "QPushButton", do something with pb here
    3. QFrame* popup1 = new QFrame(this, Qt::Tool | Qt::Window );
    4. popup1->setWindowTitle("Rename the folder");
    5. QLineEdit *lb=new QLineEdit(popup1);
    6. connect( lb, SIGNAL( returnPressed() ), lb, SLOT( hide() ) );
    7. lb->setFocus();
    8. lb->show();
    9. lb->move(QCursor:os());
    10. qDebug()<<lb->text();
    11. button->setText(lb->text());
    12. button->setObjectName(lb->text());
    13.  
    14. } }
    To copy to clipboard, switch view to plain text mode 

    u are not setting any text in QLineEdit in ur code .
    u shuld use
    Qt Code:
    1. lb->setText("Ur Text that u want to display");
    To copy to clipboard, switch view to plain text mode 
    or else while constructing the lineEdit
    QLineEdit::QLineEdit ( const QString & contents, QWidget * parent = 0 )u can set
    Qt Code:
    1. QLineEdit *lb=new QLineEdit("Ur Text", popup1);
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Jul 2013
    Posts
    25
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to get the text from a LineEdit using QT??

    Actually the text is entered at run time.
    Whatever is entering at runtime, that should be displayed using text() and set as the text of the button.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to get the text from a LineEdit using QT??

    Sure, but your code just reads the initial text, which in your snippet, is empty.

    You create a line edit and read its content, which if course is unchanged since there has been no possibility for any user interaction.

    What you probably want is to show the input window and set the button once there the user has entered some text.
    Connect a slot to editingFinished() or returnPressed() and read the text in there.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get the text from a LineEdit using QT??

    Quote Originally Posted by Soumya Somasekhar Ram View Post
    Actually the text is entered at run time.
    Whatever is entering at runtime, that should be displayed using text() and set as the text of the button.
    u need to connect the signal of lineEdit to a slot which will update the text of QPushButton
    Qt Code:
    1. connect( lb, SIGNAL( textChanged (const QString &) ), lb, SLOT( changeButtonText(const QString &) ) );
    To copy to clipboard, switch view to plain text mode 
    slot:
    Qt Code:
    1. changeButtonText(const QString &data)
    2. {
    3. button->setText(data);
    4. }
    To copy to clipboard, switch view to plain text mode 

    as u used returnPressed() , remove the hide() slot
    and add ur changeButtonText()
    Qt Code:
    1. connect( lb, SIGNAL( returnPressed ( ), lb, SLOT( changeButtonText() ) )
    To copy to clipboard, switch view to plain text mode 

    and in
    Qt Code:
    1. changeButtonText()
    2. {
    3. button->setText(lb->text());
    4. }
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to get the text from a LineEdit using QT??

    Just to clarify what wagmare wrote: those are two options, both will result in the line edit's text on the button.
    The first option will update the button text as the user types, the second one will update it only at the end.

    Either option can keep the connect to hide() though.

    Cheers,
    _

  7. #7
    Join Date
    Jul 2013
    Posts
    25
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to get the text from a LineEdit using QT??

    That code didn't work.
    Actually its because of the object name of the dynamically created push button won't available using the following code.

    QPushButton *button = (QPushButton *)sender()

    Can u suggest any other way to get the reference of that particular button ,when user clicks on it.
    Thank you for your response.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to get the text from a LineEdit using QT??

    Rule of thumb: if you need to access a widget after construction, keep its pointer in a member variable of your class

    Cheers,
    _

  9. #9
    Join Date
    Jul 2013
    Posts
    25
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to get the text from a LineEdit using QT??

    But the problem is ,here I am creating alarge no of pushButtons dynamically.
    I have to identify which one is currently selected.
    Is there any other method for checking the active widget?
    QObject::sender() return the refference of the main window.
    Can any one help me.
    Thanks in advance.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to get the text from a LineEdit using QT??

    Quote Originally Posted by Soumya Somasekhar Ram View Post
    But the problem is ,here I am creating alarge no of pushButtons dynamically.
    Ah, you didn't say that.

    Look at QButtonGroup or alternatively for QSignalMapper

    Cheers,
    _

  11. #11
    Join Date
    Jul 2013
    Posts
    25
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to get the text from a LineEdit using QT??

    In the above reply of Mr.Wagmare, where should I define the functions changeButtonText(const QString &data) and changeButtonText().

    I defined it as
    void Mainwindow::changeButtonText(const QString &data)
    {
    }
    Then ,it shows a run time error that no such slot for lb(label name here).

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to get the text from a LineEdit using QT??

    Well, obviously you need to use the pointer of the main window instance as the receiver object in the connect statement.
    e.g. "this" if you do the connect from inside a main window method

    Cheers,
    _

Similar Threads

  1. send text of lineedit
    By akhilteja in forum Newbie
    Replies: 1
    Last Post: 23rd August 2013, 10:53
  2. Clear text of LineEdit
    By stbb24 in forum Qt Programming
    Replies: 7
    Last Post: 12th June 2012, 14:30
  3. lineEdit's setSelection - How to show more text?
    By squidge in forum Qt Programming
    Replies: 3
    Last Post: 30th January 2010, 18:22
  4. Get text from dynamic lineedit
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2010, 19:56
  5. Lineedit text change signal
    By zgulser in forum Qt Tools
    Replies: 2
    Last Post: 19th January 2009, 14:38

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.