Search code examples
qtqlineeditqactionqiconqtoolbutton

How to make size of button in the LineEdit smaller?


I made the X button in the LineEdit, when i click on this button, the LineEdit is clear. But with my method, the X button looks a little big and not beautiful, I need to make it smaller. How I can do it?

enter image description here

myLineEdit = new LineEdit;
myLineEdit->setFixedHeight( 25 );
m_clear = m_lineEdit->addAction( QIcon( ":/clearButton" ), QLineEdit::TrailingPosition );

the size of clearButton.png is 12x12 px, so in this case it is enlarged and looks not beautiful like this.


Solution

  • For this solution it is assumed that in the original image the relationship between the foreground size and the background is 1: 1 (this is normal in the icons), so the solution is to increase that relationship, for this we create a new image

    QPixmap in(":/clearButton");
    QPixmap out(in.size()*10/7);
    QRect r= in.rect();
    r.moveCenter(out.rect().center());
    out.fill(Qt::transparent);
    
    QPainter painter(&out);
    painter.drawPixmap(r , in);
    painter.end();
    
    QLineEdit *m_lineEdit = new QLineEdit;
    m_lineEdit->setFixedHeight(25);
    m_lineEdit->addAction(QIcon(out), QLineEdit::TrailingPosition);
    

    Before:

    enter image description here

    After:

    enter image description here