Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Can't show the border of the class inheriting QWidget class

Can't show the border of the class inheriting QWidget class

Scheduled Pinned Locked Moved Solved Qt for Python
11 Posts 3 Posters 1.7k 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.
  • C Offline
    C Offline
    caio393
    wrote on 30 Jun 2023, 08:07 last edited by caio393
    #1

    I have created a custom widget (inherits QWidget class) to draw a rectangle in the center of its zone. I add it into QVBoxLayout before setting the layout for the main window (the QWidget object). Firstly, I use Qt C++ to create my custom widget. And I used

    this->setStyleSheet("border: 1px solid red")
    

    to show its border (I used that line in the initialization method of my custom widget). It worked well with the thin red line border on the screen like the image below.
    QtC.png
    But when I switched to Python (using PySide6), the border didn't show. Weird that when I create a QWidget object and use that code, it shows the border normally.
    QtPython_QWidget_Object.png QtPython_Custom_QWidget.png
    I also tried creating only my custom widget and displaying it as below, but it didn't work (working normally on Qt C++).

    w = CustomWidget()
    w.show()
    

    Here is my Python code

    from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget
    import sys
    
    class CustomWidget(QWidget):
        def __init__(self):
            super().__init__()
            self.setStyleSheet("border: 1px solid red")
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
    
        foo = CustomWidget()
        
        layout = QVBoxLayout()
        layout.addWidget(foo)
    
        w = QWidget()
        w.setLayout(layout)
        w.show()
    
        sys.exit(app.exec())
    
    

    Please show me what happening. Thank you.

    J 1 Reply Last reply 30 Jun 2023, 08:16
    0
    • C Offline
      C Offline
      caio393
      wrote on 30 Jun 2023, 17:00 last edited by
      #11

      @JoeCFD Yes, I have read the post at https://forum.qt.io/topic/29555/how-to-set-border-on-a-widget and it suggests using QFrame or styleSheet. And when I use Qt C++, both two suggestions work fine. However, Python does not. That makes me feel confused. I can't understand why my Qt C++ code can work well but both PySide6 and PyQt6 can not. Therefore, I think I was wrong somewhere and I posted it to find help. But that may be the difference between the two languages.

      1 Reply Last reply
      0
      • C caio393
        30 Jun 2023, 08:07

        I have created a custom widget (inherits QWidget class) to draw a rectangle in the center of its zone. I add it into QVBoxLayout before setting the layout for the main window (the QWidget object). Firstly, I use Qt C++ to create my custom widget. And I used

        this->setStyleSheet("border: 1px solid red")
        

        to show its border (I used that line in the initialization method of my custom widget). It worked well with the thin red line border on the screen like the image below.
        QtC.png
        But when I switched to Python (using PySide6), the border didn't show. Weird that when I create a QWidget object and use that code, it shows the border normally.
        QtPython_QWidget_Object.png QtPython_Custom_QWidget.png
        I also tried creating only my custom widget and displaying it as below, but it didn't work (working normally on Qt C++).

        w = CustomWidget()
        w.show()
        

        Here is my Python code

        from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget
        import sys
        
        class CustomWidget(QWidget):
            def __init__(self):
                super().__init__()
                self.setStyleSheet("border: 1px solid red")
        
        if __name__ == "__main__":
            app = QApplication(sys.argv)
        
            foo = CustomWidget()
            
            layout = QVBoxLayout()
            layout.addWidget(foo)
        
            w = QWidget()
            w.setLayout(layout)
            w.show()
        
            sys.exit(app.exec())
        
        

        Please show me what happening. Thank you.

        J Offline
        J Offline
        JonB
        wrote on 30 Jun 2023, 08:16 last edited by
        #2

        @caio393
        The above code, which I assume you have copied & pasted, will error on line foo = CustomWidget() since no such class is defined.

        C 1 Reply Last reply 30 Jun 2023, 08:22
        0
        • J JonB
          30 Jun 2023, 08:16

          @caio393
          The above code, which I assume you have copied & pasted, will error on line foo = CustomWidget() since no such class is defined.

          C Offline
          C Offline
          caio393
          wrote on 30 Jun 2023, 08:22 last edited by
          #3

          @JonB Oh, sorry. It's just my typo :)))
          I want my class's name to be "CustomWidget", but in my code is "CustomeWidget". Therefore, when I copy & paste, I see it's weird. I rename it to "CustomWidget" but I forgot to change the class definition.

          J 1 Reply Last reply 30 Jun 2023, 08:30
          0
          • C caio393
            30 Jun 2023, 08:22

            @JonB Oh, sorry. It's just my typo :)))
            I want my class's name to be "CustomWidget", but in my code is "CustomeWidget". Therefore, when I copy & paste, I see it's weird. I rename it to "CustomWidget" but I forgot to change the class definition.

            J Offline
            J Offline
            JonB
            wrote on 30 Jun 2023, 08:30 last edited by JonB
            #4

            @caio393
            Please copy & paste code, not "type" it, we waste a lot of time on posts which purport to show actual code but do not....!

            I do not have Qt6 or PySide6, but I find it "hard to believe" that would fail to show your red border while the exactly corresponding C++ program does.

            Try something else like self.setStyleSheet("background-color: blue"). Do you not see that? Also try that on your w QWidget instead of your foo CustomWidget, do you see that?

            C 1 Reply Last reply 30 Jun 2023, 08:37
            0
            • J JonB
              30 Jun 2023, 08:30

              @caio393
              Please copy & paste code, not "type" it, we waste a lot of time on posts which purport to show actual code but do not....!

              I do not have Qt6 or PySide6, but I find it "hard to believe" that would fail to show your red border while the exactly corresponding C++ program does.

              Try something else like self.setStyleSheet("background-color: blue"). Do you not see that? Also try that on your w QWidget instead of your foo CustomWidget, do you see that?

              C Offline
              C Offline
              caio393
              wrote on 30 Jun 2023, 08:37 last edited by caio393
              #5

              @JonB My custom widget only changes the background color, not the border. I tried it before posting it to the forum. The QWidget object still works fine.
              P/s: I know it is hard to believe, but it is true. I tried many ways to fix it before I post it on the forum. I'm so sorry if I made you feel uncomfortable.

              J 1 Reply Last reply 30 Jun 2023, 08:44
              0
              • C caio393
                30 Jun 2023, 08:37

                @JonB My custom widget only changes the background color, not the border. I tried it before posting it to the forum. The QWidget object still works fine.
                P/s: I know it is hard to believe, but it is true. I tried many ways to fix it before I post it on the forum. I'm so sorry if I made you feel uncomfortable.

                J Offline
                J Offline
                JonB
                wrote on 30 Jun 2023, 08:44 last edited by JonB
                #6

                @caio393 said in Can't show the border of the class inheriting QWidget class:

                I'm so sorry if I made you feel uncomfortable.

                ? Not at all!

                My custom widget only changes the background color, not the border.

                You show your code doing

                self.setStyleSheet("border: 1px solid red")
                

                Now I don't understand what your code actually, really does....

                I guess it's possible that there is a PySide6 issue here, though it would be surprising. Are you able to test your code against PyQt6 instead?

                C 1 Reply Last reply 30 Jun 2023, 09:00
                0
                • J JonB
                  30 Jun 2023, 08:44

                  @caio393 said in Can't show the border of the class inheriting QWidget class:

                  I'm so sorry if I made you feel uncomfortable.

                  ? Not at all!

                  My custom widget only changes the background color, not the border.

                  You show your code doing

                  self.setStyleSheet("border: 1px solid red")
                  

                  Now I don't understand what your code actually, really does....

                  I guess it's possible that there is a PySide6 issue here, though it would be surprising. Are you able to test your code against PyQt6 instead?

                  C Offline
                  C Offline
                  caio393
                  wrote on 30 Jun 2023, 09:00 last edited by
                  #7

                  @JonB I tried it. The result is the same as the PySide6
                  May my code is have problem?
                  Here is my Qt C++ (it works well)

                  #include <QApplication>
                  #include <QWidget>
                  #include <QVBoxLayout>
                  
                  class CustomWidget: public QWidget
                  {
                  public:
                      CustomWidget()
                      {
                          this->setStyleSheet("border: 1px solid red");
                      }
                  };
                  
                  int main(int argc, char* argv[]) {
                      QApplication *app = new QApplication(argc, argv);
                  
                      CustomWidget *custom = new CustomWidget;
                  
                      QVBoxLayout *layout = new QVBoxLayout();
                          layout->addWidget(custom);
                  
                      QWidget *w = new QWidget;
                      w->setLayout(layout);
                      w->show();
                  
                      return app->exec();
                  }
                  
                  
                  J 1 Reply Last reply 30 Jun 2023, 09:12
                  0
                  • C caio393
                    30 Jun 2023, 09:00

                    @JonB I tried it. The result is the same as the PySide6
                    May my code is have problem?
                    Here is my Qt C++ (it works well)

                    #include <QApplication>
                    #include <QWidget>
                    #include <QVBoxLayout>
                    
                    class CustomWidget: public QWidget
                    {
                    public:
                        CustomWidget()
                        {
                            this->setStyleSheet("border: 1px solid red");
                        }
                    };
                    
                    int main(int argc, char* argv[]) {
                        QApplication *app = new QApplication(argc, argv);
                    
                        CustomWidget *custom = new CustomWidget;
                    
                        QVBoxLayout *layout = new QVBoxLayout();
                            layout->addWidget(custom);
                    
                        QWidget *w = new QWidget;
                        w->setLayout(layout);
                        w->show();
                    
                        return app->exec();
                    }
                    
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 30 Jun 2023, 09:12 last edited by
                    #8

                    @caio393
                    I am at a loss to understand how both PyQt6 and PySide6 fail where your C++ succeeds. Your Python code looks fine to me as equivalent to the C++.

                    In your Python code, let's make sure: after the w.show() append line print(foo.styleSheet()) to verify the stylesheet was set correctly.

                    C 1 Reply Last reply 30 Jun 2023, 09:17
                    0
                    • J JonB
                      30 Jun 2023, 09:12

                      @caio393
                      I am at a loss to understand how both PyQt6 and PySide6 fail where your C++ succeeds. Your Python code looks fine to me as equivalent to the C++.

                      In your Python code, let's make sure: after the w.show() append line print(foo.styleSheet()) to verify the stylesheet was set correctly.

                      C Offline
                      C Offline
                      caio393
                      wrote on 30 Jun 2023, 09:17 last edited by
                      #9

                      @JonB I can't understand too.
                      It shows exactly what I set up. Too weird.
                      bug.png

                      JoeCFDJ 1 Reply Last reply 30 Jun 2023, 15:18
                      0
                      • C caio393
                        30 Jun 2023, 09:17

                        @JonB I can't understand too.
                        It shows exactly what I set up. Too weird.
                        bug.png

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on 30 Jun 2023, 15:18 last edited by
                        #10

                        @caio393 Use QFrame( or QLabel) as suggested by Chris Kawa before. You may not be able to set a border with stylesheet on QWidget.

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          caio393
                          wrote on 30 Jun 2023, 17:00 last edited by
                          #11

                          @JoeCFD Yes, I have read the post at https://forum.qt.io/topic/29555/how-to-set-border-on-a-widget and it suggests using QFrame or styleSheet. And when I use Qt C++, both two suggestions work fine. However, Python does not. That makes me feel confused. I can't understand why my Qt C++ code can work well but both PySide6 and PyQt6 can not. Therefore, I think I was wrong somewhere and I posted it to find help. But that may be the difference between the two languages.

                          1 Reply Last reply
                          0
                          • C caio393 has marked this topic as solved on 30 Jun 2023, 17:03

                          1/11

                          30 Jun 2023, 08:07

                          • Login

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