Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Highlight QAction
Forum Updated to NodeBB v4.3 + New Features

Highlight QAction

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 499 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    I am creating a find/replace functionality using two QLineEdits. I want to add the case-sensitive and regex feature as well.
    I want to achive the ui similar to the one in vscode:
    01161f5c-71f5-49ad-8820-bbe37d3f9291-image.png

    What I have so far:
    752ac49b-e610-4324-8707-ba3683929b98-image.png

    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);
    
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      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;
      }
      
      H 1 Reply Last reply
      2
      • Chris KawaC Chris Kawa

        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;
        }
        
        H Offline
        H Offline
        hbatalha
        wrote on last edited by
        #3

        @Chris-Kawa How do I add QToolButton to QLineEdit?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          H 1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

            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.

            H Offline
            H Offline
            hbatalha
            wrote on last edited by
            #5

            @Chris-Kawa is there a way I can highlight it like in vscode?
            01161f5c-71f5-49ad-8820-bbe37d3f9291-image.png

            Chris KawaC 1 Reply Last reply
            0
            • H hbatalha

              @Chris-Kawa is there a way I can highlight it like in vscode?
              01161f5c-71f5-49ad-8820-bbe37d3f9291-image.png

              Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @hbatalha I gave you code for that.

              H 1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                @hbatalha I gave you code for that.

                H Offline
                H Offline
                hbatalha
                wrote on last edited by
                #7

                @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!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved