10

QTでは、createdlineEditはメソッドを使用してテキストを表示しますsetText()

  1. ただし、デフォルトのテキストではカーソルは移動可能です。デフォルトのテキストに対してカーソルを移動できないようにしたい。

  2. 私のlineEditタイプはパスワードとして設定されています。したがって、デフォルトのテキスト(「パスワード」)も「********」として表示されます。ユーザーが入力するたびに、タイプをパスワードとして変更する必要があり、テキストがない場合、またはユーザーがテキストを入力しなくなるまで、lineEditプレーンテキストの「パスワード」を表示する必要があります。

上記の2つの問題を修正するためのアイデアはありますか? ここに画像の説明を入力してください

4

4 に答える 4

15

コンストラクターに

ui->lineEdit->setPlaceholderText("password");
ui->lineEdit->setReadOnly(1);

そしてon_lineEdit_selectionChanged()SLOTに入れて

ui->lineEdit->setText("");
ui->lineEdit->setEchoMode(QLineEdit::Password);
ui->lineEdit->setReadOnly(0);
于 2012-05-21T07:16:27.610 に答える
8

この質問にはタグpyqtが含まれていることに気付いたので、c ++ではなくPythonの方法を実際に探している人のために、そのタグに関連する実際の回答を示します。

self.searchEditText = QtGui.QLineEdit()
self.searchEditText.setPlaceholderText("Search for word")
于 2017-02-21T23:20:13.960 に答える
5

QLineEdit以下のようにクラスを導き出すことで、なんとかやりたいことができました。

コンストラクタ..

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);
}

オーバーライド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);
}

カーソル位置変更イベント。

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

テキスト変更イベント。

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);
        }
    }
}

お見せするために急いで書きました。自分でテストして、間違いや最適化を自由に指摘してください。お役に立てれば。

于 2012-05-21T07:07:28.083 に答える
4

質問1の場合、Qt 5.0以降では、setPlaceholderTextが必要な処理を実行します。https://codereview.qt-project.org/#change,45326

于 2014-01-28T02:01:39.953 に答える