【发布时间】:2012-05-14 10:59:21
【问题描述】:
我有一个 QLineEdit,我在其上设置了一个 QRegExpValidator,它允许用户在单词之间只输入一个空格。
现在我希望每当用户尝试输入多个空格时,应该显示 QLineEdit 的工具提示,但我没有任何方法来实现它。
感谢:)
【问题讨论】:
标签: qt user-interface tooltip qlineedit
我有一个 QLineEdit,我在其上设置了一个 QRegExpValidator,它允许用户在单词之间只输入一个空格。
现在我希望每当用户尝试输入多个空格时,应该显示 QLineEdit 的工具提示,但我没有任何方法来实现它。
感谢:)
【问题讨论】:
标签: qt user-interface tooltip qlineedit
似乎没有直接的方法可以执行您想要的操作。一种方法是处理 QLineEdit 的 textChanged() 信号。然后,您可以使用 QRegExp::exactMatch() 函数将该字符串与您的正则表达式进行比较,如果不匹配,则显示工具提示。
连接信号..
...
connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(onTextChanged(QString)));
...
你的位置到了..
void MainWindow::onTextChanged(QString text)
{
QRegExp regExp;
regExp.setPattern("[^0-9]*"); // For example I have taken simpler regex..
if(regExp.exactMatch(text))
{
m_correctText = text; // Correct text so far..
QToolTip::hideText();
}
else
{
QPoint point = QPoint(geometry().left() + ui->lineEdit->geometry().left(),
geometry().top() + ui->lineEdit->geometry().bottom());
ui->lineEdit->setText(m_correctText); // Reset previous text..
QToolTip::showText(point,"Cannot enter number..");
}
}
【讨论】:
m_correctText。每当正则表达式匹配失败时,您使用QLineEdit::setText(m_correctText).. Ya 重置工具提示check this.. 或者另一个建议是用红色文本显示QLabel,位于QLineEdit 控件的右侧。 :)
我不记得显示工具提示的显式 API。恐怕您必须弹出一个自定义工具窗口(即无父级QWidget)才能达到预期的结果。
如果您想像标准工具提示一样设置自己的弹出窗口的样式,QStyle 应该有相应的功能。如果有疑问,请阅读它呈现工具提示的 Qt 源代码。这会告诉你要使用哪些样式元素。
【讨论】: