#include <QtGui>
class Label : public QLabel
{
public:
Label(QWidget *parent =0) :
QLabel(parent)
{
resize(100, 100);
setText("hello");
show();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Label l;
return a.exec();
}
This outputs a label 'hello' with a background. I'm using Qt4. I want to make the background of this label completely transparent. But setWindowOpacity changes the whole widget transparency. I want the content as it is, but only the background to be transparent.
I found this as simpler....
QWidget::setAttribute(Qt::WA_TranslucentBackground);
You can use stylesheets to set the color and the alpha value of the background:
setStyleSheet("background-color: rgba(0,0,0,0%)");
In PyQt:
lbl.setAttribute(Qt.WA_TranslucentBackground, True)
If you define a QColor with alpha of 0 you will get background with transparent color, so for example:
QColor bg_color(255, 0, 0, 0);
QPalette p(l.palette());
p.setColor(QPalette::BackgroundColor, bg_color);
l.setPalette(p);
Which should make label's background whatever color transparent.
In my case with Qt5, the following worked:
movieLabel->setAutoFillBackground(false);
Tested both with Qt 5.11.1 and Qt 5.8.0.
Related
I have a problem when use QGraphicsView and QGraphicsBlurEffect in my project. When I put them together, my program does not work normally. I wrote a tiny program to reproduce this problem.
The Widget class is inherited from QGraphicsView.
Widget::Widget(QWidget *parent)
: QGraphicsView(parent)
{
scene = new QGraphicsScene(this);
this->setScene(scene);
label = new QLabel;
QPixmap pixmap = QPixmap("../partly_cloudy.png").scaledToWidth(200);
label->setPixmap(pixmap);
label->setGeometry(100,100, pixmap.width(), pixmap.height());
label->setStyleSheet("border:3px;border-color: rgb(255, 100, 0); border-style:solid;");
/* ********image won't show when adding following comment code********
QGraphicsBlurEffect *blur = new QGraphicsBlurEffect;
blur->setBlurRadius(10);
label->setGraphicsEffect(blur);
******* */
QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
proxyWidget->setWidget(label);
proxyWidget->setPos(10,10);
scene->addItem(proxyWidget);
}
and main.cpp is as follows.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
This is a screenshot when QGraphicsBlurEffect is not used.
However, this is a screenshot when QLabel uses setGraphicsEffect() to bind blur effect.
To solve this problem, I tried to use a QWidget to wrap QLabel. When I did this, QLabel was rendered. However, it seems to be bounded by a rectangle area.
Widget::Widget(QWidget *parent)
: QGraphicsView(parent)
{
scene = new QGraphicsScene(this);
this->setScene(scene);
/* ********/
container = new QWidget;
container->setStyleSheet("border:3px;border-color: blue; border-style:solid;");
/******** */
label = new QLabel(container);
QPixmap pixmap = QPixmap("../partly_cloudy.png").scaledToWidth(200);
label->setPixmap(pixmap);
label->setGeometry(100,100, pixmap.width(), pixmap.height());
label->setStyleSheet("border:3px;border-color: red; border-style:solid;");
QGraphicsBlurEffect *blur = new QGraphicsBlurEffect;
blur->setBlurRadius(10);
label->setGraphicsEffect(blur);
QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
proxyWidget->setWidget(container);
proxyWidget->setPos(80,80);
qDebug() << proxyWidget->boundingRect();
scene->addItem(proxyWidget);
this->setSceneRect(0,0,640,480);
}
The screenshot of the result is.
I tried to set proxyWidget position to {0,0}, and it works normally. it seems that the position of effect rectangle will not influenced by proxyWidget position.
By the way, the version of Qt is 5.14.2.
I've searched for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
I was trying to change the background color of a QPlainTextEdit widget to black and apply a glow effect to it. But when I apply a DropShadowEffect to a QTextEdit or a QPlainTextEdit widget, its background color reverts back to the original and refuses to change. I'm using Qt version 5.12.2. Here's the code:
#include <QtWidgets>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QMainWindow window;
window.setWindowTitle("Title");
window.setStyleSheet("QMainWindow{background-color: #191b21}");
window.setFixedSize(800, 600);
window.show();
QPlainTextEdit* w = new QPlainTextEdit(&window);
w->setGeometry(250, 50, 300, 50);
w->setStyleSheet("QPlainTextEdit{background-color: black; color: white;}");
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setColor("#59f1ff");
effect->setBlurRadius(30);
effect->setOffset(.0);
w->setGraphicsEffect(effect);
w->show();
return app.exec();
}
I've tried using QPalette but it won't work too. What am I doing wrong?
I have in my Qt code a QLabel with a defined background color.
I would in a function to change the background color for one second only and then set it back to the original color.
I thought about using a sleep() function but is there a way to do that without blocking the rest of the program activities ?
Thanks!
You have to use a QTimer::singleShot(...) and QPalette:
#include <QApplication>
#include <QLabel>
#include <QTimer>
class Label: public QLabel{
public:
using QLabel::QLabel;
void changeBackgroundColor(const QColor & color){
QPalette pal = palette();
pal.setColor(QPalette::Window, color);
setPalette(pal);
}
void changeBackgroundColorWithTimer(const QColor & color, int timeout=1000){
QColor defaultColor = palette().color(QPalette::Window);
changeBackgroundColor(color);
QTimer::singleShot(timeout, [this, defaultColor](){
changeBackgroundColor(defaultColor);
});
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Label label;
label.setText("Hello World");
label.show();
label.changeBackgroundColorWithTimer(Qt::green, 2000);
return a.exec();
}
Maybe you can use QTimer to wait a second. Use timer->start(1000) to wait a second and create a SLOT in your class that recive the Qtimer::timeOut signal to changeback the label background color.
i'm trying to make a qt widget that shows a table of qlabels displaying hex numbers.
i pass the numbers to the labels as qstrings ready to be printed and the labels work properly but the font type is the system default (a sans serif) that has different letter sizes, so the numbers containing "A-F" digits are no more aligned with the other numbers...
i initially create the font with the function:
static const QFont getMonospaceFont(){
QFont monospaceFont("monospace"); // tried both with and without capitalized initial M
monospaceFont.setStyleHint(QFont::TypeWriter);
return monospaceFont;
}
and create a custom QLabel class that has this constructor:
monoLabel(QWidget *parent = 0, Qt::WindowFlags f = 0) : QLabel(parent, f) {
setTextFormat(Qt::RichText);
setFont(getMonospaceFont());
}
but it doesn't work, so i add to the main file
QApplication app(argn, argv);
app.setFont(monoLabel::getMonospaceFont(), "monoLabel");
and again the font remains unchanged..
i searched the net for font-setting problems with QLabels but i seem to be the only one who doesn't get them to work properly..
what am i doing wrong??
You probably want a Monospace style hint, not Typewriter. The following works for me on OS X under Qt 4 and 5.
Setting QLabel to rich text is unnecessary for your application.
Note that QFontInfo::fixedPitch() is not the same as QFont::fixedPitch(). The latter lets you know whether you requested a fixed pitch font. The former indicated whether you actually got a fixed pitch font.
// https://github.com/KubaO/stackoverflown/tree/master/questions/label-font-18896933
// This project is compatible with Qt 4 and Qt 5
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QtWidgets>
#endif
bool isFixedPitch(const QFont &font) {
const QFontInfo fi(font);
qDebug() << fi.family() << fi.fixedPitch();
return fi.fixedPitch();
}
QFont getMonospaceFont() {
QFont font("monospace");
if (isFixedPitch(font)) return font;
font.setStyleHint(QFont::Monospace);
if (isFixedPitch(font)) return font;
font.setStyleHint(QFont::TypeWriter);
if (isFixedPitch(font)) return font;
font.setFamily("courier");
if (isFixedPitch(font)) return font;
return font;
}
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QString text("0123456789ABCDEF");
QWidget w;
QVBoxLayout layout(&w);
QLabel label1(text), label2(text);
label1.setFont(getMonospaceFont());
layout.addWidget(&label1);
layout.addWidget(&label2);
w.show();
return a.exec();
}
I want to change the background color of a QPlainTextEdit, how do I do this?
Modify the palette of your plain text edit. Sample program:
#include <QApplication>
#include <QPlainTextEdit>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPlainTextEdit edit;
QPalette p = edit.palette();
p.setColor(QPalette::Active, QPalette::Base, Qt::red);
p.setColor(QPalette::Inactive, QPalette::Base, Qt::red);
edit.setPalette(p);
edit.show();
return app.exec();
}
Substitute whatever color you want, of course.
If QPlainTextEdit supports style sheets, you could do it like this:
myPlainTextEdit->setStyleSheet("background-color: yellow");
or
qApp->setStyleSheet("QPlainTextEdit {background-color: yellow}");
Slightly confusingly they call it role rather than colour/color.
https://doc.qt.io/qt-5/qwidget.html#setBackgroundRole
hint - if you can't find a function for a particular control, click on show inherited members - most general settings are in qWidget which is the basis for eveything drawn on screen.
May be you need to call QPlainTextEdit::setBackgroundVisible(true).
In order to modify the background, you need to modify the palette of your QPlainTextEdit and to set background visible:
myPlainTextEdit->setPalette(QPalette(/*Select the constructor you need*/));
myPlainTextEdit->setBackgroundVisible(true);