How to make a bold font to a QCheckBox - qt

I have the following QCheckBoxes that will enable/disable alarms as shown below.
The problem I have is how do I change the font of the words "ON" and "OFF" only to make them bold? I am not sure how to combine a non-bold font with a bold font in the QCheckbox.
Below the snippet of code I have:
void FilterPCDInterface::on_disable123_toggled(bool checked)
{
if(ui->disable123->isChecked())
{
if(checked)
{
ui->disable123->setStyleSheet("QCheckBox {color: red}");
ui->enable123->setStyleSheet("QCheckBox {color: green}");
ui->disable123->setText("Alarms Zones Disabled: ON");
ui->enable123->setText("Enable All Alarms: OFF");
ui->enable123->setChecked(false);
ui->enableZone1->setEnabled(false);
ui->enableZone2->setEnabled(false);
ui->enableZone3->setEnabled(false);
}
}
if(!ui->disable123->isChecked())
{
ui->enableZone1->setEnabled(true);
ui->enableZone2->setEnabled(true);
ui->enableZone3->setEnabled(true);
ui->disable123->setStyleSheet("QCheckBox {color: red}");
ui->disable123->setText("Alarms Zones Disabled: OFF");
ui->enable123->setEnabled(true);
ui->enable123->setChecked(true);
ui->disable123->setEnabled(false);
}
}
What I have done so far:
I went through the following posts to hep me sort out the problem but without success.
I consulted this, also I came across this other source, which basically is from the official documentation. But it does not explain how to concatenate a non-bold font with a bold one.
It seems that the best way would be to use the QFont include but I am not sure how to apply it to the QCheckbox because I am not sure how to combine different fonts.
Thanks for pointing in the right direction on how to solve this problem.

You can use a QCheckBox with QLabel in a horizontal layout. So it becomes,
QString labelText = QString("Enable All Alarms <strong>%1</strong>").args(status)
ui->whateverQLabel->setText(labelText)
QHBoxLayout
/\
/ \
/ \
QCheckbox QLabel(label)

Related

How to customize QToolButtons from QToolBar in Qt?

I am having QToolBar with various tool buttons on it. I want to customize those buttons with some simple effects like, it should be seen that button is pressed, after pressing it should change its icon color or background color etc.
I tried but I could not succeed.
_toolbar = new QToolBar;
_toolbar->setIconSize(QSize(35,35));
_toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
void createIcons()
{
_zoomInIcon = QIcon::fromTheme("zoom-in");
_zoomIn = new QAction(_zoomInIcon, "Zoom in", this);
// code for other icons
_toolbar->addAction(_zoomIn);
}
void myClass::ZoomIn()
{
_zoomIn->setCheckable(true);
//QToolButton:_zoomInIcon {background-color: red; }
//setStyleSheet('background-color: red;');
// other logic
}
Moreover I am using Qt's default icons from this default-icons
But some of the icons are not looking good specially save in and save in as.
So does any one knows more default icons apart from above link in Qt ?
Can anyone help me ?
Try something like below (not tested)
//Get the tool button using the action
QToolButton* zoomInButton = mytoolbar->widgetForAction(_zoomIn);
//Set the style you want.
zoomInButton->setStyleSheet("QToolButton:pressed"
"{"
"background-color : red;"
"}"
);
And you can use all QPushButton styles, if your tool button don't have a menu.
https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qtoolbutton
The QToolButton has no menu. In this case, the QToolButton is styled
exactly like QPushButton.

How to center a QProgressBar's chunk?

