Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Draw a QFrame-like rectangle
Qt 6.12 LTS is out! See what's new in the release blog

Draw a QFrame-like rectangle

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 3.4k 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
    Harry123
    wrote on last edited by
    #1

    I am trying to draw a rectangle that is QFrame-like with 3D border, but I only ever get a simple 1-pixel rectangle.

    When testing the simplest case, that of a box with a 10-pixels border, my simplified test-code :

    void Win::paintEvent(QPaintEvent * event)
    {
      QWidget::paintEvent(event);
      QPainter painter(this);
      QStyleOptionFrame  option;
      option.initFrom(this);
      option.rect = QRect(100,100,100,100);
      option.lineWidth = 10;
      option.frameShape = QFrame::Box;
      style()->drawPrimitive(QStyle::PE_Frame,  &option,  &painter,  this);
    }
    

    The above draws a 1-pixel rectangle. If I replace the drawPrimitive call with :

    painter.setPen(QPen(Qt::red, 10));
    painter.drawRect(QRect(100,100,100,100));
    

    then the rectangle is correct.

    What am I doing wrong ?

    mrjjM 1 Reply Last reply
    0
    • H Harry123

      I am trying to draw a rectangle that is QFrame-like with 3D border, but I only ever get a simple 1-pixel rectangle.

      When testing the simplest case, that of a box with a 10-pixels border, my simplified test-code :

      void Win::paintEvent(QPaintEvent * event)
      {
        QWidget::paintEvent(event);
        QPainter painter(this);
        QStyleOptionFrame  option;
        option.initFrom(this);
        option.rect = QRect(100,100,100,100);
        option.lineWidth = 10;
        option.frameShape = QFrame::Box;
        style()->drawPrimitive(QStyle::PE_Frame,  &option,  &painter,  this);
      }
      

      The above draws a 1-pixel rectangle. If I replace the drawPrimitive call with :

      painter.setPen(QPen(Qt::red, 10));
      painter.drawRect(QRect(100,100,100,100));
      

      then the rectangle is correct.

      What am I doing wrong ?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Might need
      Option.state = QStyle::State_Sunken; // or what ever
      Since values might be random (not all set by initFrom) ,
      you must set all used for a PE_X style.

      If still not working, check out QFrame source for what it really needs.

      This works for me. ( for line)

             QStyleOptionFrame option;
              option.initFrom(this);
              option.state = QStyle::State_Sunken;
              option.frameShape = QFrame::HLine;
              style()->drawPrimitive(QStyle::PE_Frame, &option, &painter, this);
      
      1 Reply Last reply
      0
      • H Offline
        H Offline
        Harry123
        wrote on last edited by
        #3

        @mrjj :

        I have tried to assign every possible documented member variable, but no go.
        The code now looks like :

        void Win::paintEvent(QPaintEvent * event)
        {
          QWidget::paintEvent(event);
          QPainter painter(this);
          QStyleOptionFrame  option;
          option.initFrom(this);
          option.rect = QRect(100,100,100,100);
          option.lineWidth = 10;
          option.frameShape = QFrame::Box;
          option.state = QStyle::State_Sunken;
          option.direction = Qt::LeftToRight;
          option.features = QStyleOptionFrame::Rounded;
          option.fontMetrics = QFontMetrics(QFont("Arial"));
          option.midLineWidth = 4;
          option.type = QStyleOption::SO_Frame;
          style()->drawPrimitive(QStyle::PE_Frame,  &option,  &painter,  this);
        }
        

        I have also tried to take out the call to option.initFrom(this);, but this changed nothing.
        I have even changed option.frameShape = QFrame::HLine;, but I still get a 1-pixel rectangle/line.

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

          You are using the wrong drawing function and element. It should be

          style()->drawControl(QStyle::CE_ShapedFrame, &option, &painter, this);
          
          1 Reply Last reply
          3
          • H Offline
            H Offline
            Harry123
            wrote on last edited by Harry123
            #5

            @Chris-Kawa :

            YES! This was it.
            Unfortunate that drawPrimitive was wrongly recommended so many times on StackOverflow and elsewhere.
            Many thanks.

            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
            • Unsolved