Qt: Weird Behaviour when removing outline from QTableView - c++

I'm trying to remove the dotted outline, when an item in a QTableView is focused. I found in other answers, that outline: 0 will remove it, but i for me, that is only working in weird cases:
With this Stylesheet, the outline is removed and the background is yellow:
tableView->setStyleSheet("QTableView::item::focus { outline: 0; background-color: yellow; } QTableView { outline: 0; }");
With this Stylesheet the outline is still shown:
tableView->setStyleSheet("QTableView::item::focus { outline: 0; } QTableView { outline: 0; }");
tableView->setStyleSheet("QTableView { outline: 0; }");
tableView->setStyleSheet("QTableView::item::focus { outline: 0; }");
tableView->setStyleSheet("QTableView::item::focus { outline: 0; background-color: yellow; }");
I want to have the outline removed and the background in its default color. But it seems, it's not having the option to. The outline command only works in specific cases, which feel very random to me.
Is this an error from me, in misunderstanding Qt or Stylesheets. Or is this a known bug, or a new bug?
Edit (images for understanding what i mean):
This is the default behaviour. See the outline around the element 1-2!
This is with the style QTableView::item::focus { outline: 0; background-color: yellow; } QTableView { outline: 0; } added. Now the ouline is not shown, but the background of the focused object is yellow:
This is with the 4 variants, that should work, but the outline is still shown (outline is around the object in position 1-2):
The thing is, i don't understand, why it works in exacly one specific way and not in the other ones?
And what do i have to do, to remove the outline without changing the background?

Related

How to change the background color of the QTableWidget's vertical header?

I would like to change the colors of a QTableWidget. I am almost getting the result I like, but two areas in the vertical header remain white:
Before writing this post I actually managed to color also the upper left corner, but not the other area. Here is the stylesheet I am using:
QTableCornerButton::section {
background-color: #8b8d8e;
}
QHeaderView::section {
color: white;
background-color: #747678;
gridline-color: #747678;
}
Finally found the answer myself:
/*The top-left area is actually a button:*/
QTableCornerButton::section {
background-color: #8b8d8e;
}
/*The lower part of the vertical header:*/
QHeaderView {
background-color: #8b8d8e;
}
The original css I posted (QHeaderView::section) referred only to the header entries, not the header itself.

How to set the QTreeWidget's header color and hide the dividing line?

I want to change the background color of the header when I use the QWidget. I tried the following methods but they didn't work:
QTreeWidgetItem * header = ui->treeWidget->headerItem();
header->setBackground(0, QBrush(QColor(185,192,201)));
header->setBackgroundColor(0, QColor(185,192,201));
setStyleSheet("QHeaderView::section { background-color:red }");
I also want to know how to hide the header's dividing line?
I've found a method to change the header's style,but I don't know why my previous method did not work.
QHeaderView::section {
color: black;
padding: 2px;
height:20px;
border: 0px solid #567dbc;
border-left:0px;
border-right:0px;
background: #f9f9f9;
}
Why don't you use only the stylesheet?
YourQTreeWidget QHeaderView::section {
background-color: red; // for the bakcground
border-right: none; // right-border of each section
border-left: none; // left border of each section
}
As you can read here (and as you see in your example) setBackgroundColor does not work for header item (I suspect that this is due to difference between header and row items).
You should to reimplement QHeaderView or to try option described above.

Change QGroupBox's content background

