Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do you properly increase QLineEdit increase icon size/



  • I have been trying to add an icon to a QLineEdit object to the effect of:
    Screen Shot 2021-04-27 at 10.39.46 PM.png
    In order to this I implemented the following code:

      ui->searchbar->setClearButtonEnabled(true);
      ui->searchbar->QLineEdit::addAction(QIcon(":/img/icons/icon_search.png"), QLineEdit::LeadingPosition);
    

    This is the result I got:
    Screen Shot 2021-04-27 at 10.38.20 PM.png

    In order to try and enlarge it I tried:

     QPixmap in(":/img/icons/icon_search.png");
     QPixmap out(in.size()*0.5);
     QRect r= in.rect();
    
     r.moveCenter(out.rect().center());
     out.fill(Qt::transparent);
    
     QPainter painter(&out);
     painter.drawPixmap(r , in);
     painter.end();
    
     ui->searchbar->setClearButtonEnabled(true);
     ui->searchbar->QLineEdit::addAction(QIcon(out), QLineEdit::LeadingPosition);
    

    However, this produced the following:
    Screen Shot 2021-04-27 at 10.46.03 PM.png
    If you notice the icon is partially cut.
    The second thing I would like to fix is that the icon is pixelated, however, it is smaller than the size of the .png file. (It is an icon from material.io)
    Thank you for your help,
    arjun


  • Lifetime Qt Champion

    Hi,

    What is the original size ?
    What not use the scaled function of QPixmap to make it bigger ?



  • @SGaist The original size is 48 by 48, when I open the .png file with an image reader outside of qt it is larger and clear, it also maintains clarity through scale change(within the range that it would have to change in the application).
    I looked at the documentation of the link you sent, I was wondering if it was possible to set size based on a percentage of the parent widget (QLineEdit in this case)
    Thank you for your help so far!



  • @arjun98 said in How do you properly increase QLineEdit increase icon size/:

    ui->searchbar->setClearButtonEnabled(true);
    ui->searchbar->QLineEdit::addAction(QIcon(":/img/icons/icon_search.png"), QLineEdit::LeadingPosition);

    I have tried loading icon to line edit using above code, it's clearly visibile. and image is not blurred.
    try to experiment loading the icon to QLineEdit without any stylesheet being set to it.