当前位置:网站首页>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 .
边栏推荐
- Is the sanic asynchronous framework really so strong? Find truth in practice
- Codeforces Round #797 (Div. 3)无F
- Codeforces Round #801 (Div. 2)A~C
- The "sneaky" new asteroid will pass the earth safely this week: how to watch it
- Opencv learning log 28 -- detect the red cup cover
- 树莓派4B64位系统安装miniconda(折腾了几天终于解决)
- 去掉input聚焦时的边框
- Shell Scripting
- Programmers, what are your skills in code writing?
- Problem - 1646C. Factorials and Powers of Two - Codeforces
猜你喜欢
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
Anaconda下安装Jupyter notebook
Codeforces Round #799 (Div. 4)A~H
第 300 场周赛 - 力扣(LeetCode)
Codeforces Round #799 (Div. 4)A~H
C language must memorize code Encyclopedia
Maximum product (greedy)
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Radar equipment (greedy)
Codeforces Round #801 (Div. 2)A~C
随机推荐
Problem - 1646C. Factorials and Powers of Two - Codeforces
AcWing:第58场周赛
Pyside6 signal, slot
Openwrt source code generation image
It is forbidden to trigger onchange in antd upload beforeupload
Interval sum ----- discretization
Flag framework configures loguru logstore
875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
第 300 场周赛 - 力扣(LeetCode)
F - birthday cake (Shandong race)
Hdu-6025-prime sequence (girls' competition)
QT实现窗口渐变消失QPropertyAnimation+进度条
Acwing - game 55 of the week
Opencv learning log 29 -- gamma correction
Opencv learning log 28 -- detect the red cup cover
Candy delivery (Mathematics)
(POJ - 3579) median (two points)
What is the difficulty of programming?
Problem - 1646C. Factorials and Powers of Two - Codeforces
C language learning notes