当前位置:网站首页>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
 Insert picture description here
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 .

原网站

版权声明
本文为[Larry_ Yanan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060920129342.html