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. How to set border on a widget
Forum Updated to NodeBB v4.3 + New Features

How to set border on a widget

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 91.0k Views 1 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.
  • U Offline
    U Offline
    user001
    wrote on last edited by
    #1

    I have a QWidget class which will be added to a mainWindow. There will be some buttons, label on this widget. I want to have a border on it and to let user know the area of this widget. Searched online and cannot find a clear and direct answer. anybody here can help me with this? Or the border belongs to layout?

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

      The easiest would be to base your widget on QFrame, not QWidget and set the "frameShape":http://qt-project.org/doc/qt-5.0/qtwidgets/qframe.html#frameShape-prop

      Another option is to use qss like this:

      yourWidget->setStyleSheet("border: 1px solid red");
      
      1 Reply Last reply
      1
      • musimbateM Offline
        musimbateM Offline
        musimbate
        wrote on last edited by Chris Kawa
        #3

        Hi,
        Have had the same problem some time ago and all my search lead to nothing,so I got creative.I inherited QWidget and in the paint event I drew the rectangle to act as my border.The next chunk of code should speak better:

        void mCustomWidget::paintEvent(QPaintEvent *e)
        {
            QPainter painter(this);
        
            painter.drawRoundedRect(0,5,width()-5, height()-7,3,3);
        
            QWidget::paintEvent(e);
        }
        

        Hope this helps.

        Why join the navy if you can be a pirate?-Steve Jobs

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          The QFrame is the easy option. Layouts aren't used for borders etc, only for arranging widgets inside parent widget. If a label/title is needed, the QGroupBox is the way to go.

          Greetz, Jeroen

          1 Reply Last reply
          0
          • C caio393 referenced this topic on

          • Login

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