lineedit focus



  • in a class member function i wanna change lineedit text when the text/focus changes. however, this won't work:

    const auto lineEdit = new QLineEdit(this);
    lineEdit->setPlaceholderText("whatever...");
    QObject::connect(lineEdit, &QLineEdit::textChanged, [&lineEdit, this]() {
        lineEdit->setPlaceholderText(lineEdit->text().isEmpty() ? "" : lineEdit->placeholderText());
    });
    

    this crashes. please help!



  • @user4592357 said in lineedit focus:

    &lineEdit, this

    Try to replace [=]



  • Wait wait....
    @user4592357 said in lineedit focus:

    lineEdit->setPlaceholderText(lineEdit->text().isEmpty() ? "" : lineEdit->placeholderText());
    

    Are you joking?



  • @Taz742

    i wanna set placeholder when there's no text



  • Hi @user4592357

    but the place holder text is shown automatically when there is no real text. That's what a place holder text is about.

    -Michael.



  • @user4592357 said in lineedit focus:

    i wanna set placeholder when there's no text

    PlaceholderText will it ever change? If not, you do not need to set up again.

    Please test this code:

        static int cnt = 0;
        const auto lineEdit = new QLineEdit(this);
        lineEdit->setPlaceholderText("whatever...");
        QObject::connect(lineEdit, &QLineEdit::textChanged, [=]() {
            lineEdit->setPlaceholderText(lineEdit->text().isEmpty() ? QString::number(cnt += 1) : lineEdit->placeholderText());
        });
    

  • Qt Champions 2017

    @Taz742 said in lineedit focus:

    @user4592357 said in lineedit focus:

    i wanna set placeholder when there's no text

    PlaceholderText will it ever change? If not, you do not need to set up again.

    Please test this code:

        static int cnt = 0;
        const auto lineEdit = new QLineEdit(this);
        lineEdit->setPlaceholderText("whatever...");
        QObject::connect(lineEdit, &QLineEdit::textChanged, [=]() {
            lineEdit->setPlaceholderText(lineEdit->text().isEmpty() ? QString::number(cnt += 1) : lineEdit->placeholderText());
        });
    

    If you really want to do it like this, I'd recommend:

         static int cnt = 0;
         const auto lineEdit = new QLineEdit(this);
         lineEdit->setPlaceholderText("whatever...");
         QObject::connect(lineEdit, &QLineEdit::textChanged, [=]() {
         if (lineEdit->text().isEmpty())
             lineEdit->setPlaceholderText(QString::number(++cnt));
         });
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.