Can't show the border of the class inheriting QWidget class
-
wrote on 30 Jun 2023, 08:07 last edited by caio393
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.
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.
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.
-
wrote on 30 Jun 2023, 17:00 last edited by
@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.
-
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.
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.
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.
-
@caio393
The above code, which I assume you have copied & pasted, will error on linefoo = CustomWidget()
since no such class is defined.wrote on 30 Jun 2023, 08:22 last edited by@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. -
@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.wrote on 30 Jun 2023, 08:30 last edited by JonB@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 yourw
QWidget
instead of yourfoo
CustomWidget
, do you see that? -
@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 yourw
QWidget
instead of yourfoo
CustomWidget
, do you see that?wrote on 30 Jun 2023, 08:37 last edited by caio393@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. -
@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.wrote on 30 Jun 2023, 08:44 last edited by JonB@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?
-
@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?
wrote on 30 Jun 2023, 09:00 last edited by@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(); }
-
@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(); }
wrote on 30 Jun 2023, 09:12 last edited by@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 lineprint(foo.styleSheet())
to verify the stylesheet was set correctly. -
@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 lineprint(foo.styleSheet())
to verify the stylesheet was set correctly. -
wrote on 30 Jun 2023, 17:00 last edited by
@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/11