我正在尝试在 QT 中专门使用 QLineEdit 功能制作一个简单的字符计数器,例如 twitter 中的字符计数器。理想情况下,它应该记录在 QLineEdit 中输入的字符数,并在单独的显示标签中显示记录的数字。例如,Spotify 在为播放列表命名和添加描述时有一个字符计数器。

我声明的用于计算 QLineEdit 中输入的字符数的函数定义如下:
void MainWindow::countChar()
{
QString tempString = ui->displayLabel->text(); //temp variable to hold the lineEdit's text
int output = tempString.size(); //to get the number of characters in the lineEdit
QString s = QString::number(output);//convert an int to QString
ui->CharCounter->setText(s); //display the number in the displayLabel(CharCounter)
ui->CharCounter->adjustSize();
}
Run Code Online (Sandbox Code Playgroud)
我在 main.cpp 中的对象 w 下调用此函数。
w.countChar();
Run Code Online (Sandbox Code Playgroud)
我想创建字符计数器函数的原因是因为我为 QLineEdit 设置了字符限制,因此用户输入的任何内容都可以以窗口的最小尺寸填充到主 displayLabel 中。我通过编写另一个函数来做到这一点:
void MainWindow::setlineInputTextCharLimit(int limit)
{
ui->inputText->setMaxLength(limit);
}
Run Code Online (Sandbox Code Playgroud)
我在同一个对象下调用它,w:
w.setLineInputTextCharLimit(200);
Run Code Online (Sandbox Code Playgroud)
QTCreator 能够成功构建,但在我在 QLineEdit 中输入一定量的文本后,charCounter …
我想将文档中的特定字段保存到变量中。到目前为止我的代码:
func getDocument(path: String, field: String? = "nil") -> some Any{
var returnVar : Any = "DEFAULT VAL"
var db: Firestore!
db = Firestore.firestore()
let docRef = db.document(path)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
if(field != "nil"){
let property = document.get("phrase") ?? "nil"
returnVar = property
return;
}
else{
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
returnVar = dataDescription
return;
}
} else {
print("Document does not exist")
returnVar = -1
return;
}
}
print("Returned …Run Code Online (Sandbox Code Playgroud)