Highlight QAction
-
I am creating a find/replace functionality using two
QLineEdit
s. I want to add the case-sensitive and regex feature as well.
I want to achive the ui similar to the one in vscode:
What I have so far:
My code:
QAction* caseSentiveAction = new QAction(QIcon(QPixmap(":/Icons/case-sentive.png")), ""); caseSentiveAction->setCheckable(true); connect(caseSentiveAction, &QAction::toggled, this, [this](bool checked) { // toggle highlight }); QAction* regexAction = new QAction(QIcon(QPixmap(":/Icons/regex.png")), ""); ui->remove_lineEdit->setPlaceholderText(tr("What to remove from the filenames")); ui->remove_lineEdit->addAction(regexAction, QLineEdit::TrailingPosition); ui->remove_lineEdit->addAction(caseSentiveAction, QLineEdit::TrailingPosition);
-
Unfortunately the built-in control does not draw checkable actions by itself, but you can add that yourself.
After you add the actions find the created buttons and install an event filter on them. In the event filter check for paint event and paint the "checked" background yourself before letting the button draw itself on top of that.So, after you add actions something like this:
for (QToolButton* btn : lineEdit->findChildren<QToolButton*>()) { btn->installEventFilter(something); }
and in the event filter method:
bool Something::eventFilter(QObject* obj, QEvent* evt) { if (evt->type() == QEvent::Type::Paint) { QToolButton* btn = qobject_cast<QToolButton*>(obj); if (btn && btn->defaultAction()->isChecked()) { QPainter p(btn); p.fillRect(btn->rect(), btn->palette().color(QPalette::Active, QPalette::Highlight)); } } return false; }
-
Unfortunately the built-in control does not draw checkable actions by itself, but you can add that yourself.
After you add the actions find the created buttons and install an event filter on them. In the event filter check for paint event and paint the "checked" background yourself before letting the button draw itself on top of that.So, after you add actions something like this:
for (QToolButton* btn : lineEdit->findChildren<QToolButton*>()) { btn->installEventFilter(something); }
and in the event filter method:
bool Something::eventFilter(QObject* obj, QEvent* evt) { if (evt->type() == QEvent::Type::Paint) { QToolButton* btn = qobject_cast<QToolButton*>(obj); if (btn && btn->defaultAction()->isChecked()) { QPainter p(btn); p.fillRect(btn->rect(), btn->palette().color(QPalette::Active, QPalette::Highlight)); } } return false; }
@Chris-Kawa How do I add
QToolButton
toQLineEdit
? -
You don't have to. When you're adding an action to it it creates the tool button for you and sets the action as the button's defaultAction.
-
You don't have to. When you're adding an action to it it creates the tool button for you and sets the action as the button's defaultAction.
@Chris-Kawa is there a way I can highlight it like in vscode?
-
@Chris-Kawa is there a way I can highlight it like in vscode?
@hbatalha I gave you code for that.
-
@hbatalha I gave you code for that.
@Chris-Kawa sorry, the code was confusing for me at first but now that I paid more attention I am able to understand it. Thank you!