I have this audio viewer that, for lack of a better widget, uses QProgressBars to display the overall volume and the volume of each RTA bin, in addition to selecting a range of "interesting" bins:
The parent widget has a stylesheet of "background-color: DimGray;"; I don't know if that has anything to do with the problem or not. The progressbars themselves are set up like this:
AudioMeter::AudioMeter(..., QWidget* parent) :
QWidget(parent)
{
...
meter = new QProgressBar(this);
meter->setOrientation(Qt::Vertical);
meter->setFormat("");
meter->setGeometry(...);
meter->setRange(FixedPoint::Zero, FixedPoint::One);
}
and used like this:
void AudioMeter::setValue(int value)
{
meter->setValue(value);
}
The problem is that the chunks (in light blue) seem to be offset to the left by a few pixels and up by one or two, so it just doesn't look right. This is true on Lubuntu 16.04 LTS (pictured) and on Ubuntu 16.04 LTS. How can I center them?
Or if there's a better widget to use (with straightforward code, not a custom thing with 20 re-implemented methods like I've seen in some examples), I'm open to that too.
http://doc.qt.io/qt-4.8/stylesheet-examples.html#customizing-qprogressbar
http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qprogressbar
http://doc.qt.io/qt-5/stylesheet-customizing.html#the-box-model
https://www.w3schools.com/css/css_padding.asp
It looks like the right padding is probably off for some reason.
// try this and see if it helps
meter->setStyleSheet("padding: 0;");
// or just zero out the right padding
meter->setStyleSheet("padding-right: 0;");
If not, you will probably need to do some iterations of stylesheet editing...
Use a stylesheet.css file in the same path as your program:
http://doc.qt.io/qt-5/qapplication.html#QApplication
-stylesheet= stylesheet, sets the application styleSheet. The value must be a path to a file that contains the Style Sheet.
So you run your program with
meters -stylesheet=./stylesheet.css
and fill in stylesheet.css with:
QProgressBar {
/* attempt at fix by removing all padding */
padding: 0;
/* Another style to try out sometime:
QProgressBar {
border: 2px solid grey;
border-radius: 5px;
}
QProgressBar::chunk {
background-color: #05B8CC;
width: 20px;
}*/
}
I've also done prototyping of stylesheets by connecting it's reload to an application wide hotkey or a pushbutton in your program.
QString stylesheet;
QFile f("stylesheet.css");
if (f.open(QFile::ReadOnly | QFile::Text))
{
QTextStream in(&f);
stylesheet = in.readAll();
}
qApp->setStyleSheet(stylesheet);
qApp->style()->unpolish(qApp);
qApp->style()->polish(qApp);
Hope that helps.

QT: Checkbox font CHANGES after first click

I'm having a strange problem in which a font set to a QCheckBox will change font after a first click to either check or uncheck it. I have no idea why. checkbox is a simple QCheckBox. The font change is subtle. Like from a low resolution font, to anti-aliasing with a slight color change.
I have no code that does that and I have no idea why it's happening.3
the stylesheet:
QCheckBox#TestCheck::indicator:checked
{
image: url(:/images/box_checked.png);
color: #535353;
}
QCheckBox#TestCheck::indicator:unchecked
{
image: url(:/images/box_unchecked.png);
color: #535353;
}
my code:
QFont font("Avenir LT Com");
font.setWeight(QFont::Bold);
font.setStyleStrategy(QFont::PreferAntialias);
font.setPixelSize(16);
this->checkbox = new QCheckBox(this);
this->shareScreenCheckbox->setObjectName(QString::fromUtf8("TestCheck"));
this->checkbox->setGeometry(10, 10, 400, 27);
this->checkbox->setText("This is a test");
this->checkbox->setFont(font);
this->checkbox->show();
The end result:
as you launch...
first click...
click again...
as you can see... font changes do not work. first font is different. click changes the font, and keeps it. What's going on?
I couldn't reproduce your problem, after running your code under qt 5.3. But I had a quite similar problem some time ago, with qt 5.1.1 maybe that workaround helps you too
QT 5.1.1: Checkbox in QWebview shows strange behavior under Win 7 (x64) / Win 8
class CustomStyle : public QProxyStyle
{
void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
{
if (element == QStyle::CE_CheckBox && dynamic_cast<QWebView*>(option->styleObject))
option->styleObject->setProperty("_q_no_animation", true);
QProxyStyle::drawControl(element, option, painter, widget);
}
};
If not, you should write a bugreport at https://bugreports.qt-project.org

Qt Color Picker Widget?

