1、概述
源码放在文章末尾
该项目实现通过回车键让光标从一个编辑框跳转到另一个编辑框,下面是demo演示:
项目部分代码如下:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
private slots:
void next();
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include "qlineedit.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->lineEdit1, SIGNAL(returnPressed()), this, SLOT(next()));
connect(ui->lineEdit2, SIGNAL(returnPressed()), this, SLOT(next()));
connect(ui->lineEdit3, SIGNAL(returnPressed()), this, SLOT(next()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::next()
{
QLineEdit *lineEdit = (QLineEdit *)sender();
if (lineEdit == ui->lineEdit1) {
ui->lineEdit2->setFocus();
} else if (lineEdit == ui->lineEdit2) {
ui->lineEdit3->setFocus();
} else if (lineEdit == ui->lineEdit3) {
ui->lineEdit1->setFocus();
}
}