按键事件,获取QInputDialog弹框内容
- 如果不需要把弹框确认和取消变成中文显示直接使用静态方法即可
void MyWindow::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_F1) {
// 弹出搜索框
bool ok;
QString result = QInputDialog::getText(this, ("Search"), ("Enter search text:"), QLineEdit::Normal, QString(), &ok);
// 如果用户按下了 OK 按钮,输出搜索结果
if (ok) {
// 将搜索文本转换为大写字母
result = result.toUpper();
qDebug() << "Search result: " << result;
// 执行其他操作
// ...
}
} else {
// 其他按键事件
QWidget::keyPressEvent(event);
}
}
- 如果需要把确认取消中文显示,把上述弹出搜索框内容换成以下内容
bool ok;
QInputDialog *dialog = new QInputDialog(this);
// 设置确认和取消按钮的文本为中文
dialog->setOkButtonText(QString::fromLocal8Bit("确定"));
dialog->setCancelButtonText(QString::fromLocal8Bit("取消"));
dialog->setWindowTitle("搜索");
dialog->setLabelText("输入车牌");
dialog->exec();
QString plate = dialog->textValue();
qDebug() << "plate.toUpper()--" << plate.toUpper();
if ( !plate.isEmpty() && !plate.contains( " " ) ) {
m_items = m_resultModel->findItems( plate.toUpper(), Qt::MatchContains );
for ( int i = 0; i < m_items.count(); ++i ) {
ui->tableView->selectRow( m_items.at(i)->row() );
}
delete dialog;
}