I want to change the background of a QGroupBox, however I would like to only change the inside background (the darker shade of gray below each QGroupBox's title) as shown here:
What I currently have is
QGroupBox {
background-color: red;
border: 3px dashed black;
}
which changes the background of the entire QGroupBox like this:
Is there a way in Qt to only change the "interior box" background rather than the whole container? Thank you in advance.
I guess there are 2 QGroupBox'es involved here, since that is not really clear from your post. Or is there a group box and some other inner container widget?
In either case you should be able to use stylesheets like following:
QGroupBox {
background-color: red;
margin-top:1em;
}
QGroupBox QGroupBox {
background-color: green;
}
QGroupBox::title {
subcontrol-origin: padding;
subcontrol-position: left top;
background: transparent;
margin-top: -2.5em;
}
This will give you following result:
You can of course replace the inner group box by an arbitrary widget.
You can do it using "setStyleSheet" function of widgets.
Get the inner group box object. And set the background color using "setStyleSheet" function.
Pseudo Code:
QGroupBox *innerGBox = new QGroupBox();
innerGBox->setStyleSheet("background-color: red");
To know more about setting styles programmatic , refer below link.
http://doc.qt.io/qt-4.8/stylesheet-examples.html
You need to tell Qt a little more about the kind of style you want, specifically the margins. Playing around a little with this code should give you the desired results:
QGroupBox {
background-color: red;
border: 3px dashed black;
margin-top: 1ex; /* leave space at the top for the title */
}
QGroupBox::title {
subcontrol-origin: margin;
padding: 0 3px;
}
Take a look at the Stylesheet examples

border and text qss settings at QTableView

Here's I am trying to do that:
but I set that stylesheet in my QTableView:
QTableView {
gridline-color: black;
background-color: transparent;
}
QHeaderView {background-color: transparent;
}
QHeaderView::section{
border-style: none;
border-bottom: 1px solid rgb(0,0,0);
background-color: transparent;
margin-bottom:5px;
margin-top:5px;
}
QTableView QTableCornerButton::section {
bottom-style:none;
border-bottom: 1px solid rgb(0,0,0);
}
Result is this:
I can handle with size problem for future but there is two main problem over here:
1.Column text between border there isnt any space, I did margin-top:5px; and margin-bottom:5px; but it changed for all QHeaderView not only QHeaderView's Text. (Solution is use padding instead of margin)
2.Every row has a right ,left even top border. I dont want that.
I tried this:
QTableView QTableCornerButton::section {
border-style:none;
border-bottom: 1px solid rgb(0,0,0);
}
Unfortunately there is a problem at QTableCornerButton:section it doesn't work...
Thank you for any helping
Note : I haven't verified, these are just suggestions to try, please upload the output if needed
1 - What do you mean by "it changed for all QHeaderView not only QHeaderView's Text"?
Maybe you expected to set margin only to the headerview's content (text) : in that case use the padding not margin.
QHeaderView::section{
/* your style */
padding-bottom:5px;
padding-top:5px;
}
2 - Every row has a right ,left even top border. I dont want that.
QTableView {
/* sone additional style */
gridline-color: cyan
background-color: cyan
}
QTableView::item
{
border-style: none;
border-bottom: 1px solid rgb(0,0,0);
}
I would try to use the border-style (set to none) as you did in QHeaderView's style.
Edit : You certainly must disable the showgrid's option of your QTableView by code to make it a working solution
tableView.setShowGrid(false);

How to change the color of the text of a QProgressBar with its value?

I don't know how to change the color of the text partially in the progress bar when its value becomes nearly 50%. This effect comes automatically in the fusion style progress bar (picture below). Does anyone know how this is done ?
Too lazy to write working example code, much less making a screenshot. Not even for 50 reps. :-)
However, the question was somewhat interesting. I had no idea how such a two colored text could be done. So I checked:
http://qt.gitorious.org/qt/qtbase/blobs/stable/src/widgets/styles/qfusionstyle.cpp
Line 1450ff (http://qt.gitorious.org/qt/qtbase/blobs/stable/src/widgets/styles/qfusionstyle.cpp#line1450).
QRegion rightRect = rect;
rightRect = rightRect.subtracted(leftRect);
painter->setClipRegion(rightRect);
painter->setPen(flip ? alternateTextColor : textColor);
painter->drawText(rect,
bar->text,
QTextOption(Qt::AlignAbsolute|
Qt::AlignHCenter|
Qt::AlignVCenter));
if (!leftRect.isNull())
{
painter->setPen(flip ? textColor : alternateTextColor);
painter->setClipRect(leftRect);
painter->drawText(rect,
bar->text,
QTextOption(Qt::AlignAbsolute|
Qt::AlignHCenter|
Qt::AlignVCenter));
}
Basically the text is drawn two times into the same rectangle. Each time with an appropriate clipping. Easy if you know how. :-)
From my point of view the best, and probably the easiest, way to do this is to change the pallet for the QProgressBar widget:
QPalette palette = progressBar->palette()
palette.setColor(QPalette::Text, textColor)
palette.setColor(QPalette::HighlightedText, textColor)
progressBar->setPalette(palette)
"The setBackgroundRole method let you use a color role for the background, which means one of the predefined color of the style applied to the widget. So your are basically limited to the style and its colors."
Background solution:
value = 65
self.progressBar.setProperty("value", value)
if value < 50:
self.progressBar.setStyleSheet("QProgressBar::chunk { background-color: black; }")
else:
self.progressBar.setStyleSheet("QProgressBar::chunk { background-color: black; } QProgressBar { color: white; }")
You can use stylesheet on the Container Widget :
myMainWidget.setStyleSheet(QString("QProgressBar {color: red}"));