要实现一个小键盘,可以输入数字0-9,小数点,清空功能
ui布局:
代码如下:
//清空
connect(ui->btnNumCAdptive, &QPushButton::clicked, [=](){
if(focusWidget()!=nullptr)
{
QLineEdit *lineedit = qobject_cast<QLineEdit*>(focusWidget());
if(lineedit)
lineedit->clear();
}
});
//0-9,小数点 按钮
for(auto btn:ui->WidgetNumAdptive->findChildren<QPushButton*>())
{
if(btn == ui->btnNumCAdptive)
continue;
connect(btn, &QPushButton::clicked, [=](){
if(focusWidget()!=nullptr)
{
QLineEdit *lineedit = qobject_cast<QLineEdit*>(focusWidget());
if(lineedit)
{
QString text = btn->text();
QChar c = text.at(0);
int num = c.unicode();
QKeyEvent evt(QEvent::KeyPress,num,Qt::KeyboardModifier::NoModifier,text);
qApp->sendEvent(lineedit,&evt);
}
}
});
}
效果图: