Important: Please read the Qt Code of Conduct -

How to set border on a widget

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

  • Moderators

    The easiest would be to base your widget on QFrame, not QWidget and set the "frameShape":

    Another option is to use qss like this:
    yourWidget->setStyleSheet("border: 1px solid red");

  • 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);



    Hope this helps.

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

Log in to reply