Set transparency of QButtons in QT C++ in Linux - c++

I am using Qt Creator 4.0.2 Based on Qt 5.7.0 (GCC 4.9.1 20140922 (Red Hat 4.9.1-10), 64 bit).
I am displaying an image on my widget and placing QButtons and QLabels on top of it.
It looks something like this:
I want to make the button semi-transparent. There isn't any property for it. I tried the answer given on another thread as:
ui->button->setStyleSheet("background-color: rgba(255, 255, 255, 50);");
But it didn't work for me. I also tried setStyleSheet("background:transparent;")
But it didn't work.
Please suggest a way of doing it.

setting the bg color to "transparent" is not enough, you need to set the "border-style: outset;" and nothing else in the main window/widget...
ui->pushButton_7->setStyleSheet("background-color: transparent; style: outset;");
then:
do:
ui->pushButton_7->setStyleSheet("background-color: #FF0011");
auto qgoEffect = new QGraphicsOpacityEffect(this);
qgoEffect->setOpacity(0.15);
ui->pushButton_7->setGraphicsEffect(qgoEffect);
ui->pushButton_7->setAutoFillBackground(true);

Setting a transparent background to the button is not enough: you have to set some attributes to your main widget in order to ask the window manager to let the button to manage its own background.
QWidget* w = new QWidget();
QPushButton* btn = new QPushButton("Click me", w);
btn->setStyleSheet("background-color: rgba(255, 255, 255, 50);");
w->setWindowFlags(Qt::FramelessWindowHint); // Do not display the window frame
w->setAttribute( Qt::WA_TranslucentBackground, true ); // Do not display the default background
w->show();

Related

Qt: change font weight

I would like to have my text in QLabel somewhere between bold and normal style and I believe that setting font-weight should be the answer to my problem.
In Qt documentation, I have found out that there are two options how to change font-weight:
From cpp side via: QFont::setWeight() method which accepts numbers 0-99
http://doc.qt.io/qt-4.8/qfont.html#Weight-enum
From Qss style via: font-weight attribute, which accepts numbers 100,200,...,900
http://doc.qt.io/qt-4.8/stylesheet-reference.html#font-weight
I have tried both methods and nothing seems to work. I always get only normal or the ordinary bold style and nothing in between.
Example:
QLabel* test1 = new QLabel("Font-weight testing");
test1->show();
QLabel* test2 = new QLabel("Font-weight testing");
QFont font = test2->font();
font.setWeight(40);
test2->setFont(font);
test2->show();
QLabel* test3 = new QLabel("Font-weight testing");
test3->setStyleSheet("font-weight: 400");
test3->show();
In the example above, I have created 3 labels. One without any additional setting, one where I have changed font weight via setWeight method, and one where the font-weight should be changed via Qss style. But all three will end up being exactly the same.
I have even tried to make font bigger, enable antialiasing, or use different font but nothing helped.
The QFont::setWeight method expects its input value to be one of the QFont::Weight enum values.
http://doc.qt.io/qt-5/qfont.html#setWeight
The correct version:
QLabel* test2 = new QLabel("Font-weight testing");
QFont font = test2->font();
font.setWeight(QFont::Bold);
test2->setFont(font);
Also you have two errors in the QSS version. First, you didn't specify a selector for your rule. Second, the value of 400 corresponds to 'normal' font.
https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
The correct version:
QLabel* test3 = new QLabel("Font-weight testing");
test3->setStyleSheet("QLabel { font-weight: bold; }");
Use function setWeight like this: setWeight(QFont::ExtraBold);
QFont font;
font.setWeight(QFont::ExtraBold); // set font weight with enum QFont::Weight
font.setPixelSize(25); // this for setting font size
ui->label->setFont(font);
void QFont::setWeight(int weight):
Sets the weight the font to weight, which should be a value from the QFont::Weight enumeration.

Qt5/C++: change style of a QIcon inside a QToolBar

I'm adding actions to a QToolBar only using icons and an empty text, and I want to change each action style when it is triggered (specifically, changing its border color):
toolbar = new QToolBar;
action1 = toolbar->addAction(my_icon1, "");
action2 = toolbar->addAction(my_icon2, "");
QObject::connect(action1, &QAction::triggered, [this]{
// change border color of action1
// unset border color of action2
});
QObject::connect(action2, &QAction::triggered, [this]{
// change border color of action2
// unset border color of action1
});
But since a QIcon is not a widget (not a QAction of course), I don't know where to set the style of a specific action, and QAction::associatedWidget() returns the QToolBar widget and not the associated button that owns the icon.
I'm using only C++ code, without QML or ui files.
Since QAction::parentWidget and QAction::associatedWidgets both contain the QToolBar instead of the actual action widget, I didn't give initial credit to QToolBar::widgetForAction (I thought it would be some kind of convenient function for the QAction:: methods above). But it deserves it, because it returns the actual widget for that action, as the function name says:
toolbar->widgetForAction(action1)->setStyleSheet
("QWidget { border: 1px solid blue; }");

Qt GUI Design - Is there an invisible character suitable for label text?

