In the constructor, I set the style for the form and buttons:
{
ui->setupUi(this);
this->setStyleSheet("QWidget {background: rgb(49, 54, 59); color: rgb(220, 221, 218); selection-color: lightyellow; selection-background-color: darkcyan;}"
"QPushButton::hover {color: darkcyan; border: 2px solid grey; border-radius: 3px};");
}
Next, in the properties (icon parameter) of the button, I added an icon via resources, I also set the icon size using the iconSize parameter - 20x20, I also set the flat - True parameter in.
And this is what I get when I hover the cursor over the button:
The size of the button itself is formed using the layout and the size is 32x26, when the picture is set to 20x20.
Please tell me how you can remove this gap between the picture and the border of the button?
I tried to set the button size statically, about 22x22 and then everything is more or less fine, but I don't want to resort to such a radical solution. Thanks.
Referring to Qt's box model, the gap between the border and the icon is the padding.
In the stylesheet, you can set the padding to 0 as followed padding: 0px;
Related
In Qt I set a background image for a QPushButton, then I added a tooltip for this button and the tooltip has the same background image as the button and I couldn't change it with stylesheet, what am I missing?
In my code I have:
button->setStyleSheet("background-image: url(pathToImage);");
button->setToolTip("Click here");
In my style.qss I have:
QToolTip{
background-image: none;
background-color: white;
font-size: 20px;
border: 1px solid black;
}
The font-size and the border works, but the tooltip's background-image is the same as the button's.
I also tried adding another background-image to the tooltip, it didn't worked either.
How can I change the tooltip's background?
You have to specify the QWidget where to apply the property. If you dont do so, it will apply it to all the childrens of the widget.
In your case, to avoid the background image in the tooltip you have to specify that you want to apply that style to a QPushButton widget. The documentation says:
If we want the property to apply only to the QLineEdits that are children (or grandchildren or grand-grandchildren) of a specific dialog, we would rather do this:
myDialog->setStyleSheet("QLineEdit { background-color: yellow }");
In the example you mention, if you want to modify the style of the tooltip and the button, do something like this:
ui->pushButton->setStyleSheet(""
"QPushButton { background-image: url(me.png); }"
"QToolTip { color: #ffffff; background-color: #000000; border: 0px; }");
It will give you something like this
Update:
If you want to apply it to a single object and not the rest of the widgets of the same type, the documentation says:
If we want the property to apply only to one specific QLineEdit, we can give it a name using QObject::setObjectName() and use an ID Selector to refer to it:
myDialog->setStyleSheet("QLineEdit#nameEdit { background-color: yellow }");
So in your case:
ui->pushButton->setObjectName("awesomeButton");
ui->pushButton->setStyleSheet("QPushButton#awesomeButton { background-image: url(me.png); }");
When you set qss with setStyleSheet your stylesheet applies for all children of object. In your case you can avoid this using stylesheet for QPushButton only
button->setStyleSheet("QPushButton {background-image: url(pathToImage);}");
Image of QFrame issue:
I am putting a frame around the window and my window is having a scrollbar (vertical scrollbar only).
I have set the QFrame styling as
QFrame {
border: 8px solid gray;
}
and for styling of scrollbar I have used customizing-qscrollbar.
Now I am seeing that there a white line at the bottom of the scrollbar next to the down arrow. The same line is not there above the up arrow of scrollbar.
Seems like a corner widget was causing the above issue.
Since i found the fix, i would like to share.
QAbstractScrollArea::corner {
background: none;
border: none;
}
I have a QTabWidget with a QTableWidget inside, as the example below:
But it has a "padding" (at least I think it is a padding) in the QTabWidget (marked as red in the figure).
How can I remove that or expand the QTableWidget to fill the QTabWidget area?
I am using Qt 5.3.
Try something like this :
tabwidget.setStyleSheet("QTabWidget::pane {
margin: 0px,1px,1px,1px;
border: 2px solid #020202;
border-radius: 7px;
padding: 1px;
background-color: #E6E6E3;
}");
Hope this help you
The problem seems to be related to the "pane" margin of the QTabWidget.
I solved the problem by using this on the stylesheet:
QTabWidget::pane {
border: 0 solid white;
margin: -13px -9px -13px -9px;
}
When you put QTableWidget to QTabWidget tab, you can right click on it (QTabWidget) and select Lay out -> Lay out vertically (for example, or horizontally), this will add an verticalLayout element and place your QTableWidget into it, filling the whole tab. Then, select newly created verticalLayout, scroll down to Layout section, and from here you can control layoutLeftMargin, layoutTopMargin, layoutRightMargin and layoutBottomMargin properties:
Setting all of them to 0, will give you desired result (no stylesheets involved):
I'm trying to make the parent items in my QTreeView taller, have larger fonts, a custom expanded/collapsed icon in the right and be underlined. Here's my style sheet so far:
QTreeView::item:has-children
{
font: bold 24px;
height: 20px;
border-bottom: 2px groove lightgray;
}
QTreeView::branch:closed:has-children
{
border-bottom: 2px groove lightgray;
image: url(:/images/collapsed.png);
}
QTreeView::branch:open:has-children
{
border-bottom: 2px groove lightgray;
image: url(:/images/expanded.png);
}
Some of the styles are working. The underline on the branch and item only appears when the item has children which is what I wanted. The icon has changed which is also good. However, the "height" and "font" are not being respected and all items have the same height and font. QTreeView::item:has-children seems valid because the border-bottom is respected. If I set "height" for simply "QTreeView::item", all items get that height but "font" still doesn't work. Can anybody help me with the syntax here?
The tree view has the following properties set in designer:
frameShape: NoFrame
frameShadow: Plain
editTriggers: NoEditTriggers
selectionMode: NoSelection
animated: true
headerHidden: true
The rest of the properties are defaults
QTabBar has a 1px border that separates tabs from their content.
QTabWidget::pane { border:1px solid #C4C4C3; }
I want the border to disappear under selected tab, like it's done in all browsers and most of applications using tabs.
However, setting styles for QTabBar::tab doesn't help:
QTabBar::tab:selected { border-bottom-color:white; }
So how can I achieve this?
That line is controlled by the top border of QTabWidget::pane.
For example:
QTabWidget::pane { border: 1px solid #C4C4C3; top: -1px; }
would move the line behind/under the tabs (somehow using top alone seems to remove the border completely).