当前位置:网站首页>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 .
边栏推荐
- Configuration du cadre flask loguru log Library
- Truck History
- Date plus 1 day
- E. Breaking the Wall
- Some problems encountered in installing pytorch in windows11 CONDA
- 树莓派4B64位系统安装miniconda(折腾了几天终于解决)
- Basic Q & A of introductory C language
- Codeforces Round #799 (Div. 4)A~H
- [exercise-3] (UVA 442) matrix chain multiplication
- Advancedinstaller安装包自定义操作打开文件
猜你喜欢

浏览器打印边距,默认/无边距,占满1页A4

C language is the watershed between low-level and high-level

QWidget代码设置样式表探讨

<li>圆点样式 list-style-type

7-1 understand everything (20 points)

1323. Maximum number of 6 and 9

力扣——第298场周赛

The concept of C language array

1689. Ten - the minimum number of binary numbers

Data storage in memory & loading into memory to make the program run
随机推荐
useEffect,函數組件掛載和卸載時觸發
QWidget代码设置样式表探讨
Openwrt source code generation image
Borg maze (bfs+ minimum spanning tree) (problem solving report)
969. Pancake sorting
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
pytorch提取骨架(可微)
Is the sanic asynchronous framework really so strong? Find truth in practice
双向链表—全部操作
QT有关QCobobox控件的样式设置(圆角、下拉框,向上展开、可编辑、内部布局等)
921. Minimum additions to make parentheses valid
2078. Two houses with different colors and the farthest distance
[exercise -10] unread messages
Alice and Bob (2021 Niuke summer multi school training camp 1)
Data storage in memory & loading into memory to make the program run
Penetration test (4) -- detailed explanation of meterpreter command
Bidirectional linked list - all operations
The concept of C language array
Generate random password / verification code
[exercise-4] (UVA 11988) broken keyboard = = (linked list)