How to set background and foreground color of QTableWidget row Vertical Header Item - c++

I am trying to set the background and foreground color of a QTableWidgetItem that is a Vertical Header Item in a row of my QTableWidget. The following code does not work for a QTableWidgetItem that is a vertical header item, even though the same code works fine for a QTableWidgetItem that is a regular cell.
I'm using Qt 4.8 on Fedora 17 x64 Gnome 3.4
static const QBrush AddedCellBackground = Qt::yellow;
static const QBrush AddedCellForeground = Qt::red;
void rowSelected()
{
QTableWidgetItem *vertHeadItem = _getSelectedItemVerticalHeader();
vertHeadItem->setForeground( AddedCellForeground );
vertHeadItem->setBackground( AddedCellBackground );
}
This code makes no change when vertHeadItem is a vertical header item, but it has the desired affect when it is a regular cell. How can I set the background and foreground of a vertical header item?

I think you can easily target specific items by knowing it's "AccessibleName", you can try this:
QTableWidgetItem#YourAccessibleName {
/* style definitions */
}

In the question Is it possible to change the colour of a QTableWidget row label? about the same topic but for PyQt my solution was to get the QTableWidgetItem of the vertical header and set the background and foreground there, however only the foreground was changed, the background stayed a smooth gray gradient. Probably the style was overridding/ignoring the background. Specifying a different style (Cleanlooks for example) fixed it.
So add something like in the construction of the table widget
QTableWidget::verticalHeader().setStyle(QStyleFactory::create("CleanLooks"))

Related

Set the QTabWidget background to be transparent by stylesheet but useless in VS2013

I set a picture as background in the mainframe and add two widgets on it: a QTableWidget and a QTabWidget.
I have set the background of tablewidget to be transparent by stylesheet
in vs2013. The table's background shows the picture I set but the horizontalheader section is still white:
TaskInfWidget_->setStyleSheet("QTableWidget{background:transparent;}\
QTableWidget::item{border-top:1px solid grey;border-bottom:none;border-right:none;border-left:none;}\
QHeaderView::section{background:transparent;color:black; font-family:宋体;font-size:15px;font-weight:200;\
border:none;}");
But when I add a tablewidget in a tabwidget and set the stylesheet of the tabwidget, it doesn't work:
QTabWidget *tab_widget=new QTabWidget;
QTableWidget *table = new QTableWidget(10, 1);
tab_widget->addtab(table,"hahaha");
tab_widget>setStyleSheet("QTabWidget::pane{background:transparent}\
QTabBar::tab{background-color:transparent;height:30px;}\
QTabBar::tab:selected{background-color:rgb(55,112,183)}\
QTabBar::tab:hover{color:rgb(235,97,0);}");
table->setStyleSheet("QTableWidget{background:transparent;\
border:3px solid rgb(30,77,135);\
selection-background-color:rgb(30,77,135);}");
I have set QTabWidget::pane{background:transparent} and QTableWidget{background:transparent}, but the background of the tablewidget is still white, not the picture of the mainframe.
This is my first time to ask question here. This question has bothered me several days. Who can tell me the reason?

QDockWidget::background-color not applied when docked

I have a QDockWidget:
I would like to alert the user to certain events by setting the background color of the title bar.
I have achieved this by setting the style sheet for my DockWidget:
void DockWidget::setCriticalAlert()
{
setStyleSheet("QDockWidget { background-color:red; }");
}
The result is this:
The problem is that the background-color doesn't get applied when the QDockWidget is docked:
How can I get the background color to be applied when the QDockWidget is docked?
This is a bug in Qt.
Issue 10537
Quoting from the linked issue:
The problem is that in QDockWidget::paintEvent, there is a
isFloating() condition before drawing PE_FrameDockWidget. We cannot
jsut remove this condition as it would break the other style (that
does not whish to draw frame when the dockwidget is docked) We cannot
either use PE_Widget to draw the frame as then it goes over the
dockwidget's title The solution is maybe to introduce a new
PE_FrameDockWidgetDocked primitive element. Or some
SH_DockWidget_DrawDockedFrame stylehint to draw the frame in every
cases.
a valid workaround seems to be to set the stylesheet of the parent, and use the class-and-id selector. Forgive the python formatted code but the concept is the same - in this case, 'dock' is a QDockWidget which has been given an object name using setObjectName(), and its parent, the QMainWindow, is 'self':
self.setStyleSheet("QDockWidget#"+str(dock.objectName())+"::title {background-color:red}")
In PyQt5.5, this works at runtime, i.e., can be changed on the fly.
I find a solution like this:
Firstly put a frame behind all the widgets of dockwidget's center widget, as the background.
Then set stylesheet for the frame.
By this way, we could change the background color of dockwidget.
Or you can extend the dockwidget and overwrite the function
void QDockWidget::setWidget(QWidget *widget)
using private/qdockwidget_h. and add a frame as this widget's father.

