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

QLineEdit disable action click



  • I added an icon to a QLineEdit using the addAction method but I was wondering how to remove the click behavior?
    I tried to use setEnabled(false) to the action but it turns the icon gray.

    0_1526952279827_6ae1489b-6ae7-442d-bb42-14505f49dccf-image.png

    disabled:

    0_1526952314357_7afd9105-49b7-4e58-885e-91776546ec46-image.png


  • Moderators

    @Mr-Gisa What do you mean by "click behavior"? What's the point of the button if you can't click it?



  • It's not a button, it's a QAction. If you put an QAction in a QLineEdit you can click on it, I want to disable the click behavior as it is when you use setEnabled(false) the problem is that it turns gray.


  • Moderators

    @Mr-Gisa Still not understanding... Do you just want the icon or do you want the actual qaction? If you want the action why would you not want it to be clickable? And if it was temporarily not clickable then setEnabled(false) is the right way to do it so it grays out so the user knows it is non-usable.

    I think I'm just confused as to what you're trying to accomplish though.



  • I want to put the icon in the QLineEdit that is the easiest way I found to do that, but it comes with the click behavior due to QAction and so I want to remove that behavior


  • Moderators

    @Mr-Gisa Ok so it's just for the icon ... I'll play with it for a minute and see if I can get a fix for you.


  • Moderators

    @Mr-Gisa Are you using a QAction that is linked to an actual clickable action? When I did it, mine doesn't process clicks.

    Here's how it looks:

    0_1526960520317_Screenshot from 2018-05-21 20-41-22.png

    And it's not clickable. I mean it is but it doesn't show that it was clicked and it doesn't do anything.

    Code:

    TestDialog::TestDialog(QWidget *parent)
        : QWidget(parent)
    {
        resize(400, 150);
        
        act_ = new QAction();
        act_->setIcon(QIcon("/usr/share/pixmaps/nobody.png"));
        
        auto le = new QLineEdit(this);
        le->setClearButtonEnabled(true);
        le->addAction(act_, QLineEdit::LeadingPosition);
        le->setPlaceholderText("User...");
        
        auto layout = new QHBoxLayout();
        layout->addWidget(le);
        setLayout(layout);
    }
    


  • @ambershark I'm on Windows and when you press the icon it behaves like it's an actual button (the effect), It's weird cause it feels like an unfinished feature or some bug (user point of view).


  • Moderators

    @Mr-Gisa Windows?! Yuck! :P Let me see if I add an action if it clicks or not. Then if I can make it "click" I can help find a way to stop it. I have some ideas but I need to test them before I tell you about them.


  • Moderators

    @Mr-Gisa Ok played with it a little bit ... linux doesn't show the clicks like windows apparently does. I don't have easy access to my Windows vm with qt in it to test right now.

    There is no easy way to disable the click behavior. Here are some ideas:

    1. You can write a custom class derived from QAction, then ignore clicks. That would be a pain though.
    2. You could write a custom QLineEdit that draws the image (should be fairly easy, but still some work).
    3. You could use a style sheet on the QLineEdit that loads the image and puts it as a left aligned non-repeating background image. No guarantees this would work, but it should.
    4. You could switch your gui to QML where stuff like this is super easy. This isn't a serious recommendation, but if you find you want custom GUIs a lot then QML is definitely the better choice for designing them. Or you end up writing a lot of custom widgets.
    5. You could just ignore the click behavior windows does
    6. Or you could assign the click to do something like focus and select all in the line edit. That would feel natural and explain the "click".

  • Lifetime Qt Champion

    Hi,

    This stack overflow thread provides several possible way of doing it that could be of interest.


Log in to reply