QLineEdit disable action click
-
I added an icon to a
QLineEdit
using theaddAction
method but I was wondering how to remove the click behavior?
I tried to usesetEnabled(false)
to the action but it turns the icon gray.disabled:
@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 anQAction
in aQLineEdit
you can click on it, I want to disable the click behavior as it is when you usesetEnabled(false)
the problem is that it turns gray.@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 toQAction
and so I want to remove that behavior@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.
-
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 toQAction
and so I want to remove that behavior@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:
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); }
-
@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:
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).
-
@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).
@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.
-
@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).
@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:
- You can write a custom class derived from QAction, then ignore clicks. That would be a pain though.
- You could write a custom QLineEdit that draws the image (should be fairly easy, but still some work).
- 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.
- 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.
- You could just ignore the click behavior windows does
- 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".
-
Hi,
This stack overflow thread provides several possible way of doing it that could be of interest.