Qt space around QPushButton in QHBoxLayout

I have a QLabel and a QPushButton added to a QHBoxLayout. The QLabel has its margins set to 0 and the layout has margin and content margins set to 0. The label and button have the same background color and the button has border set to none. However, the button still looks with a brighter color than the label and there is some extra space around the button, so it doesn't look like it's "glued" to the label. I want them to look like one big widget.
In Qt, margins describe the space surrounding the layout. In newer versions of Qt, the margins on the top/bottom/left/right can be set individually through setContentsMargins().
The space between widgets in the same layout is described by the spacing property. The spacing has nothing to do with the margins. Try calling hboxLayout->setSpacing(0); This should work.
Some days ago I have coded a widget with similar behavior. To avoid problems with margins and colors I can recommend to use second QPushButton button instead of QLabel and set both buttons to be flat with btn->setFlat(true);

Styling QListWidget item widgets: selected state

I have a window which displays tiles, each tile having some set of information. Tiles are arranged in a tabular structure. The way in which this is implemented is, a QListWidget is used to hold tiles and each tile is set as the item widget in QListWidgetItems in QListWidget.
I have styled the tiles using a stylesheet. My problem is, I cannot get a tile highlighted in some way when the tile is selected. If I do not use stylesheets at all, default selected highlighting works. But as soon as I apply styles to tiles, there is no difference in the tile in non selected and selected states.
I tried to do it in following way but it does not work.
.tile
{
/*non selected style*/
}
.tileList::item:selected
.tile
{
/*selected style*/
}
Any idea how I can achieve this?
I solved it in Qt Designer by setting the palette how I wanted it and then put
QListView::item:selected { background: palette(Highlight) }
as the styleSheet. Maybe this helps somebody.
If you want to do it from a central qss, I guess you'll have to remove the ".tile" part from the code in the question.
.tileList::item:selected
.tile <--- remove this line
{
/*selected style*/
}
I could get this done to some extent (not a comprehensive solution), by doing following.
Make tile widget semi transparent.
Set a background color to QListWidgetItem
Set a different background color to QListWidgetItem when selected
Styles:
.titleList::item {
background-color: #fff;
}
.lstSnapQuote::item:selected {
background-color: #5555FF;
}

Change color highlight of icons in QTableView, when the cell is selected

When a cell is selected in QTableView, the icons in it are given a blue highlight, how can I control the color of this highlight or disable it?
I tried setting the QPalette::Highlight but it didn't work.
Edit:
Okay, so I do know how to change the background color and text color and color highlight, but not for an icon. If I return an icon as decoration for a cell, it is given a light blue highlight when the cell is selected. How do I remove this?
You can use style sheets to define the color of your elements. The name of the selected item in your QTableView is selection-background-color. So, changing the color of this element you will chose the background color that your prefer.
#include <QtWidgets/QApplication>
#include <QtWidgets/QTableView>
#include <QStandardItemModel>
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QTableView *table = new QTableView();
QStandardItemModel *model = new QStandardItemModel(2,2);
table->setModel(model);
table->setStyleSheet("selection-background-color: red");
table->show();
return app.exec();
}
Look how it looks in the picture:
I discovered a way around this issue, but it has some cost associated with it.
Fundamentally, deep within Qt code it is calling onto QIcon::paint() and passing QIcon::Selected as the icon mode, so the issue is that the "selected" form of the icon's pixmap at the desired resolution is the one auto-generated by Qt.
I worked around this by setting the Selected form of the icon to be the same as the Normal mode:
// Make the "Selected" version of the icon look the same as "Normal".
for (const auto& size : icon.availableSizes())
{
icon.addPixmap(icon.pixmap(size, QIcon::Normal, QIcon::Off),
QIcon::Selected, QIcon::Off);
icon.addPixmap(icon.pixmap(size, QIcon::Normal, QIcon::On),
QIcon::Selected, QIcon::On);
}
The downside is extra time spent doing this, possibly extra memory to store it, and wasted time generating the selected icons that we're throwing away.
In my case I'm using a QStyledItemDelegate and unfortunately that doesn't give you the ability to more closely influencing how the icon is rendered without completely reimplementing how QStyle::CE_ItemViewItem is rendered in your style.
Come to think of it, if you use a proxy style it wouldn't be too hard to override how CE_ItemViewItem is rendered to not use a selected icon, so that would be an option too.
It's utterly impossible to change this behavior with the standard style in Qt. You need to implement your own specific style in order to work around this.