Okay I'm working on a GUI with Qt Designer for c++, and I have a bunch of labels setup to display some content from a file once it is loaded.
On the labels that are used to display loaded content, I currently have them set to "set later", and in the initial GUI configuration, I call ui->label_id.setText(""); on each of the labels before they can be seen. I really just want them to have some text so I can visually see them in the designer, yet not have any when the program is running before content is loaded.
So my question is: Is this the best way to handle this? Or is there a character I can set the text to in designer that won't appear at runtime?
With more and more labels as my gui expands, I don't want to bog down the loading time of the program setting the text to "" for every one, so that's why I'm here.
One possible solution could be to set a color to that labels with a transparent color (0 alpha value).
This can be handled via a style sheet in the widget constructor:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setStyleSheet("QLabel { color: qrgba(255, 255, 255, 0); }");
}
If you want to selectively apply a stylesheet to a label, you could use all kinds of Qt Style Sheet Selector Types.
You could use the ID Selector for example. If you've set in QtDesigner the objectName attribute of a label to label_1, you hide this label selectively by setting this style sheet:
setStyleSheet("#label_1 { color: qrgba(255, 255, 255, 0); }");
A more advanced solution would be a method that returns the style sheet for the object names:
QString MainWindow::styleSheetForHiddenLabels(const QStringList &labelObjectNames)
{
QString styleSheet;
foreach (QString labelName, labelObjectNames) {
QString style("#");
style.append(labelName);
style.append(" { color: qrgba(255, 255, 255, 0); }");
styleSheet.append(style);
}
return styleSheet;
}
This method could be used this way:
QStringList labelNames;
labelNames << "label1" << "label2" << "label3";
QString stylesheet = styleSheetForHiddenLabels(labelNames);
setStyleSheet(stylesheet);
An alternative to the stylesheet answer.
QList<QLabel*> widgets = this->findChildren<QLabel*>();
for (QLabel *label : widgets) {
label->setText("");
}
place this in your MainWindow class constructor. It will get a list of all QLabels from your window, you can then cycle through them and set the text to "".
You can use Object inspector. In Object inspector you'll se a list of your labels, and when you click on some of your labels you'll see border around your widget. That way you can see your labels without putting any text in it.

Qt - Get QPushButton icon name

I have a two state QPushButton. I want to associate an icon to each state.
It is like Play|Pause buttons in music players.
To do so, I would like to get the current icon name in order to know what the next icon to set will be.
I could subclass QPushButton but is it worth it?
Instead of setting an icon based on the QPushButton's state, set one QIcon that has two states, Qt will select the correct icon if you use it with a checkable QPushButton.
QIcon icon = QIcon();
// 'Off' state corresponds to unchecked state of QPushButton
icon.addPixmap( QPixmap( ":/img/play.png" ), QIcon::Normal, QIcon::Off );
// 'On' state corresponds to checked state of QPushButton
icon.addPixmap( QPixmap( ":/img/pause.png" ), QIcon::Normal, QIcon::On );
QPushButton * button = new QPushButton();
button->setIcon( icon );
button->setCheckable( true );
Use QPushButton::icon() and QIcon::name() to get the icon name.

How to change the background colour of a wxTextCtrl without breaking the inactive selection colour?

I want to signal an input error in a wxGTK application by setting the background of a text field to red on error and to white on successful input. However, when the background color is set via SetBackgroundColor, the background colour of an inactive selection is set to the same color. This leads to a very undesirable situation when setting the background colour to white: Since the foreground color for selected text is white, and the background colour for selected text is now also white, the text is unreadable.
How can I reset the colours on a wxTextCtrl so that inactive selected text has a grey background (the default setting before SetBackgroundColour)? SetBackgroundStyle( wxBG_STYLE_SYSTEM) was my first guess, but has no effect on wxGTK.
Code example:
#include <wx/textctrl.h>
#include <wx/frame.h>
#include <wx/defs.h>
#include <wx/app.h>
class App : public wxApp {
bool OnInit() {
wxFrame* frame = new wxFrame(NULL, wxID_ANY, wxT("Frame"));
wxTextCtrl* text = new wxTextCtrl( frame, wxID_ANY, wxT("foo bar") );
text->SetBackgroundStyle( wxBG_STYLE_COLOUR );
text->SetBackgroundColour( *wxWHITE );
frame->Show();
return true;
}
};
IMPLEMENT_APP( App );
You could try working with SetDefaultStyle, I didn't try this myself but here is some excerpt from the wxwidgets documentation:
text->SetDefaultStyle(wxTextAttr(*wxRED));
text->AppendText("Red text\n");
text->SetDefaultStyle(wxTextAttr(wxNullColour, *wxLIGHT_GREY));
text->AppendText("Red on grey text\n");
text->SetDefaultStyle(wxTextAttr(*wxBLUE);
text->AppendText("Blue on grey text\n");
This will most likely allow you to change the colour independantly of the text and/or change the colour of the text itself as well. Here is the link to the wxTextCtrl Documentation where I found this code snippet:
http://docs.wxwidgets.org/2.8/wx_wxtextctrl.html
Regards,
Roin
Tried this and it works:
TextCtrl1->SetBackgroundColour(wxColour(0xFF,0xA0,0xA0));
TextCtrl1->SetStyle(0, -1, TextCtrl1->GetDefaultStyle());