I'm doing:
QIcon(QPixmap::fromImage(img));
and then putting it to QToolButton with setIcon and setIconSize
I need to have my img surrounded with gray color inside this icon - how can I implement this?
alike there:
Setting a Stylesheet should do the job for you..
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QToolButton b;
b.setStyleSheet("QToolButton { background-color: grey }");
QImage img("C:\\Users\\Administrator\\Desktop\\Icon.png");
b.setIcon( QPixmap::fromImage(img) );
b.show();
a.exec();
}
Try this out.
You can also play around with the QPalette of the button, that also provides you a very fine control over the way you want your widgets to look.
Related
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?
#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.
I'm writing interface with C++/Qt in QtCreator's designer. What element to chose to make as a rect with some background image?
And the second question: how to draw tiled image? I have and image with size (1×50) and I want to render it for the parent width. Any ideas?
mTopMenuBg = QPixmap("images/top_menu_bg.png");
mTopMenuBrush = QBrush(mTopMenuBg);
mTopMenuBrush.setStyle(Qt::TexturePattern);
mTopMenuBrush.setTexture(mTopMenuBg);
ui->graphicsView->setBackgroundBrush(mTopMenuBrush);
QBrush: Incorrect use of
TexturePattern
If you just want to show an image you can use QImage. To make a background with the image tiled construct a QBrush with the QImage. Then, if you were using QGraphicsScene for example, you could set the bursh as the background brush.
Here is an example which fills the entire main window with the tiled image "document.png":
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QMainWindow *mainWindow = new QMainWindow();
QGraphicsScene *scene = new QGraphicsScene(100, 100, 100, 100);
QGraphicsView *view = new QGraphicsView(scene);
mainWindow->setCentralWidget(view);
QImage *image = new QImage("document.png");
if(image->isNull()) {
std::cout << "Failed to load the image." <<std::endl;
} else {
QBrush *brush = new QBrush(*image);
view->setBackgroundBrush(*brush);
}
mainWindow->show();
return app.exec();
}
The resulting app:
Alternatively, it seems that you could use style sheets with any widget and change the background-image property on the widget. This has more integration with QtDesigner as you can set the style sheet and image in QtDesigner.
I am trying to add a label to the main window using Qt. Here is a piece of the code:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget Main_Window;
QPixmap Image;
Image.load("1837.jpg");
QLabel i_label;
i_label.setPixmap(Image);
i_label.show();
QPushButton Bu_Quit("Quit", &Main_Window);
QObject::connect(&Bu_Quit, SIGNAL(clicked()), qApp, SLOT(quit()));
Main_Window.show();
return app.exec();
}
I've been having a very hard time figuring out how to properly add QLabels to QWidgets, I tried to set the Main_Window as the main widget using this method: app.setMainWidget(Main_Window) and the label was still outside the window. So how do I put labels into widgets using Qt?
hamza, this code worked fine for me:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget Main_Window;
QLabel i_label("Start", &Main_Window);
//i_label.setPixmap(QPixmap("1837.jpg"));
QPushButton Bu_Quit("Quit" , &Main_Window);
QObject::connect(&Bu_Quit , SIGNAL(clicked()), qApp , SLOT(quit()));
QVBoxLayout *vbl = new QVBoxLayout(&Main_Window);
vbl->addWidget(&i_label);
vbl->addWidget(&Bu_Quit);
Main_Window.show();
return app.exec();
}
I commented setting the image code to show you that the label was set correctly. Make sure your image is valid (otherwise you won't see the text). The trick here was that you need to use qt layouts like QVBoxLayout
Add the label to a layout widget and set the window layout to that layout.
Design note: its better to create your own MainWindow class, inheriting from QMainWindow for instance, and design it from the inside.
or even better, use QtCreator.
You can try:
QWidget window;
QImage image("yourImage.png");
QImage newImage = image.scaled(150, 150, Qt::KeepAspectRatio);
QLabel label("label", &window);
label.setGeometry(100, 100, 100, 100);
label.setPixmap(QPixmap::fromImage(newImage));
window.show();
this way you can even decide where to put the label and choose the image size.
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);