1

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.

songvan
  • 369
  • 5
  • 21
  • the solution would be to modify the image so that the x has less size while maintaining the same size of the image. – eyllanesc Aug 02 '18 at 09:20
  • @eyllanesc: thank you, I thought about that already. But don't we have solution with coding? Because what happen if in the future I set `myLineEdit->setFixedHeight( 50 );` then I have to change the size of the image again? Therefore I think the solution with coding would be better :) – songvan Aug 02 '18 at 09:23
  • The solution that I proposed can be implemented in code, or without it. – eyllanesc Aug 02 '18 at 09:28
  • What size do you want the icon to have? Do you want it to be the same height as the text or a bit smaller? If it is smaller, how much smaller? – eyllanesc Aug 02 '18 at 09:30
  • The height of LineEdit is 25 so I want the height of the icon as original size (15*15) :) – songvan Aug 02 '18 at 09:44
  • if the height of the QLineEdit is "H", what would be the size of the icon you want. – eyllanesc Aug 02 '18 at 09:44
  • I think H*0,7 should be ok as an example – songvan Aug 02 '18 at 09:48
  • see my answer :) – eyllanesc Aug 02 '18 at 10:11

1 Answers1

5

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241