I have a QTabWidget with two tabs inside. I want to set the background of both tabs to be transparent to see the color of the main window underneath, but when I set
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("background-color: transparent;");
it makes all the background of the widgets I have inside the tabs transparent, not the tab itself.
Album showing before stylesheet change:
after change,
and what I'm trying to achieve.
I can provide more code if needed. Thanks in advance!
Stylesheets are applied to touched widgets and all its children.
Currently your style definitions
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("background-color: transparent;");
says something like "Set background color for all widgets to transparent".
... but you can restrict stylesheet applicability.
E.g. you can say, that style definitions should be applied only to instances of certain classes like QLabel:
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("QLabel{ background-color: transparent; }");
There are even more possibilietes to define applicability in more details:
The Qt Style Sheet Syntax, Selector Types
Relevant for your case is the possibility to restrict the applicability of style definitions to objects with certain name:
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("QWidget#custom_tab, QWidget#templates_tab{"
" background-color: transparent; "
"}");
You must make sure, that object names match (here: 'custom_tab' and 'templates_tab'.
Related
I have a small qt application written in c++. I want the same application to run in small embedded device with a touchscreen but the size of few QToolButton was too small to be visible comfortably. I have tired to increase the size by modifying following function (adding setfont member call):
QToolButton* ColorToolBar::setupToolButton(QString name, QString iconPath, bool isCheckable)
{
QToolButton *p_btn = new QToolButton(this);
p_btn->setCheckable(isCheckable);
p_btn->setIcon(QIcon(iconPath));
p_btn->setIconSize(QSize(ICON_WIDTH, ICON_HEIGHT));
p_btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
QFont font = p_btn->font();
font.setPointSize(10);
p_btn->setFont(font);
if (!name.isEmpty())
p_btn->setText(name);
p_btn->setMinimumWidth(MINIMUM_WIDTH);
p_btn->setMinimumHeight(MINIMUM_HEIGHT);
return p_btn;
}
I am able to change the Icon size using setIconSize member function but setPointSize and setFont doesn't do anything. I want to have larger text and icon.
I have a class AdvancePlotToolBar which has all the QToolButton.My qss file looks like :
AdvancePlotToolBar QToolButton {
border: none;
padding-right: 10px;
padding-bottom: 5px;
}
AdvancePlotToolBar QToolButton:hover {
border-bottom: 3px solid #52ce90;
padding-bottom: 5px;
}
AdvancePlotToolBar QToolButton:pressed {
border-bottom: 3px solid #52ce90;
}
AdvancePlotToolBar QToolButton:checked {
border-bottom: 3px solid #52ce90;
margin: 0px;
}
EDIT:
The code below shows how I add QToolButton to the layout of AdvancePlotToolbar.
AdvancePlotToolBar::AdvancePlotToolBar(QWidget *parent) : QWidget(parent)
{
transformationButton_ = setupToolButton("Transformations", "://images//summation.png", true);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(transformationButton_);
setLayout(layout);
}
Setting a style sheet is overriding the font size you're setting in the C++ code. Try it w/out any style sheet applied (even to QApplication) and check. (Also How to set the font size of the label on pushbutton in Qt? )
I have the same issue with a completely custom QToolButton which overrides initStyleOption() and sets own font details in there (so it happens each time just before the button is painted, unrelated to setFont() at all). This works until an app-wide style-sheet is applied (even if it doesn't affect QToolButton directly! *), or a style sheet is applied specifically to the button instance or to its parent. I haven't tracked down where exactly this is happening (somewhere in QStyleSheetStyle I imagine), so I do not have a workaround yet **. In your case since you're using CSS for the tool button anyway, that would probably be best place to set the font size as well.
* The reason setting a style sheet may affect elements which are not styled specifically in the CSS rules is that the whole underlying QStyle is replaced with QStyleSheetStyle, and the original style (Fusion, WindowsVista, Macintosh, etc) becomes a proxy/fallback for QStyleSheetStyle. Or IOW all painting is filtered through the CSS style first. This includes setting style sheet on individual elements as well as globally on QApplication.
** I suspect this is a Qt bug.
As a follow up of " Hide label text for Qt tabs without setting text to empty string " :
Can I directly access the widgets within the tabs of the QTabBar. I do not mean the corresponding widget which is shown when I select a tab, but the tab's widgets (so in the screenshot below the log label and log icon).
I have tried QTabBar::findChildren, but with no success. Any idea?
QTabBar header sections are not actually widgets. They are drawn by QStylePainter inside QTabBar::paintEvent. Thus you can't get access to them.
As a workaround you can add a tab with an empty text and set a custom widget to it:
QTabBar *bar = new QTabBar;
bar->addTab("");
QLabel *label = new QLabel("my label");
bar->setTabButton(0, QTabBar::LeftSide, label);
I set the style sheet of a widget, and then I add a child widget after that. But it seems that contentsMargins() does not return the real margins until after the child widget is shown.
Sample code:
this->setStyleSheet("QLabel {padding: 0px 5px 10px 15px;}");
QLabel *label = new QLabel(this);
qDebug() << label->contentsMargins();
label->show();
qDebug() << label->contentsMargins();
This is the output:
QMargins(0, 0, 0, 0)
QMargins(15, 0, 5, 10)
Note that if I were to swap the order, i.e. adding the child widget before setting the parent's style sheet, the problem would disappear.
But for various reasons, I can't add the child widget before setting the style sheet, and I also need to be sure that I'm not being fed false data from contentsMargins(). Is there a workaround to ensure that?
You can call QWidget::ensurePolished to make sure the widget has the correct styling. This is what QWidget uses internally right before it is shown.
I'm trying to make a simple design to select a color and I'm using a QToolButton to open a QColorDialog.
My problem is that I would like the QToolButton to be of the selected color but I've only suceeded to set the background color, which is not what I want.
Here is a picture:
My code:
QColor color = QColorDialog::getColor(m_couleur);
if (color.isValid()) m_couleur=color;
m_labelCouleur->setPalette(QPalette(m_couleur));
m_labelCouleur->setAutoFillBackground(true);
m_buttonCouleur->setPalette(QPalette(m_couleur));
m_buttonCouleur->setAutoFillBackground(true);
Basically I would like the QToolButton to look something like this:
edit: Can anyone explain to me why the website won't let me add "Hi everybody" at the beginning?
QColor color = QColorDialog::getColor(m_couleur);
QPixmap px(20, 20);
px.fill(color);
m_buttonCouleur->setIcon(px);
No CSS involved in this case is (for me ofcourse) big pro
Use the setStylesheet function in order to change the background color of your button
m_buttonCouleur->setStyleSheet(QString("QToolButton{ background: %1; }").arg(m_couleur.name()));
I've done exactly that by using a QPushButton and setting its style sheet to the result from the color picker. I guess a tool button should probably be the same.
button->setStyleSheet(QString("background-color: %1; "
"border: 1px; "
"border-color: black; "
"border-style: outset;").arg(color.name()));
I currently have a QGraphicsScene that I am using with a QGraphicsGridLayout. I am trying to align QWidgets (QLabels and a custom graph QWidget) on this grid layout, and then export it to a QPrinter for pdf export.
The problem I'm having is that I have these grey divider lines between the QLabels that I can't seem to get rid of. I have tried settings spacing in the layout to 0, margins to 0, all the different properties of the QLabel palette, etc. all to no avail. Here's the relevant code:
main class:
QLabel lbl("some text");
lbl.setAutoFillBackground(true);
QPalette pal = lbl.palette();
pal.setColor(QPalette::Window, Qt::white);
lbl.setPalette(pal);
lbl.setFrameStyle(QFrame::NoFrame);
reportlayout->addWidget(&lbl);
reportlayout->generatePDF(reportfilename);
reportlayout class:
gridlayout->setContentsMargins(0,0,0,0);
gridlayout->setSpacing(0);
QGraphicsWidget* page = new QGraphicsWidget();
page->setLayout(gridlayout);
scene->addItem(page);
printer->setOutputFileName(filename);
painter->begin(printer);
scene->render(painter);
painter->end();
I have a feeling that it is the layout doing this, as the lines are between cells in the grid - but the layout doesn't have any color properties and I couldn't find anything to do with divider lines.
Thanks a bunch!
Have you tried stylesheets?
For example,
setStylesheet("QLabel { border:0px solid black; }");
You must investigate all possible selectors till find which one introduces the border.