Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Focus Rectangle paintEvent

    General and Desktop
    qt5.12.x painteevent beginner
    2
    8
    255
    Loading More Posts
    • 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.
    • B
      Bopet last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • B
          Bopet last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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

            B 1 Reply Last reply Reply Quote 0
            • B
              Bopet @SGaist last edited by Bopet

              @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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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

                B 1 Reply Last reply Reply Quote 0
                • B
                  Bopet @SGaist last edited by

                  @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 Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    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 Reply Quote 0
                    • First post
                      Last post