The answer https://stackoverflow.com/a/24909605/3125006 shows how to add a QPushButton into a QTableWidget cell. This works perfectly.
But then the background area around the PushButton is selectable. (Background turns into selection color when clicked). How can I prevent that?
The solution in Pogrammer_ARM's comment is a suitable workaround. With corrected case:
QTableWidget* tblSensors = new QTableWidget();
tblSensors->setStyleSheet("QTableWidget::item{ selection-background-color: rgb(255,255,255)}");
Related
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?
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);
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.
I have a custom widget which inherits from QWidget and contains some labels in its layout. I would like to change the background color of the widget and the labels in the widget (this is, everything!) every time I put the mouse over it.
When using *:hover { background: red; } in my custom widget, I only get the contents red when moving the mouse over the labels, but not outside them, between labels, etc. I don't understand this behavior taking into account that I put the StyleSheet in the parent widget.
Any ideas? Many thanks,
You can set the parent's stylesheet which will cascade to children like this:
parent->setStyleSheet("* {background: red}");
For hovering only:
parent->setStyleSheet("*:hover {background: red}");
Check out https://qt-project.org/doc/qt-5.1/qtwidgets/stylesheet-syntax.html
Finally I solved the problem creating a QFrame inside the main QWidget and setting the StyleSheet of that QFrame.
How can I modify an existing stylesheet?
For example: if I want to create buttons, which when pressed each modify a single aspect of the stylesheet. One button can insert a margin-left attribute of 10. Another button can make the background colors blue. Lastly, another button can round the corners. The trick here though, is that I dont want to store all the variables and rebuild the style sheet on each button press. I would like to have a simple this->setStyleSheet(this->getStylesheet()+"margin-left: 10px:") for example.
Is there any way to do this?
Here is the code in main.cpp
QWidget wdg;
QHBoxLayout hlay;
wdg.setStyleSheet("border:2px solid rgb(74, 74, 74);");
QPushButton btn;
btn.setStyleSheet("border-radius:5px;");
btn.setText("Hello");
QPushButton btn2;
btn2.setStyleSheet("background-color: rgb(190, 190, 190);");
btn2.setText("Hello");
hlay.addWidget(&btn);
hlay.addWidget(&btn2);
qDebug()<<btn.styleSheet();
wdg.setLayout(&hlay);
wdg.show();
setting and getting style sheet works with QString and so you can use + operator.