当前位置:网站首页>QT按钮点击切换QLineEdit焦点(含代码)
QT按钮点击切换QLineEdit焦点(含代码)
2022-07-06 09:28:00 【Larry_Yanan】
正常来说,点击QLineEdit控件后,焦点就进入该控件,可以进行键盘文字输入了。但某些场合可能需要通过按钮来点击,切换焦点所处的QLineEdit,如图
通过点击按钮,切换焦点所在的控件,这是下面讨论的问题
好,首先要明白QLineEdit的两个接口,setFocus();和hasFocus();,前者是设置焦点,能将焦点切换到具体的QLineEdit,后者是获取焦点,返回bool值。
一开始的想法是,通过遍历获取当前QLineEdit的焦点标志,判断按钮点击后要对哪一个QLineEdit设置焦点。但问题是,点击按钮的那一刻,会将焦点切换到按钮本身上,从而导致所有的QLineEdit都失去了焦点。
所以,我们需要在QLineEdit处于焦点的时候,外部对其有一个记录,以免像上面这样被覆盖。因此,QLineEdit需要自定义一下,这里在它的focusInEvent事件中重载一下。
mylineedit.h
#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H
#include <QLineEdit>
#include <QDebug>
class myLineEdit : public QLineEdit
{
Q_OBJECT
public:
myLineEdit(QWidget *parent = nullptr);
~myLineEdit();
void set_num(int num_);
protected:
void focusInEvent(QFocusEvent *) override;
int num = -1;
signals:
void sig_lineedit_num(int num);
};
#endif // MYLINEEDIT_H
mylineedit.cpp
#include "mylineedit.h"
myLineEdit::myLineEdit(QWidget *parent) : QLineEdit(parent)
{
}
myLineEdit::~myLineEdit()
{
}
void myLineEdit::set_num(int num_)
{
num = num_;
}
//焦点进入时响应
void myLineEdit::focusInEvent(QFocusEvent *e)
{
emit sig_lineedit_num(num);//发送信号
QLineEdit::focusInEvent(e);//正常处理焦点接收事件
}
通过set_num我们可以对QLineEdit都设置一个标志,在focusInEvent焦点进入时,信号返回该标志,从而在外部响应并记录
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
int flag = -1;//记录当前焦点的QLineEdit编号(自己设置)
private slots:
void on_pushButton_clicked();
void slot_lineedit_num(int num);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//连接信号槽
connect(ui->lineEdit,SIGNAL(sig_lineedit_num(int)),this,SLOT(slot_lineedit_num(int)));
connect(ui->lineEdit_2,SIGNAL(sig_lineedit_num(int)),this,SLOT(slot_lineedit_num(int)));
connect(ui->lineEdit_3,SIGNAL(sig_lineedit_num(int)),this,SLOT(slot_lineedit_num(int)));
//设置控件编号
ui->lineEdit->set_num(0);
ui->lineEdit_2->set_num(1);
ui->lineEdit_3->set_num(2);
//初始默认其中一个设置为焦点
ui->lineEdit->setFocus();
}
MainWindow::~MainWindow()
{
delete ui;
}
//切换焦点
void MainWindow::on_pushButton_clicked()
{
if(flag == 0)
{
flag = 1;
ui->lineEdit_2->setFocus();
}else if(flag == 1)
{
flag = 2;
ui->lineEdit_3->setFocus();
}else if(flag == 2)
{
flag = 0;
ui->lineEdit->setFocus();
}
ui->lineEdit_2->hasFocus();
}
//槽函数接受响应编号,记录下来
void MainWindow::slot_lineedit_num(int num)
{
qDebug()<<"shoudao num"<<num;
flag = num;
}
在slot_lineedit_num中记录编号,在on_pushButton_clicked的按钮响应中根据编号进行setFocus的切换,从而达到我们的目的。
这里还设计到按钮自定义的提升,即在ui界面中对QLineEdit提升为myLineEdit,非常简单,不懂的同学再自己百度吧。
边栏推荐
- China potato slicer market trend report, technical dynamic innovation and market forecast
- 栈的经典应用—括号匹配问题
- Research Report on shell heater industry - market status analysis and development prospect forecast
- Nodejs+vue网上鲜花店销售信息系统express+mysql
- 树莓派4B安装opencv3.4.0
- Opencv learning log 13 corrosion, expansion, opening and closing operations
- Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
- Gartner:关于零信任网络访问最佳实践的五个建议
- 渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
- Maximum product (greedy)
猜你喜欢
Data storage in memory & loading into memory to make the program run
Nodejs+vue网上鲜花店销售信息系统express+mysql
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
“鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
628. Maximum product of three numbers
【高老师UML软件建模基础】20级云班课习题答案合集
Information security - threat detection - detailed design of NAT log access threat detection platform
Pyside6 signal, slot
Gartner: five suggestions on best practices for zero trust network access
随机推荐
Maximum product (greedy)
1013. Divide the array into three parts equal to and
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
B - Code Party (girls' competition)
Opencv learning log 13 corrosion, expansion, opening and closing operations
Shell脚本编程
1605. Sum the feasible matrix for a given row and column
Nodejs crawler
渗透测试 ( 1 ) --- 必备 工具、导航
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
Openwrt build Hello ipk
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
最全编程语言在线 API 文档
【练习-11】4 Values whose Sum is 0(和为0的4个值)
【练习-6】(Uva 725)Division(除法)== 暴力
【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
D - function (HDU - 6546) girls' competition
Gartner: five suggestions on best practices for zero trust network access
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具