Segmentation fault when using setStyleSheet
-
wrote on 16 Sept 2022, 10:14 last edited by lukutis222
Hello. I would like to understand in a little bit more detail how setStyleSheet works and why I get Segmentation faults.
For example, in the mainwindow.ui stylesheet I can paste the following
QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
And it will work fine. However, I prefer coding styles in my constructor rather than .ui.
I try many different syntax for exmple:
ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget::pane{ background-color: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget{ background: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget{ background-color: rgb(11, 33, 40)}");
All result in segmentation fault. Am I not using correct syntax to style my tab widget?
-
Hello. I would like to understand in a little bit more detail how setStyleSheet works and why I get Segmentation faults.
For example, in the mainwindow.ui stylesheet I can paste the following
QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
And it will work fine. However, I prefer coding styles in my constructor rather than .ui.
I try many different syntax for exmple:
ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget::pane{ background-color: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget{ background: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget{ background-color: rgb(11, 33, 40)}");
All result in segmentation fault. Am I not using correct syntax to style my tab widget?
@lukutis222 said in Segmentation fault when using setStyleSheet:
All result in segmentation fault. Am I not using correct syntax to style my tab widget?
Has nothing to do with syntax (in that case you would get compiler errors).
Simple question: is ui->tabWidget initialised when you're trying to call setStyleSheet on it? -
Hello. I would like to understand in a little bit more detail how setStyleSheet works and why I get Segmentation faults.
For example, in the mainwindow.ui stylesheet I can paste the following
QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
And it will work fine. However, I prefer coding styles in my constructor rather than .ui.
I try many different syntax for exmple:
ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget::pane{ background-color: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget{ background: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget{ background-color: rgb(11, 33, 40)}");
All result in segmentation fault. Am I not using correct syntax to style my tab widget?
wrote on 16 Sept 2022, 10:20 last edited by Pl45m4It looks like there is an issue wth your
ui
ortabWidget
pointer rather than a problem with your stylesheet code.
Usually if you mess up you stylesheet, the style simply wont apply, but from my experience the code still runs.Do you delete your
tabWidget
somewhere (intentionally or not) before you apply the style?@jsulm Sry :) Took to long to write :o)
-
Hello. I would like to understand in a little bit more detail how setStyleSheet works and why I get Segmentation faults.
For example, in the mainwindow.ui stylesheet I can paste the following
QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
And it will work fine. However, I prefer coding styles in my constructor rather than .ui.
I try many different syntax for exmple:
ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget::pane{ background-color: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget{ background: rgb(11, 33, 40)}");
or
ui->tabWidget->setStyleSheet("QTabWidget{ background-color: rgb(11, 33, 40)}");
All result in segmentation fault. Am I not using correct syntax to style my tab widget?
wrote on 16 Sept 2022, 10:26 last edited by JonB@lukutis222
And further to the above (correct) replies. Whenever you have a "segmentation fault" (or other similar) you should be running your program under the debugger. That will break on the fault. You then look in the stack trace pane to see where this emanated from in your code, perhaps examining variables, and you should have a good clue. For example, this would show you if indeed yourui->tabWidget
is null or invalid. -
put the :
ui->tabWidget->setStyleSheet....after the
ui->setupUi(this);
call. -
wrote on 16 Sept 2022, 11:09 last edited by lukutis222
Thanks all for quick responses.
I do not destroy or delete that tab widget anywhere. The structure of my widgets are as following:I have tried to call the setStyleSheet after setuipUI and that fixed my segmentation fault.. However, I would still like to understand about this QTabWidget and why no color is applied.
I can do:
ui->tab_3->setStyleSheet("QWidget { background-color: rgb(11, 33, 40);border: 1px solid white;}");
And the color will be applied (blackish color):
When I do:
ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}");
-
Thanks all for quick responses.
I do not destroy or delete that tab widget anywhere. The structure of my widgets are as following:I have tried to call the setStyleSheet after setuipUI and that fixed my segmentation fault.. However, I would still like to understand about this QTabWidget and why no color is applied.
I can do:
ui->tab_3->setStyleSheet("QWidget { background-color: rgb(11, 33, 40);border: 1px solid white;}");
And the color will be applied (blackish color):
When I do:
ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}");
Nothing is styled:
wrote on 16 Sept 2022, 11:12 last edited by JonB@lukutis222 said in Segmentation fault when using setStyleSheet:
ui->tab_3->setStyleSheet("QWidget { background-color: rgb(11, 33, 40);border: 1px solid white;}");
ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}");
The first styles all
QWidget
s, the second onlyQTabWidget
. Is that what you are testing? I trust you are copying these statements from actual code. -
Thanks all for quick responses.
I do not destroy or delete that tab widget anywhere. The structure of my widgets are as following:I have tried to call the setStyleSheet after setuipUI and that fixed my segmentation fault.. However, I would still like to understand about this QTabWidget and why no color is applied.
I can do:
ui->tab_3->setStyleSheet("QWidget { background-color: rgb(11, 33, 40);border: 1px solid white;}");
And the color will be applied (blackish color):
When I do:
ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}");
Nothing is styled:
@lukutis222 I guess you need to set the color on QTabWidget::pane like shown here: https://doc.qt.io/qt-6/stylesheet-examples.html
-
@lukutis222 said in Segmentation fault when using setStyleSheet:
ui->tab_3->setStyleSheet("QWidget { background-color: rgb(11, 33, 40);border: 1px solid white;}");
ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}");
The first styles all
QWidget
s, the second onlyQTabWidget
. Is that what you are testing? I trust you are copying these statements from actual code.wrote on 16 Sept 2022, 11:14 last edited by@JonB
Yes. I would expect to see my whole tab in black color ( similar to when I color the whole widget in black color) -
@lukutis222 I guess you need to set the color on QTabWidget::pane like shown here: https://doc.qt.io/qt-6/stylesheet-examples.html
wrote on 16 Sept 2022, 11:15 last edited by@jsulm
Yes but how can I do that in code rather than UI?
in UI I use/* * QTabWidget::pane { border: none; background: rgb(25, 33, 40); } */
But how can I add pane in code?
ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(11, 33, 40)}");
I tried below and that changes nothing
-
@jsulm
Yes but how can I do that in code rather than UI?
in UI I use/* * QTabWidget::pane { border: none; background: rgb(25, 33, 40); } */
But how can I add pane in code?
ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(11, 33, 40)}");
I tried below and that changes nothing
wrote on 16 Sept 2022, 11:22 last edited by JonB@lukutis222
I don't understand. If you say "in UI I use" (and I assume you mean it works)QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
then in code you can use
setStyleSheet("QTabWidget::pane { border: none; background-color: rgb(25, 33, 40); }")
. You can even look in the generatedui_....h
file to see exactly what statement is generated for what object where from the QSS you type in. -
@lukutis222
I don't understand. If you say "in UI I use" (and I assume you mean it works)QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
then in code you can use
setStyleSheet("QTabWidget::pane { border: none; background-color: rgb(25, 33, 40); }")
. You can even look in the generatedui_....h
file to see exactly what statement is generated for what object where from the QSS you type in.wrote on 16 Sept 2022, 11:38 last edited by lukutis222@JonB
When I go to my mainwindow.ui, right click the application window -> Change styleSheet and input the following:QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
I checked the style file:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout_3"> <item> <widget class="QTabWidget" name="tabWidget"> <property name="styleSheet"> <string notr="true"> QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
I cannot wrap my head around what is wrong with this and why the style does not apply
ui->setupUi(this); ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}");
-
@JonB
When I go to my mainwindow.ui, right click the application window -> Change styleSheet and input the following:QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
The style applies:
I checked the style file:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout_3"> <item> <widget class="QTabWidget" name="tabWidget"> <property name="styleSheet"> <string notr="true"> QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
I cannot wrap my head around what is wrong with this and why the style does not apply
ui->setupUi(this); ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}");
wrote on 16 Sept 2022, 11:41 last edited by@lukutis222 said in Segmentation fault when using setStyleSheet:
QTabWidget::pane {
ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}");
But now you have gone back to not specifying
QTabWidget::pane
in your code version. It's really hard to keep up if these things are not consistent.... -
@JonB
When I go to my mainwindow.ui, right click the application window -> Change styleSheet and input the following:QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
The style applies:
I checked the style file:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout_3"> <item> <widget class="QTabWidget" name="tabWidget"> <property name="styleSheet"> <string notr="true"> QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
I cannot wrap my head around what is wrong with this and why the style does not apply
ui->setupUi(this); ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}");
@lukutis222 And did you also check the generated ui_*.h file like @JonB suggested to see how the style-sheet is set there?
-
@lukutis222 said in Segmentation fault when using setStyleSheet:
QTabWidget::pane {
ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}");
But now you have gone back to not specifying
QTabWidget::pane
in your code version. It's really hard to keep up if these things are not consistent....wrote on 16 Sept 2022, 11:42 last edited byBoth are not working. I can even call:
ui->setupUi(this); ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}"); ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(11, 33, 40)}");
-
@lukutis222 And did you also check the generated ui_*.h file like @JonB suggested to see how the style-sheet is set there?
wrote on 16 Sept 2022, 11:46 last edited by lukutis222@jsulm I assume you are talking about this
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout_3"> <item> <widget class="QTabWidget" name="tabWidget"> <property name="styleSheet"> <string notr="true">QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
The above is when I edit style sheet in UI instead of code which seems identical to what I am calling in code and it does not work
ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(25, 33, 40)}");
-
Both are not working. I can even call:
ui->setupUi(this); ui->tabWidget->setStyleSheet("QTabWidget { background-color: rgb(11, 33, 40)}"); ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(11, 33, 40)}");
wrote on 16 Sept 2022, 11:48 last edited by-
Start by using the same copied stuff between the two examples. Don't (at this stage) have, for example, one specifying
border
and compare against the other not specifying border, until you have got both working consistently. -
The stuff done in the Designer does not work "by magic". The
.ui
file is not read at runtime. Theuic
generates theui_....h
file, the C++ code includes that and callssetupUi()
. Logic tells you that you must be able to achieve the same in your "manual" code. So you must be able to reproduce. You have some subtle difference somewhere in what you are coding versus what happens from the Designer stuff, so start by getting them identical before you allow any differences.
-
-
@jsulm I assume you are talking about this
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout_3"> <item> <widget class="QTabWidget" name="tabWidget"> <property name="styleSheet"> <string notr="true">QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
The above is when I edit style sheet in UI instead of code which seems identical to what I am calling in code and it does not work
ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(25, 33, 40)}");
@lukutis222 said in Segmentation fault when using setStyleSheet:
I assume you are talking about this
No, I'm not.
"generated ui_*.h file" - has nothing to do with XML file you're showing... -
@lukutis222 said in Segmentation fault when using setStyleSheet:
I assume you are talking about this
No, I'm not.
"generated ui_*.h file" - has nothing to do with XML file you're showing...wrote on 16 Sept 2022, 11:48 last edited by@jsulm Can you explain where can I find that magic file you are talking about, because I cannot see it in my project file list
-
@jsulm I assume you are talking about this
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout_3"> <item> <widget class="QTabWidget" name="tabWidget"> <property name="styleSheet"> <string notr="true">QTabWidget::pane { border: none; background: rgb(25, 33, 40); }
The above is when I edit style sheet in UI instead of code which seems identical to what I am calling in code and it does not work
ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(25, 33, 40)}");
wrote on 16 Sept 2022, 11:49 last edited by@lukutis222 said in Segmentation fault when using setStyleSheet:
ui->tabWidget->setStyleSheet("QTabWidget::pane { background-color: rgb(25, 33, 40)}");
Yet again: start by copying what you have working in the
.ui
, not assuming that theborder: none;
is irrelevant. How hard can it be to copy? Maybe this is irrelevant, but just maybe it is significant, why take the risk?
1/22