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);
}
Related
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.
I would like to change the color of the categories and the values of the text (see embedded picture), I'm not sure if it has anything to do with my css styling or with the amcharts api. I did manage however to change the label with label.fill = am4core.color("white");
.chartdiv {
padding: 10px 10px;
background: #1a2035;
font-size: 12px;
line-height: 11px;
width: 610px;
border-radius: 15px;
border-style: solid;
border-color: #1a2035;
border-width: 1px;
opacity: 0.75;
}
.chartdiv .ImpactHeader {
line-height: 14px;
text-align: center;
font-weight: 600;
margin-bottom: 5px;
}
.chartdiv .ImpactLead {
font-weight: 500;
}
.chartdiv .ImpactCount {
text-align: right;
font-weight: 600;
}
.chartdiv .ImpactEnd {
font-weight: 500;
}
.chartdiv:empty {
display: none;
}
and the code:
am4core.ready(function() {
const units = 'km';
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart['data'] = [{"country":"","volume":'','Name':''}];
// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "country";
categoryAxis.renderer.grid.template.location = 10;
categoryAxis.renderer.minGridDistance = 20;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "volume";
series.dataFields.categoryX = "country";
series.dataFields.countryNameX = "c_name";
series.name = "volume";
series.columns.template.tooltipText = "{countryNameX}: [bold]{valueY}[/] "+units;
series.columns.template.fillOpacity = .8;
//label units on left side
var label = chart.createChild(am4core.Label);
label.text = units;
label.fontSize = 16;
label.align = "left";
label.rotation=270;
label.isMeasured = false;
label.x = -10;
label.y = 50;
label.fill = am4core.color("white");
var columnTemplate = series.columns.template;
columnTemplate.strokeWidth = 1;
columnTemplate.strokeOpacity = 1;
});
You need to set the color directly on the axis objects' label template as documented here:
categoryAxis.renderer.labels.template.fill = am4core.color("#ffffff");
valueAxis.renderer.labels.template.fill = am4core.color("#ffffff");
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 want to print rows of "QStandardItemModel". I use this code, It works but table is not beautiful.
I don't know how to adjust row height and I don't know how to set column widths based on the width of cells in the first row of the table.
QPrinter printer;
printer.setPageSize(QPrinter::A4);
printer.setFullPage(true);
QPrintDialog *dlg = new QPrintDialog(&printer,0);
if(dlg->exec() == QDialog::Accepted) {
QPainter painter;
if (painter.begin(&printer)) {
painter.translate(0,0);
int position = 0;
int rowCount=newMyModel->rowCount(QModelIndex());
for(int r=0;r<rowCount;r++)
{
if( position > painter.window().height()-100 )
{
printer.newPage();
position = 0;
painter.resetTransform();
painter.translate(0, 0);
}
QString html="<table style='page-break-after:always' border='1' width='100%' cellpadding =10 style='border-width: 1px;border-style: solid;border-color: #9e9e9e;width:100%'>";
index=newMyModel->index(r, 0);
QVariant prop1=index.data(MyModel::prop1);
QVariant prop2=index.data(MyModel::prop2);
'''
'''
'''
html.append(
"<tr style='background-color:red'>"
"<td style='border-width: 1px;padding:10; border-style: solid; border-color: #9e9e9e;width:16%'>"+prop1.toString()+" </td>"
"<td style='border-width: 1px;padding:10; border-style: solid; border-color: #9e9e9e;width:16%'>"+prop2.toString()+" </td>"
...
...
...);
}
html.append("</table>");
QRect rect = painter.boundingRect(painter.window(),
Qt::AlignJustify | Qt::TextWordWrap,
html);
QTextDocument doc;
doc.setHtml(html);
doc.drawContents(&painter, rect);
painter.drawRect(rect);
painter.translate(0, rect.height());
position += rect.height();
}
painter.end();
}
}
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();
}