Qt LineEdit cursor text position + set icon problem

Insert picture description here

demand

The main problem is that the text will appear under the icon, so I hope to start typing from the right side of the icon.

Preface

This problem has not been solved for a long time. I searched the Internet for a long time, such as setCursorPosition, setFocus, LineEdit text position setting setTextCursor, cursorForward, and setAlignment initialized at the beginning and end of the text can not solve the problem. The following code All are some attempts.

Some attempts

//    int i = ui->lineEdit->cursorPosition();
//    i+=10;
//    qDebug() << i;
//    ui->lineEdit->setCursorPosition(i);
//    ui->lineEdit_2->setCursorPosition(i);
//    ui->lineEdit->setFocus();
//    ui->lineEdit_2->setFocus();

    //ui->lineEdit->setCursorPosition(120);
    //ui->lineEdit->setFocus();
      //ui->lineEdit->setCursorPosition(ui->lineEdit->text().size());
    //tmpCursor.setPosition(20);
   // ui->lineEdit->setTextCursor(tmpCursor);
    //ui->lineEdit->setAlignment(Qt::AlignJustify);

    //ui->lineEdit->cursorForward(10);//往右

    //ui->lineEdit->setFocusPolicy(Qt::NoFocus); // 得到焦点时,不显示虚线框

solution

The icon is a label. Then this size is the approximate size of the icon (not precise), but leaving this space is enough.

	//一般小图标就是这个大小
    QSize size = QSize(30, 30);
    // 设置输入框中文件输入区,不让输入的文字被隐藏在按钮下
    ui->lineEdit->setTextMargins(size.width(), 1, 1 , 1);

Code location

Just set it at this location:
Insert picture description here

reference

https://blog.csdn.net/sniper_bing/article/details/47039939

Guess you like

Origin blog.csdn.net/qq_16488989/article/details/109068789