Adding image to QPushButton on Qt - image

I'm fairly new to C++ in general, so I need a little help with Qt. I'm trying to add an image to a PushButton, and I keep having problems with it. Here is an example of what I have:
#include <QtWidgets/QPushButton>
QPushButton *button;
button = new QPushButton(Example);
button->setObjectName(QStringLiteral("button"));
button->setGeometry(0,0,128,56);
So I have a picture saved in /example/pics/example.png (example being the project name), and I would like to use it on the PushButton. I've been messing around with it for a while, and can't find a solution, so any help is appreciated.

button->setIcon(QIcon("/example/pics/example.png"));

In pyqt5/pyside2, this is what I used:
icon = QIcon()
pixmap = QPixmap(r'C:\Users\git\Desktop\test.png').scaled(QSize(160, 90))
icon.addPixmap(pixmap, QtGui.QIcon.Normal, QtGui.QIcon.Off)
pushButton.setIcon(icon)
pushButton.setIconSize(QSize(160, 90))
pushButton.setStyleSheet("QPushButton{border-radius:5px;border: 1px solid #345781;}")

Related

Is it possible to select part of desktop with open windows like browers and screen it?

I'm trying get screenshot of choosen area of screen. Screen I mean desktop with opened folders, browsers etc.. The problem is everything is outside java program windows, and it shoud be like that.
I'm adding screen to explain you better.
And result
How it should be done? By other scene that I will put in the region and resize it and get what's under it? Or maybe is here something easier to use?
If I understood it right you want to make a screenshot of a certain area of the Desktop so you have first take a screenshot:
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
Then I would just crop it with this:
BufferedImage croppedImage = capture.getSubimage(x, y, width, height);
I hope this helps you :)

Setting an Image to a label in Qt

I need to get a label image into a Qt Widget
I have tried putting a label on the widget then just on its properties on the right under text i selected pixmap and selected the image I wanted to use.
However when I run the program no image appears any ideas why this is?
I have also tried :
QLabel label("<img src='image.jpg' />");
label.show();
But had no luck do not think I was using it right
Official way of "adding image to QLabel" provided by Nokia Corp. A QPixmap is used.
QLabel label;
QPixmap pixmap(":/path/to/your/image.jpg");
label.setPixmap(pixmap);
label.setMask(pixmap.mask());
label.show();
QIcon searchIcon = Icon::fromTheme("edit-find");
searchLabel->setPixmap( searchIcon.pixmap(16,16) );
Another way is to use Qt Style Sheets.
QLabel label;
label.setStyleSheet("background-image: url(:/images/assets/your.png)")

QGraphicsView / QGraphicsScene image size

I want to display an image in a QGraphicsView with a QGraphicsScene. My code is very basic :
QGraphicsScene* scene = new QGraphicsScene(this);
scene->addPixmap(QPixmap(qApp->applicationDirPath() +"/france.gif"));
ui->myGraphicsView->setScene(scene);
ui->myGraphicsView->fitInView(scene->sceneRect());
My problem is that my picture has a wrong size. It is very small, with wrong proportions and on the center of my GraphicsView. With qDebug i kwow that my picture is load successfully and it has a size of about 100x800px. My graphicsView is smaller so i want to resize my picture to adjust it with my GraphicsView size.
The graphicsView is set in the mainwindow Form and the scene is declared in the header : "QGraphicsScene scene;"
I try everything is possible in the world (i think) but the graphicsView is alaways blank or contains the small version of the picture. When I copy/paste some codes of internet i always get this problem. I also try with this example : Qt GUI Development - Displaying a 2D grid using QGraphicsView , same problem...
Maybe i'm very very very tired but i really don't understand what is wrong. Help me please :)
sceneRect() may not be what you think it is unless you specifically set it. Also you are missing aspectRatioMode in fitInview call which can distort it image, sometimes resulting in "small" appearance.
QGraphicsScene* scene = new QGraphicsScene(this);
QGraphicsPixmapItem p = scene->addPixmap(QPixmap(qApp->applicationDirPath() +"/france.gif"));
ui->myGraphicsView->setScene(scene);
ui->myGraphicsView->fitInView(p, Qt::KeepAspectRatio);
From my experience, a shown image is very small if we try to fitInView before the scene window is displayed. Technically, the sceneRect size is not set in the way we want until it is actually shown. Therefore, we need to let the scene window shows up before we can properly use fitInView.
Well, I think the solution is available here
Qt5 C++ QGraphicsView: Images don't fit view frame

QT QPushButton with an icon and overlaid centered text

I recently took to Qt Programming and want to port my app to Qt.
I'm fiddling with QPushButtons and I've managed to display an image into the button and to set some text to the button but, whatever I do, with or without the designer,
I have the same problem, the text is aligned right to the icon instead of being overlaid over the icon.
addButton = new QPushButton();
addButton->setIcon(QIcon(":/AddButton.png"));
addButton->setText(tr("+"));
addButton->setFlat(true);
addButton->setIconSize(QSize(100,90));
What am I missing ?
I know there is the toolButton but it doesn't seem to have the "flat" property.
Any idea please ?
Thanks a lot,
Mike
If you are trying to use your image as a background image, you can use a style sheet:
addButton->setStyleSheet("background-image: url(:/AddButton.png);"
"background-repeat: no-repeat;"
"background-position: center center");
You'll just have to make sure the size of your button is at least as big as the image.

How do I style an img with CSS3

is it possible to change the background-color of e.g. the first icon of this site with CSS3, or WebKit’s CSS extensions?
I'm new to this and would appreciate some help. A link or an example would be great.
Thank you in advance.
Although you can style an Image, but changing color of Icon is not possible in CSS3 as it requires Blending modes.
You can achieve this with HTML5 Canvas
// Color
var over = someCanvas.getContext('2d');
// Icon
var under = anotherCanvas.getContext('2d');
over.blendOnto( under, 'screen', {destX:30,destY:15} );
use https://github.com/Phrogz/context-blender for achieving this.
If the icon image had a transparent background, then you could set the CSS background colour behind the image like this:
background: #c00 url(kombine-iphone-icons.png); /* Puts a red background behind the icon image */
Unfortunately, there’s nothing in CSS 3 (or any of WebKit’s CSS extensions) that lets you change the colour of images.
Try
img {
background-color: #FF00FF;
}
Yes, you can style an image with css3. But something tells me that you question is actually more specific that it looks like..

Resources