新增文本框回车焦点下移

This commit is contained in:
feiyangqingyun
2019-10-28 13:51:20 +08:00
parent 62a789c61c
commit b73fa9e7f3
7 changed files with 149 additions and 135 deletions

View File

@@ -0,0 +1,31 @@
#-------------------------------------------------
#
# Project created by QtCreator 2019-10-28T13:44:49
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = lineeditnext
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui

11
lineeditnext/main.cpp Normal file
View File

@@ -0,0 +1,11 @@
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

30
lineeditnext/widget.cpp Normal file
View File

@@ -0,0 +1,30 @@
#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();
}
}

25
lineeditnext/widget.h Normal file
View File

@@ -0,0 +1,25 @@
#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

50
lineeditnext/widget.ui Normal file
View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QLineEdit" name="lineEdit1">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>113</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit2">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>113</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit3">
<property name="geometry">
<rect>
<x>10</x>
<y>70</y>
<width>113</width>
<height>20</height>
</rect>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>