当前位置:网站首页>Click QT button to switch qlineedit focus (including code)
Click QT button to switch qlineedit focus (including code)
2022-07-06 16:17:00 【Larry_ Yanan】
Normally , Click on QLineEdit After control , The focus enters the control , Keyboard text input is available . But in some cases, you may need to click through the button , Switch the focus QLineEdit, Pictured
By clicking the button , Switch the control where the focus is , This is the problem discussed below
good , First of all, understand QLineEdit Two interfaces of ,setFocus(); and hasFocus();, The former is to set the focus , Can switch the focus to specific QLineEdit, The latter is to gain focus , return bool value .
The idea at first was , Get the current through traversal QLineEdit Focus mark , Judge which button to click QLineEdit set focus . But the problem is , The moment you click the button , Will switch the focus to the button itself , This leads to all QLineEdit Have lost focus .
therefore , We need to be in QLineEdit When in focus , There is an external record , To avoid being covered like this . therefore ,QLineEdit Need to customize , Here it is focusInEvent Reload in the event .
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_;
}
// Respond when the focus enters
void myLineEdit::focusInEvent(QFocusEvent *e)
{
emit sig_lineedit_num(num);// Sending signal
QLineEdit::focusInEvent(e);// Normally handle focus receiving events
}
adopt set_num We can QLineEdit Set a sign , stay focusInEvent When the focus enters , The signal returns this flag , So as to respond externally and record
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;// Record the current focus QLineEdit Number ( Set up your own )
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 the signal slot
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)));
// Set the control number
ui->lineEdit->set_num(0);
ui->lineEdit_2->set_num(1);
ui->lineEdit_3->set_num(2);
// By default, one of them is set as focus
ui->lineEdit->setFocus();
}
MainWindow::~MainWindow()
{
delete ui;
}
// Switch focus
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();
}
// Slot function accepts response number , recorded
void MainWindow::slot_lineedit_num(int num)
{
qDebug()<<"shoudao num"<<num;
flag = num;
}
stay slot_lineedit_num Record number in , stay on_pushButton_clicked According to the number in the button response of setFocus Handoff , So as to achieve our goal .
Here is also designed to enhance button customization , That is to say ui Interface alignment QLineEdit Upgrade to myLineEdit, It's simple , If you don't understand, please Baidu yourself .
边栏推荐
猜你喜欢
Read and save zarr files
Analysis of protobuf format of real-time barrage and historical barrage at station B
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
Codeforces Round #797 (Div. 3)无F
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Configuration du cadre flask loguru log Library
浏览器打印边距,默认/无边距,占满1页A4
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
Flask框架配置loguru日志库
Write web games in C language
随机推荐
Flag framework configures loguru logstore
2027. Minimum number of operations to convert strings
Vs2019 initial use
input 只能输入数字,限定输入
Effet d'utilisation, déclenché lorsque les composants de la fonction sont montés et déchargés
AcWing——第55场周赛
Codeforces Round #802(Div. 2)A~D
OneForAll安装使用
Some problems encountered in installing pytorch in windows11 CONDA
860. Lemonade change
TCP's three handshakes and four waves
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
Sanic异步框架真的这么强吗?实践中找真理
Candy delivery (Mathematics)
1013. Divide the array into three parts equal to and
1855. Maximum distance of subscript alignment
It is forbidden to trigger onchange in antd upload beforeupload
Common configuration files of SSM framework
C language must memorize code Encyclopedia
b站 實時彈幕和曆史彈幕 Protobuf 格式解析