以lineEdit和pushButton为例
创建两个lineEdit控件:lineEdit_socketsn和lineEdit_dutsn
和一个pushButton控件:pushButton_sn
需要实现检测到lineEdit_socketsn的回车之后光标自动跳转到lineEdit_dutsn
按pushButton_sn按钮之后,清空lineEdit_socketsn和lineEdit_dutsn的内容,并且光标跳转到lineEdit_socketsn控件
关于光标跳转代码:
//在.h文件中,添加槽函数
public slots:
//点击扫码完成按钮,把扫入的一组socket码和dut码存储,并且控件清空
void on_pushButton_sn_clicked();
//光标跳转槽函数
void processRet_dutsn();
void processRet_socketsn();
//在.cpp文件中的构造函数中 添加控件的信号槽
connect(ui.lineEdit_socketsn, SIGNAL(returnPressed()), this, SLOT(processRet_dutsn()));
connect(ui.pushButton_sn, SIGNAL(clicked()), this, SLOT(processRet_socketsn()));
//.cpp文件中,根据需要的功能实现写入槽函数
void MainWidget::processRet_dutsn()
{
qInfo() << "processRet_dutsn";
// 判断信号发出者
QLineEdit* returnPressedSender = (QLineEdit*)sender();
if (returnPressedSender == ui.lineEdit_socketsn)
{
ui.lineEdit_dutsn->setFocus();
}
}
void MainWidget::processRet_socketsn()
{
qInfo() << "processRet_dutsn";
ui.lineEdit_socketsn->setFocus();
}
另外:按钮点击之后的数据提取与控件清空:(跟光标无关)
void MainWidget::on_pushButton_sn_clicked()
{
qInfo() << "on_pushButton_sn_clicked";
//点击扫码完成按钮
QString socketsn = ui.lineEdit_socketsn->text();
QString dutsn = ui.lineEdit_socketsn->text();
if ((socketsn != "") && (dutsn != ""))
{
//自己进行读取数据的处理
//root["ui_dutsn"] = dutsn;
//root["ui_socketsn"] = socketsn;
//保存完毕,控件清空
ui.lineEdit_socketsn->clear();
ui.lineEdit_dutsn->clear();
qInfo() << QSC("保存UI中的socketsn和dutsn数据完成") ;
}
else {
qInfo() << QSC("保存UI中的socketsn和dutsn数据失败,socketsn为")<< socketsn<< QSC("dutsn为")<<dutsn;
}
}
用以上就能实现需要的功能。
内容参考博客:Qt lineEdit 焦点自动跳转