当前位置:网站首页>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 .
边栏推荐
- Codeforces Round #803 (Div. 2)A~C
- Bidirectional linked list - all operations
- QNetworkAccessManager实现ftp功能总结
- Openwrt build Hello ipk
- Luogu P1102 A-B number pair (dichotomy, map, double pointer)
- Problem - 922D、Robot Vacuum Cleaner - Codeforces
- Codeforces Round #801 (Div. 2)A~C
- Useeffect, triggered when function components are mounted and unloaded
- Share an example of running dash application in raspberry pie.
- 栈的经典应用—括号匹配问题
猜你喜欢
C language must memorize code Encyclopedia
807. Maintain the urban skyline
PySide6 信号、槽
Frida hook so layer, protobuf data analysis
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
(POJ - 3685) matrix (two sets and two parts)
力扣:第81场双周赛
Problem - 922D、Robot Vacuum Cleaner - Codeforces
1689. Ten - the minimum number of binary numbers
Flask框架配置loguru日志庫
随机推荐
Truck History
875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
Configuration du cadre flask loguru log Library
Codeforces Round #799 (Div. 4)A~H
Codeforces Round #800 (Div. 2)AC
Acwing - game 55 of the week
[exercise -11] 4 values why sum is 0 (and 4 values of 0)
socket通讯
TCP's three handshakes and four waves
Li Kou - 298th weekly match
树莓派4B64位系统安装miniconda(折腾了几天终于解决)
[exercise-3] (UVA 442) matrix chain multiplication
Input can only input numbers, limited input
1903. Maximum odd number in string
1605. Sum the feasible matrix for a given row and column
去掉input聚焦时的边框
Socket communication
Nodejs crawler
Codeforces Round #802(Div. 2)A~D
Interesting drink