在QT中,创建了一个使用setText()方法显示文本的lineedit。qlineedit有一些默认文本,不应该移动光标?
但是光标是可移动的默认文本。我希望光标不能移动为默认文本。
我的lineedit类型已被设置为密码。因此,默认文本('密码')也显示为'********'。每当类型具有用户类型被改变为密码,并且当没有文字或者直到用户没有键入任何文本,则QLineEdit的应显示纯文本“密码”
任何想法修复以上两个问题?
在QT中,创建了一个使用setText()方法显示文本的lineedit。qlineedit有一些默认文本,不应该移动光标?
但是光标是可移动的默认文本。我希望光标不能移动为默认文本。
我的lineedit类型已被设置为密码。因此,默认文本('密码')也显示为'********'。每当类型具有用户类型被改变为密码,并且当没有文字或者直到用户没有键入任何文本,则QLineEdit的应显示纯文本“密码”
任何想法修复以上两个问题?
我设法做你想做的通过获得来自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);
}
}
}
我也很匆忙,只是告诉你写的。自己测试一下,随时指出任何错误或优化。希望这可以帮助。
在构造函数中把
ui->lineEdit->setPlaceholderText("password");
ui->lineEdit->setReadOnly(1);
而且在on_lineEdit_selectionChanged()
SLOT,把
ui->lineEdit->setText("");
ui->lineEdit->setEchoMode(QLineEdit::Password);
ui->lineEdit->setReadOnly(0);
谢谢spyke。 setPlaceholderText()方法很好。但是,无论何时qlineedit小部件在qlineedit上集中或鼠标点击,默认文本都会隐藏。我想要显示默认文本,即使光标在小部件中,条件是光标不应该移动,直到用户键入任何文本.... –
问题1,使用Qt 5.0和更高版本,setPlaceholderText你想要做什么。 https://codereview.qt-project.org/#change,45326
我注意到这个问题有标签pyqt,所以我会为那些实际寻找python方式而不是C++的人提供与该标签相关的实际答案。
self.searchEditText = QtGui.QLineEdit()
self.searchEditText.setPlaceholderText("Search for word")
这对我python python python 3.5中的工作,谢谢 –
不客气! – answerSeeker
setPlaceholderText()方法很好。但是,无论何时qlineedit小部件在qlineedit上集中或鼠标点击,默认文本都会隐藏。我想要显示默认文本,即使光标位于小部件中,条件是不应该移动光标,直到用户输入任何文本。 –
好的。删除ui-> lineEdit-> setText(“”);和ui-> lineEdit-> setEchoMode(QLineEdit :: Password);来自on_lineEdit_selectionChanged()。并添加ui-> lineEdit-> setEchoMode(QLineEdit :: Password);在on_lineEdit_textEdited() – ScarCode
@ user971306:我在我更新的答案中添加了一些代码。 – Ammar