Qt - QPushButton margin of icon needs to stay the same - css

I create a set of buttons using the function below with text, buttonName, that can change in width. The icon that appears when I click one of the buttons then justfies itself based on the width of the text; how do I make the icon stay the same margin from the right of the button regardless of text? Don't say custom delegate, because I haven't been able to figure out how to implement that!
QPushButton *LayoutCreator::createButton(const QString &buttonName) {
QIcon ico;
ico.addPixmap(QPixmap(":images/images/on.png"), QIcon::Normal, QIcon::On);
ico.addPixmap(QPixmap(":images/images/off.png"), QIcon::Normal, QIcon::Off);
QPushButton* button = new QPushButton(buttonName);
button->setStyleSheet("QPushButton { height: 70px; font-size: 20px; }");
button->setIcon(ico);
button->setLayoutDirection(Qt::RightToLeft);
button->setIconSize(QSize(32,32));
button->setCheckable(true);
return button;
}

Try adding
text-align: right;
to your button style sheet.

Related

center align textarea in JavaFX with CSS

I know I am supposed to set -fx-text-alignment: center; in order to achieve a center-aligned textarea.
#txtFaContent
{
-fx-text-alignment: center;
}
with txtFaContent being the ID of my textarea (as well as its variable name inside the controller). But it doesn't have any effect on the text alignment of my textarea (I played with right/left/center; but no success).
Have I missed anything?
Add style class in your TextArea.text:
TextArea textArea = new TextArea();
textArea.getStyleClass().add("centered-text-area");
Add .text in your CSS:
.centered-text-area .text {
-fx-text-alignment: center;
}

QTabwidget's tab text alignment not working

i am trying to show the opened tab text in left .i wrote that in stylesheet.but it is not working.
here is the stylesheet i used
QTabWidget QTabBar{
background-color: #373738;
height: 30px;
}
QTabWidget QTabBar::tab{
text-align: left;
background-color: #136ba2;
border-style: 1px rgb(67, 67, 67);
height: 30px;
width: 130px;
color: #136ba2;
padding: 0px 5px 0px 5px;
}
QTabWidget QTabBar::tab:selected{
background-color: #5A5B5C;
font-weight: bold;
color: #ffffff;
}
QTabWidget QTabBar::tab:!selected{
background-color:#353536;
color: #B4B4B4;
}
QTabWidget QTabBar::tab:hover{
background-color: #5A5B5C;
color: #ffffff;
}
here is an image
I think it is not possible with stylesheet, according to the doc :
text-align [...] This property is currently supported only by QPushButton and QProgressBar.
https://doc.qt.io/qt-5/stylesheet-reference.html#list-of-properties
This can be accomplished using the tab button widget to show the tab text.
Have a label, with left aligned text:
QLabel * button = new QLabel("text");
button->setAlignment(Qt::AlignLeft);
Set the tab text to empty string if necessary:
ui->tabWidget->tabBar()->setTabText(index, "");
Set the label as the tab button widget:
ui->tabWidget->tabBar()->setTabButton(index, QTabBar::LeftSide, button);
You can still style the label, adding a QLabel entry in the tab widget stylesheet:
QLabel{ color: white; }
Only problem here: no way to style the label text depending on selection (i.e. making the selected tab text bold) using CSS. But still feasible catching the tab widget currentChanged signal:
void Form::on_tabWidget_currentChanged(int index)
{
for(int i=0; i<ui->tabWidget->tabBar()->count(); i++)
{
QWidget * button = ui->tabWidget->tabBar()->tabButton(i, QTabBar::LeftSide);
if(button != nullptr)
{
QFont font = button->font();
font.setBold(i == index);
button->setFont(font);
}
}
}
One workaround(not clean method) I found is add some extra space at the end of the title;
QString tabTitle = "Page 1 ";
Will move the text towards left.

How do I make qtreewidget header transparent?

I want the treewidget header to look as if it's a label, so, I guess, I have to make it transparent? How do I do it?
I tried treeWidget->setStyleSheet("QTreeWidget::header {background-color: transparent}"); but it doesn't work.
The header is not a subcontrol of an itemview, thus QTreeWidget::header will not work. The header rather is a children widget of the view.
That means you could access the header via stylesheets with QTreeWidget QHeaderView {/*style here*/ }
For the background color of a header view you can check out the official Qt example.
In your case as you are setting the stylesheet directly to the view you can omit the parent so the following will do what you asked for:
treeWidget->setStyleSheet("QHeaderView::section { background-color: transparent; }");
In order to set the headers transparent, you have to add the following to the widget stylesheet:
QAbstractItemView QHeaderView {
show-decoration-selected: 0;
background: transparent;
}
QAbstractItemView::section QHeaderView::section {
show-decoration-selected: 0;
background: transparent;
}

