Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7967973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:00:16+00:00 2026-06-04T07:00:16+00:00

In Qt, a created lineEdit shows a text using the setText() method. But the

  • 0

In Qt, a created lineEdit shows a text using the setText() method.

  1. But the cursor is movable for the default text. I want the cursor should not be movable for the default text.

  2. My lineEdit type has been set as password. Hence the default text(‘Password’) is also displayed as ‘********’. Whenever user types the type has to be changed as password and when there is no text or until the user have not typed any text, the lineEdit should display the plain text ‘password’

Any idea to fix the above two issues?
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:00:17+00:00Added an answer on June 4, 2026 at 7:00 am

    I managed to do what you want by deriving a class from QLineEdit as per following..

    Constructor..

    QCustomLineEdit::QCustomLineEdit(QWidget *parent) :
        QLineEdit(parent)
    {
        connect(this, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
        connect(this, SIGNAL(cursorPositionChanged(int,int)), this, SLOT(onCursorPositionChanged(int,int)));
    
        setEchoMode(QLineEdit::Password);   // Echo mode in your case..
    
        m_echoMode = echoMode();            // Member variable to store original echo mode..
        m_placeHolderText = "Password";     // Member variable..
        m_isPlaceHolderActive = true;       // Member varible..
    
        // Default case..
        setPlaceholderText("");
        setStyleSheet("QCustomLineEdit{color: gray;}");
        setEchoMode(QLineEdit::Normal);
        setText(__placeHolderText);
    }
    

    Override keyPressEvent..

    void QCustomLineEdit::keyPressEvent(QKeyEvent *e)
    {
        if(m_isPlaceHolderActive)
        {
            if(e->key() == Qt::Key_Delete || e->key() == Qt::Key_Backspace)
                e->accept();
            else
                QLineEdit::keyPressEvent(e);
    
            return;
        }
    
        QLineEdit::keyPressEvent(e);
    }
    

    Cursor position change event..

    void QCustomLineEdit::onCursorPositionChanged(int /*oldPos*/, int newPos)
    {
        if(m_isPlaceHolderActive)
        {
            if(newPos != 0)
                setCursorPosition(0);
        }
    }
    

    Text change event..

    void QCustomLineEdit::onTextChanged(const QString &text)
    {
        if(m_isPlaceHolderActive)
        {
            if(text.compare(m_placeHolderText) != 0)
            {
                m_isPlaceHolderActive = false;
    
                // Remove the 'placeHolderText' from 'text' itself..
                QString temp = text;
                temp = temp.mid(0, text.lastIndexOf(m_placeHolderText));
    
                setStyleSheet("QCustomLineEdit{color: black;}");
                setEchoMode(m_echoMode);
                setText(temp);
            }
            else
            {
                setEchoMode(QLineEdit::Normal);
                setText(m_placeHolderText);
                setStyleSheet("QCustomLineEdit{color: gray;}");
                setCursorPosition(0);
            }
        }
        else
        {
            if(text.isEmpty())
            {
                m_isPlaceHolderActive = true;
                setStyleSheet("QCustomLineEdit{color: gray;}");
                setEchoMode(QLineEdit::Normal);
                setText(m_placeHolderText);
            }
        }
    }
    

    I have written it very hastily to just show you. Test it yourself and feel free to point any mistake(s) or optimization(s). Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Created using Sphinx 0.6.5. I know Python's documentation uses reStructuredText , but it has
Created a simple jquery-nivo-like slider using wpf and want to add an animation between
I created a project in flex builder with application server type set to none.
I created a JApplet using Netbeans 6.5.1 that accesses a database. When the program
Created an Accordion side bar using jQuery 1.2.6. I cannot access newer version of
Created a button in my aspx file and add its click method in the
Created basic C++ DLL and exported names using Module Definition file (MyDLL.def). After compilation
This question should hopefully be easy to answer. I created a few buttons in
I created a project without ARC . I want to use a third party
I created an application for android. Now, I want to record a video on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.