I have a QDialog subclass that presents some options to the user for their selecting. One of these options is a color. I have seen the QColorDialog, and I need something much simpler, that is also a regular widget so I can add to my layout as part of my dialog. Does Qt offer anything like this or will I have to make my own? If the latter, what is the best strategy?
Have you looked at the QtColorPicker, part of Qt Solutions?
QtColorPicker provides a small widget in the form of a QComboBox with a customizable set of predefined colors for easy and fast access. Clicking the ... button will open the QColorDialog. It's licensed under LGPL so with dynamic linking and proper attribution it can be used in commercial software. Search for QtColorPicker and you'll find it. Here's a link to one site that hosts many of the Qt Solutions components:
https://github.com/pothosware/PothosFlow/tree/master/qtcolorpicker
There's a very easy way to implement that using a QPushButton to display the current color and pickup one when it is clicked:
Definition:
#include <QPushButton>
#include <QColor>
class SelectColorButton : public QPushButton
{
Q_OBJECT
public:
SelectColorButton( QWidget* parent );
void setColor( const QColor& color );
const QColor& getColor() const;
public slots:
void updateColor();
void changeColor();
private:
QColor color;
};
Implementation:
#include <QColorDialog>
SelectColorButton::SelectColorButton( QWidget* parent )
: QPushButton(parent)
{
connect( this, SIGNAL(clicked()), this, SLOT(changeColor()) );
}
void SelectColorButton::updateColor()
{
setStyleSheet( "background-color: " + color.name() );
}
void SelectColorButton::changeColor()
{
QColor newColor = QColorDialog::getColor(color, parentWidget());
if ( newColor != color )
{
setColor( newColor );
}
}
void SelectColorButton::setColor( const QColor& color )
{
this->color = color;
updateColor();
}
const QColor& SelectColorButton::getColor() const
{
return color;
}
Qt doesn't offer anything simpler than QColorDialog natively, but there are several color picking widgets as part of wwWidgets, a user made set of widgets for Qt (note that this is "wwWidgets" with a "w" and not "wxWidgets" with an "x").
I think QColorDialog is best suited for your application. If you want to go for something simpler, it will come with reduced functionality. I'm not aware of any standard widget in Qt offering such an option but you can try out the following:
QCombobox with each entry corresponding to a different color. You can maybe even have the colors of the names in their actual color.
One or more slider bars to adjust the hue, saturation, val or R,G,B components.
QLineEdit fields for individual R,G,B components. You can also have a signal / slot mechanism wherein once the user changes a color, the color shown to the user gets changed accordingly.
You can have '+' and '-' signs to increase / decrease the above color component values.
I hope the above gives you some ideas.

Change Horizontal Header Background Color of QCalendarWidget

I am currently using Eclipse 3.5.2 and Qt Jambi 4.7.2 in Ubuntu 11.04 Natty Narwhal
I have a class that currently extends QCalendarWidget.
I am trying to use style sheets to style my calendar widget. Right now, I am using QAbstractItemView to color the background but it only changes the background color of the cells with dates in them. The horizontal header piece that contains the days of the week names remains white no matter what I do. Is there a way to change the background color of this header using a style sheet?
Any help would be appreciated.
Thank you.
I've not tested it with versions below 4.8 but we had the same problem and the solution was quite simple. We used this CSS code:
QWidget#qt_calendar_navigationbar
{
background-color: #424242;
border: 1px solid #4f4f4f;
}
edit: Well, read before you post - I do not know if it works in your subclass, but it may be worth a try.
I assume that you are trying to use a .qss file and setting the stylesheet using that file.
The developers did not fully implement the use of external stylesheets with the QCalendarWidget so you will have to hack it a bit.
I would suggest that you added a constant to the .qss file that you are using something like:
#cons BACKGROUND_COLOR: cyan
Then you can read from the file in your code:
String color = "";
try {
URL qssFile = getClass().getResource("*PATHNAME*");
Scanner scanner = new Scanner(qssFile.openStream());
String nextLine;
try {
while (scanner.hasNextLine()){
nextLine = scanner.nextLine();
if (nextLine.contains("BACKGROUND_COLOR:")) {
color = nextLine.substring(nextLine.indexOf("BACKGROUND_COLOR:") + 17);
}
}
finally{
scanner.close();
}
} catch (IOException e) {
e.printStackTrace();
}
The 17 in the code refers to the length of BACKGROUND_COLOR: so we can get the text after it.
Next you will want to create a new QColor, setting it to the variable color from above. Next create a new QBrush from the QColor. Then create a new QTextCharFormat and set its background to be the QBrush. Finally, set the format by calling the setWeekdayTextFormat method on the QCalendarWidget, passing it the days of the week you wish to change and the format you want to change it to. The follow code sets the box for every day of the week in the HorizontalHeader to have a background color of cyan:
QColor c = new QColor(color);
QBrush b = new QBrush(c);
QTextCharFormat format = new QTextCharFormat();
format.setBackground(b);
this.setWeekdayTextFormat(Qt.DayOfWeek.Sunday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Monday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Tuesday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Wednesday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Thursday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Friday, format);
this.setWeekdayTextFormat(Qt.DayOfWeek.Saturday, format);
Use all of that code together and you have yourself a way to change the HorizontalHeader background color by using .qss files (and more if you wish).

Resources