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. Border for Class inheriting QWidget and QDialog
QtWS25 Last Chance

Border for Class inheriting QWidget and QDialog

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 6 Posters 6.2k 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.
  • Pradeep KumarP Offline
    Pradeep KumarP Offline
    Pradeep Kumar
    wrote on last edited by
    #1

    Hi,

    I have a class inheriting from QWidget and QDialog,
    i require border for outer widget not to all children inside.

    How can i achieve this?.

    Thanks,

    Pradeep Kumar
    Qt,QML Developer

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

      Hi,

      Why the double inheritance ? QDialog is already a QWidget.

      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
      2
      • Pradeep KumarP Offline
        Pradeep KumarP Offline
        Pradeep Kumar
        wrote on last edited by
        #3

        Hi,

        No i have inherited from QWidget,
        sorry about question. rephrased
        i asked if we have inherited either from QDialog or QWidget, how can we have border for the main widget not to its child ?.

        if i use

        this.setStyleSheet("QWidget{border:2px solid black}");

        or

        this.setStyleSheet("QDialog{border:2px solid black}");

        it is providing border for the child widgets too, reading the docs i came to know this cant be done.

        I only want to have border for main widget, not for its children.

        Please provide guidance,

        Thanks,

        Pradeep Kumar
        Qt,QML Developer

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Just name your parent object with ->setObjectName("Pradeep") then use object name selector # in the css.

          #Pradeep {

          }

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          9
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #5

            Hi,

            I used

            this->setObjectName("hello");
            this->setWindowFlags(Qt::FramelessWindowHint);
            this->setStyleSheet("#hello{border:2px solid black}");
            

            still not working , i am not getting border for MyWidget

            Thanks,

            Pradeep Kumar
            Qt,QML Developer

            mrjjM 1 Reply Last reply
            0
            • Pradeep KumarP Pradeep Kumar

              Hi,

              I used

              this->setObjectName("hello");
              this->setWindowFlags(Qt::FramelessWindowHint);
              this->setStyleSheet("#hello{border:2px solid black}");
              

              still not working , i am not getting border for MyWidget

              Thanks,

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

              @Pradeep-Kumar said in Border for Class inheriting QWidget and QDialog:

              Hi
              Well test some more. It does work.
              If I place widget and do
              ui->widget->setObjectName("hello");
              ui->widget->setWindowFlags(Qt::FramelessWindowHint);
              ui->widget->setStyleSheet("#hello{border:2px solid black}");

              i get alt text

              So naming the widget does work :)

              1 Reply Last reply
              0
              • Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on last edited by
                #7

                Widget::Widget(QWidget *parent)
                : QWidget(parent)
                {
                this->setObjectName("hello");
                this->setWindowFlags(Qt::FramelessWindowHint);

                this->setStyleSheet(
                            "#hello{"
                            "border: 2px solid grey;"
                            "padding: 6px; }"
                            );
                

                }

                My Class Name is Widget , how to make use of it?.

                Thanks,

                Pradeep Kumar
                Qt,QML Developer

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

                  Hi,

                  IIRC:

                  "Widget{"
                  "border: 2px solid grey;"
                  "padding: 6px; }"
                  

                  I'd suggest giving your class a more sensible name (even MyWidget will do). I can already see the hair pulling debugging session because nobody notices there's a missing Q before Widget.

                  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
                  • Venkatesh VV Offline
                    Venkatesh VV Offline
                    Venkatesh V
                    wrote on last edited by
                    #9

                    Hi @Pradeep-Kumar

                    You can draw a rectangle of your screen size it can be looks like border.

                    drawRect(this->rect())

                    1 Reply Last reply
                    2
                    • Pradeep KumarP Offline
                      Pradeep KumarP Offline
                      Pradeep Kumar
                      wrote on last edited by
                      #10

                      Hi,

                      if i use paintevent to get the border for the main widget.

                      void Widget::paintEvent(QPaintEvent *event)
                      {

                      QPainter painter(this);
                      
                      QPen oMainBorderQPen;
                      oMainBorderQPen.setWidth(1);
                      oMainBorderQPen.setColor("black");
                      
                      painter.setPen(oMainBorderQPen);
                      painter.drawRect(this->rect());
                      

                      }

                      i am getting the border only for top and left , but for bottom and right i am no getting the border.

                      image is attached
                      https://i.imgsafe.org/3e800d2ed5.png

                      Any Solutions?.

                      Thanks,

                      Pradeep Kumar
                      Qt,QML Developer

                      K 1 Reply Last reply
                      0
                      • Pradeep KumarP Pradeep Kumar

                        Hi,

                        if i use paintevent to get the border for the main widget.

                        void Widget::paintEvent(QPaintEvent *event)
                        {

                        QPainter painter(this);
                        
                        QPen oMainBorderQPen;
                        oMainBorderQPen.setWidth(1);
                        oMainBorderQPen.setColor("black");
                        
                        painter.setPen(oMainBorderQPen);
                        painter.drawRect(this->rect());
                        

                        }

                        i am getting the border only for top and left , but for bottom and right i am no getting the border.

                        image is attached
                        https://i.imgsafe.org/3e800d2ed5.png

                        Any Solutions?.

                        Thanks,

                        K Offline
                        K Offline
                        kenchan
                        wrote on last edited by kenchan
                        #11

                        @Pradeep-Kumar if you set the width and height 1 pixel less you might see it. I have had to do that in the past.

                        1 Reply Last reply
                        1

                        • Login

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