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. Focus Rectangle paintEvent
QtWS25 Last Chance

Focus Rectangle paintEvent

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5.12.xpainteeventbeginner
8 Posts 2 Posters 1.4k Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi! I'm trying to make my application draw a focus rectangle (the blue border around ex. QLineEdits). The painting itself works, but the style of the drawn focusRect doesn't match at all. What I get instead is a dotted line, akin to the ones you see in a QTableWidget when selecting an item. How do I access and draw it using the style used by default of other widgets in the application?

    FocusFrame::FocusFrame(QWidget *parent) : QFrame(parent)
    {
    }
    
    void FocusFrame::paintEvent(QPaintEvent *)
    {
            QPainter painter(this);
            QStyleOptionFocusRect option;
            option.initFrom(this); //I thought this would give it the default appearance of focus rectangles
            
            style()->drawPrimitive(QStyle::PE_FrameFocusRect, &option, &painter, this);
    }
    

    Thanks in advance!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Wouldn't QRubberBand do what you want ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        It kind of works, though I can't find any way to fix the blue background. Also, will the QRubberBand style always match the focusFrame style independent of OS?

        Desired look:
        Capture.PNG

        Current (with option.backgroundColor = yellow just to show the issue more clearly):
        Capture2.PNG

        Rubberband attempt:
        Capture3.PNG

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can set the shape of the QRubberBand to line. That said, which OS are you running ?
          Usually it is in charge of handling the focus rect around the widget that you are currently editing or about to activate with the entrer key in the case of a button.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          ? 1 Reply Last reply
          0
          • SGaistS SGaist

            You can set the shape of the QRubberBand to line. That said, which OS are you running ?
            Usually it is in charge of handling the focus rect around the widget that you are currently editing or about to activate with the entrer key in the case of a button.

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #5

            @SGaist

            Tried setting it to line, but the background still gets filled and appearance doesn't differ from rectangle at all.

            I'm currently using Windows, but the aim of this is to get my "custom" focus rectangle to mirror the appearance of the ones of QLineEdit on at least Windows, Mac and Linux. If there was a way to ask Qt for the focus style and use that to draw, that'd be perfect - but maybe there's an easier solution?

            To clarify: I've got 4 QLineEdits and when focusing any of them, I want "frame" to show as focused (without moving focus from the current QLineEdit.

                layout = new QHBoxLayout(frame);
                layout->addWidget(textIP1);
                layout->addWidget(labelDot1);
                layout->addWidget(textIP2);
                layout->addWidget(labelDot2);
                layout->addWidget(textIP3);
                layout->addWidget(labelDot3);
                layout->addWidget(textIP4);
            

            If there's some kind of way to get frame to show itself as focused without manually drawing a focus frame, that'd solve the issue as well.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              A simpler way would be to use a style sheet:

              QLineEdit:focus
              {
                  border: 2px solid #000080;
              }
              

              Note that not all desktop environment uses focus rectangle by default so your application style may go against the platform guidelines which your users might not appreciate.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              ? 1 Reply Last reply
              0
              • SGaistS SGaist

                A simpler way would be to use a style sheet:

                QLineEdit:focus
                {
                    border: 2px solid #000080;
                }
                

                Note that not all desktop environment uses focus rectangle by default so your application style may go against the platform guidelines which your users might not appreciate.

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                @SGaist said in Focus Rectangle paintEvent:

                A simpler way would be to use a style sheet:

                QLineEdit:focus
                {
                    border: 2px solid #000080;
                }
                

                Note that not all desktop environment uses focus rectangle by default so your application style may go against the platform guidelines which your users might not appreciate.

                Stylesheet sadly wouldn't work due to it applying to textIP1-4. It would also have the issue of not following the style of the desktop environment.

                Is there no way to properly make the custom widget follow the styling independently of environment style? My main goal is to make "frame" indistinguishable from a regular QLineEdit.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Did you consider using a proxy style ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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