Tab close button position

I want to style my tabs in my Qt app as follows:
I used following style sheet:
QTabBar{background-color: #fff; border-top: 0px;}
QTabBar::tab {
border-image: url(:/New_UI/tab_inactive.png) 7 17 7 2;
margin-left: 2px;
border-right: 17px;
border-top: 5px;
border-bottom: 5px;
font: 400 9.2pt "Segoe UI";
color: #ccc;
padding: 0px 13px 0px 5px;
max-height: 26px;
}
QTabBar::tab:selected, QTabBar::tab:hover {
border-image: url(:/New_UI/tab_active.png) 6 17 6 2;
}
QTabBar::close-button {
image: url(:/New_UI/tab_close.png);
subcontrol-origin: padding;
subcontrol-position: right;
width: 13px;
height: 13px;
}
The result is as follows (close button position is not as I wanted):
What am I doing wrong & how could I get my desired result ?
EDIT : I know this post is old, but I hope it could help someone else.
After a couple of tests, I think there is one way to do this, but it does not use Qt style sheets :
Subclass your QTabWidget to have complete access to the protected features
Create your own QWidget or QPushButton as your close button
Manage the position of your button with the stylesheet property (margin-right for example)
Add your button to the tab tabBar()->setTabButton(index, QTabBar::RightSide, closeButton);
The code I used for the test :
MyTab::MyTab(QWidget *parent) : QTabWidget(parent)
{
/// Create your button
QPushButton *close = new QPushButton(this);
// Add a tab
addTab(new QWidget(), QIcon(), "Tab 1");
setStyleSheet("QTabBar::tab { width : 150px;}");
// Size and move your button
close->setStyleSheet("max-height: 14px; max-width: 15px; margin-right: 50px;");
// Add your button to the tab
tabBar()->setTabButton(0, QTabBar::RightSide, close);
}
Finally, in the MainWindow, I added my own TabWidget to a layout :
ui->layout->addWidget(new MyTab(this));
The result :
But now you will have to handle the close action manually by connecting the button and get the index for a removeTab(index) call.
I am doing the same thing as you do, here is my stylesheet:
QTabBar::close-button{
image:url(:tabclose.png);
margin-right:4px;
}
Do not use "width" and "height" property, those two doesn't work here, setting a "image:url()" on sub controls implicitly sets the width and height of the sub-control (unless the image in a SVG).
Use "margin-right" property to control the distance from the right edge of the tab;
Add a custom button is a good answer.but if you use margin to decide close-button's position,the close-button's mouse area will be abnormal,so I add a SpacerItem and button in a widget,finally add this widget to TabWidget.
void TabBarCloseable::tabInserted(int index)
{
QWidget *widget = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout(this);
widget->setLayout(layout);
QToolButton *closeBtn = new QToolButton(this);
layout->addWidget(closeBtn);
layout->insertSpacing(1, 15);
closeBtn->setStyleSheet("max-height: 16px; max-width: 16px;");
this->setTabButton(index, QTabBar::RightSide, widget);
QTabBar::tabInserted(index);
}
You have wrong padding
Top
QTabBar::tab {
min-width: 25ex;
padding: 10px 50px 10px 10px;
}
buttom
QTabBar::tab {
min-width: 25ex;
padding: 10px 0px 10px 10px;
}
This is a pure stylesheet solution, without creating the buttons manually:
QTabBar::close-button {
image: url(:/tab-close.png);
padding-left: -13px;
}
If you check the Qt source, the image painting code uses only the padding values, not the margin values.

Styling QTabWidget background with CSS

I wish to set a custom background color for active QTabWidget tab. Unfortunately, I can't figure out the desired selector.
On Linux the following hack works:
QTabWidget::tab > QWidget > QWidget {
background: #fff;
}
But on Windows I have to use one more QWidget:
QTabWidget::tab > QWidget > QWidget > QWidget {
background: #fff;
}
Is there a "real" solution?
You have to use QTabBar and not QTabWidget. The selectors you should use are the following:
// Control the tab-bar with respect to the QTabWidget
QTabWidget::tab-bar {
left: 5px;
}
// Control the look of the tab in general
QTabBar::tab {
border-top-left-radius: 4px;
border-top-right-radius: 4px;
}
// Control the look of the tab when it is selected
QTabBar::tab:selected
{
// Add css parameters
}
// Control the look of the tab when hovering over it
QTabBar::tab:hover
{
// Add css parameters
}
// Control the look of the tab when it is not selected
QTabBar::tab:!selected
{
// Add css parameters
}

Resources