I want to add a colored Widget over the full QStatusBar. I added a QLabel with red background-color but there is a padding around the label, which i can't remove.
what i tried:
setSizeGripEnabled(false)
setStyleSheet("QStatusBar { border: 0px; padding: 0px; margin: 0px; }"
"QStatusBar::item { border: 0px; padding: 0px; margin: 0px; }"
layout()->setContentsMargins(0, 0, 0, 0);
Update: Example Code:
QWidget *w = new QWidget;
QHBoxLayout *layout = new QHBoxLayout;
QStatusBar *statusBar = new QStatusBar;
QLabel *label = new QLabel("Example");
w->setStyleSheet("background-color: green");
label->setStyleSheet("background-color: red");
statusBar->addPermanentWidget(label, 1);
statusBar->layout()->setContentsMargins(0, 0, 0, 0);
statusBar->setSizeGripEnabled(false);
setStatusBar(statusBar);
w->setLayout(layout);
setCentralWidget(w);
}
I think it is not possible without pointer hacking or reimplementing all QStatusBar functionality because QStatusBar implementation based on pimpl idiom, which means some implementation hidden in private headers and borders between QStatusBar widget and children widgets are hardcoded in qstatusbar.cpp
QRect ir = item->w->geometry().adjusted(-2, -1, 2, 1);
...
QStyleOption opt(0);
opt.rect = ir;
...
style()->drawPrimitive(QStyle::PE_FrameStatusBarItem, &opt, &p, item->w);
QStatusBar{
min-height: 20px;
}
use the min-height css property.
Related
On my Windows machine text is displayed to the right of the progress bar.
On my Linux machine text appears in the middle of the progress bar.
If I apply style to progress bar, text appears inside on both systems. How do I get it to appear outside, like in default Windows style?
Here's the simplified version of the code I used to play with progress bars:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
auto widget = new QWidget;
auto layout1 = new QVBoxLayout;
widget->setLayout(layout1);
auto layout2 = new QHBoxLayout;
auto progressBar = new QProgressBar;
auto spinBox = new QDoubleSpinBox;
spinBox->setRange(0,100);
spinBox->setDecimals(1);
spinBox->setSingleStep(1);
progressBar->setRange(0, 10000);
connect(spinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), progressBar,
[spinBox, progressBar]
{
progressBar->setValue(spinBox->value() * 100);
progressBar->repaint();
});
layout2->addWidget(spinBox);
layout2->addWidget(progressBar);
auto textEdit = new QPlainTextEdit;
auto stylesheet = R"(
QProgressBar {
background-color: cyan;
border-radius: 1px;
text-align: right;
}
QProgressBar::chunk {
background-color: magenta;
border-radius: 1px;
}
)";
textEdit->setPlainText(stylesheet);
connect(textEdit, &QPlainTextEdit::textChanged, progressBar,
[textEdit, progressBar]
{
progressBar->setStyleSheet(textEdit->toPlainText());
});
layout1->addLayout(layout2);
layout1->addWidget(textEdit, 1);
setCentralWidget(widget);
}
The solution is to use a margin. If you change the QProgressBar section of your stylesheet to this:
QProgressBar {
background-color: cyan;
border-radius: 1px;
text-align: right;
margin-right: 4em;
}
then the right end of the progress bar will stop short of the text (which is right-aligned), resulting in the effect you are looking for.
I changed a QPushButton into a drop-down menu. My codes are as follows:
m_menu = new QMenu(this);
m_addAction = new QAction(m_menu);
m_delAction = new QAction(m_menu);
m_addAction->setText(QObject::tr("add"));
m_delAction->setText(QObject::tr("del"));
m_menu->addAction(m_addAction);
m_menu->addAction(m_delAction);
m_menu->setStyleSheet("\
QMenu {\
background-image: url(:/img/tanchu-1.png);; /*background-image*/\
border: 3px solid rgb(235,110,36);/*menu border*/\
}\
QMenu::item {\
font-size: 10pt; \
color: rgb(225,225,225);\
border: 3px solid rgb(60,60,60);\
background-image: url(:/img/tanchu-1.png);\
padding:160px 160px;\
margin:2px 2px;\
}\
QMenu::item:selected { \
background-color:rgb(235,110,36);\
}\
QMenu::item:pressed {\
border: 1px solid rgb(60,60,61); \
background-color: rgb(220,80,6); \
}\
");
ui->pushButton->setMenu(m_menu);
and the button I get looks like this. There is a black drop-down arrow in the lower right corner.
But now the problem is that when I remove the drop-down arrow from the drop-down menu by using
ui->pushButton->setStyleSheet("QPushButton::menu-indicator{image:none;}");
the border-image that I set to the QPushButton with QSS border-image: url(:/img/btn_mid_0.png);disappears like image2 . The drop-down arrow and the border-image disapper together. How do I remove the drop-down arrow while maintaining the border-image I set before?
Besides, the The width of the submenu does not change with the qpushbutton like image3. How can I set the width of the drop-down menu to be as wide as the QPushButton?
Thanks!
All my codes are as follows:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
QMenu *m_menu;
QAction *m_addAction;
QAction *m_delAction;
QPushButton *m_pushButton;
void addFunc();
void delFunc();
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMenu>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_menu = new QMenu(this);
m_addAction = new QAction(m_menu);
m_delAction = new QAction(m_menu);
m_addAction->setText(QObject::tr("add"));
m_delAction->setText(QObject::tr("del"));
m_menu->addAction(m_addAction);
m_menu->addAction(m_delAction);
connect(m_addAction, &QAction::triggered, this, &MainWindow::addFunc);
connect(m_delAction, &QAction::triggered, this, &MainWindow::delFunc);
m_menu->setStyleSheet("\
QMenu {\
background-image: url(:/img/tanchu-1.png);; /*background-image*/\
border: 3px solid rgb(235,110,36);/*menu border*/\
}\
QMenu::item {\
font-size: 10pt; \
color: rgb(225,225,225);\
border: 3px solid rgb(60,60,60);\
background-image: url(:/img/tanchu-1.png);\
padding:160px 160px;\
margin:2px 2px;\
}\
QMenu::item:selected { \
background-color:rgb(235,110,36);\
}\
QMenu::item:pressed {\
border: 1px solid rgb(60,60,61); \
background-color: rgb(220,80,6); \
}\
");
ui->pushButton->setMenu(m_menu);
ui->pushButton->setStyleSheet("QPushButton::menu-indicator{image:none;}");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addFunc()
{
qDebug() << "addFunc";
}
void MainWindow::delFunc()
{
qDebug() << "delFunc";
}
Just set all the button CSS in one place, either in the QtCreator/Designer properties, or in the C++ code, not both. Basically your C++ CSS is overriding what you set in designer view.
In Designer view just use something like this for the styleSheet property:
QPushButton { border-image: url(:/img/btn_mid_0.png); }
QPushButton::menu-indicator { image: none; }
Or the same thing in C++ but with quotes :)
As for the menu styling... I'm not sure what's going on in there now (looks strange!) but the simplest way to have their sizes match is to set the size for both explicitly, either in CSS (width: XXXpx;) or in C++ (QWidget::setFixedWidth(XXX)). The C++ version may work better if the button is managed by a layout, since what happens with CSS size properties then gets vague. For CSS sometimes min-width and/or max-width work better than just width.
I'm messing around in the QT UI designer and trying to add a horizontal layout box around some buttons and a spacer. But I'm getting some weird results:
Here's the before image:
And here's the after image:
Not only did it not retain the order of the items, it also changed the style of the minus button to this weird gray box.
How do I keep it from doing that? Or how do I change the button's style back to what it was before it was added to the constraint?
I tried in Qt designer and it works. I think something overwrites your button styles.
Also have you tried to code buttons style? To use this code you need to create empty QWidget window and place two buttons.
form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
#include <QHBoxLayout>
#include <QMessageBox>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
~Form();
public slots:
void plusClicked();
void minusClicked();
private:
Ui::Form *ui;
QHBoxLayout *horizontalLayout;
};
#endif // FORM_H
form.h
#include "form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
ui->pushButton->setText("+");
ui->pushButton->setStyleSheet("background-color: white; border-style: solid; border-width: 1px; border-radius: 8px; border-color: gray; width: 60px; height: 30px; font-weight: bold; font-size: 14pt;");
ui->pushButton_2->setText("-");
ui->pushButton_2->setStyleSheet("background-color: white; border-style: solid; border-width: 1px; border-radius: 8px; border-color: gray; width: 60px; height: 30px; font-weight: bold; font-size: 14pt;");
horizontalLayout = new QHBoxLayout(this);
horizontalLayout->addStretch();
horizontalLayout->addWidget(ui->pushButton);
horizontalLayout->addWidget(ui->pushButton_2);
this->setLayout(horizontalLayout);
connect(ui->pushButton, &QPushButton::clicked, this, &Form::plusClicked);
connect(ui->pushButton_2, &QPushButton::clicked, this, &Form::minusClicked);
}
void Form::plusClicked()
{
QMessageBox::information(this, "Form", "Plus button clicked!", QMessageBox::Ok);
}
void Form::minusClicked()
{
QMessageBox::information(this, "Form", "Minus button clicked!", QMessageBox::Ok);
}
Form::~Form()
{
delete ui;
}
main.cpp
#include "form.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Form mainWindow;
mainWindow.show();
return a.exec();
}
That is something like closable tabs in QTabBar.
Ready:::::::::::::::::::::
BtnWithClosing::BtnWithClosing(QString txt, QWidget *parent):
QPushButton(txt, parent) {
QHBoxLayout *layout = new QHBoxLayout;
layout->setMargin(4);
layout->setSpacing(0);
layout->setSizeConstraint(QLayout::SetMaximumSize);
setText(text() + " "); // for close button space
QPushButton *btnClose = new QPushButton;
btnClose->setStyleSheet("QPushButton:hover {image:"
"url(:/images/closebtnover.png);}"
"QPushButton {image: url(:/images/closebtn.png); margin-right: -9;"
"margin-left: -6; min-width: 0; background-color: transparent;"
"min-height: 0; border-width: 0px; border-radius: 9px; padding: 0px;}");
layout->addWidget(btnClose, 0, Qt::AlignRight);
setLayout(layout);
}
I am using the following code for set the style for the table in simulator(S60)(Nokia Qt SDK).
searchTable->setStyleSheet("background: rgb(255,255,255);color:rgb(0,0,0); font-family:Arial Narrow;font-size:20px; border: 4px outset rgb(255,255,255);gridline-color: #669933;"
"selection-background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #486909, stop: 1 white);"
);
But When I am selecting the element in the data I got the following output. Please find the attachment.
Please help me.... What I did wrong .. Thanks in advance.
I guess your mistake is that you set up the stylesheet only for QTableView and not for all it's child widgets: cells. You could try to write your style code into a ".qss" file, add it to your app's resource file and then load it into your main.cpp with this code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QFile file(":/qss/stylesheet.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
file.close();
qApp->setStyleSheet(styleSheet);
w.show();
}
In your style file you have to write something like this:
QLineEdit{
border: 2px solid grey;
border-radius: 10px;
padding: 0 8px;
background: white;
selection-background-color:darkgrey;
}
In this way all QLineEdit widgets will be shown with your style rules.