Setting an Image to a label in Qt - image

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)")

Related

Using Vector pdf Image in Xamarin IOS

In my Xamarin.IOS visual studio project, I wanted to use Vector pdf image in an UIImageView. But I am not able to add the vector image directly in the storyboard designer (also tried drag-drop).
Is there a way to use Vector pdf image in xamarin IOS?
Any sample would be much helpful.
Thanks.
Yes, As lowleetak said, We need to add the pdf file into the asset first.
Xamarin tutorial here , just follow the step ,it's simple.
Edit:
there is no option to select the Attributes Inspector for setting the Scales to Single Vector in Visual Studio. Is there an alternative for this?
AFAIK,there is no option to set the scale for the vector , maybe you could set this property when generate the pdf file.
I want to handle click event to capture the image X Y co-ordinates in C#. Is there a way to do this?
Override the touch event . documentation here.
Code :
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
var touch = touches.AnyObject as UITouch;
CGPoint point = touch.GetPreciseLocation(yourImage);
}
Update:
On iPhone5:
On iPhone6
And this are the constraints
You will need to add the pdf images to asset catalog first (Images.xcassets). You can refer to the guide HERE. It is a guide for Xcode but should be quite the same in Visual Studio.

Adding image to QPushButton on Qt

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;}")

iPhoto like NSButton

How can I create a iPhoto like button like in the picture
I've tried out several things using round textured buttons or setting the button's image as template. But none of these approaches really works.
Thanks so far for your answers.
EDIT:
The image of the button should just be a simple pdf. The gradient and the white shadow should be drawn automatically.
As Justin mentioned you should create an NSButton with type Momentary Change. You should then indicate that it is a template image by including the suffix Template in the filename, e.g. EditTemplate.png.
I know you said you tried setting the image as template, but I found that this wasn't always effective if I wasn't using the Momentary Change button type.
You could subclass NSButtonCell. And implement these functions:
- (void)drawImage:(NSImage*)image
withFrame:(NSRect)frame
inView:(NSView*)controlView
{
// Draw the image of the pen.
}
- (NSRect)drawTitle:(NSAttributedString*)title
withFrame:(NSRect)frame
inView:(NSView*)controlView
{
// Draw the text.
}
- (void)drawBezelWithFrame:(NSRect)frame
inView:(NSView *)controlView
{
}
To setup the subclass just edit the "Custom Class" field of the Button Cell in Interface Builder from "NSButtonCell" to "YourSubClass"
I think the simplest solution is to create button with type: Momentary Change and add on it image with transparent background with .png type of file and when button is ON You can change to other picture with background.
For example to set/change image:
[_yourButton setImage:_image];
If I have understood correctly what You want to do.

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.

Displaying an image when hovering a label

I'm wondering if there is a simple and quick way of displaying an image when the user hovers a QLabel...
Since QLabel can be used to display QPixmap, I would like to display some kind of "preview" when hovering a QLabel...
Do you guys know how I could manage this ?
Thanks in advance !
You can set the tooltip to use html, including <img> tags. Will that do what you need?
As described in the docs, you can override the object's event handler if you need to do something fancier in a tooltip than can be done by default. (such as selecting a particular area of an image to put in the tooltip)
As long as you set the button size large enough, you should be able to capture the mouse-over events and display on mouse-over... but this seems like a somewhat strange way to go about things. Why not display all the images at first?
For those who are interested in the result proposed by jkerian :
Simply :
MyLabel->setToolTip("<html><img src="+MyImagePathAsString+"/></html>");
Will result in :
Thanks again to jkerian.
I don't know what kind of layout your QLabel is in, but I'm guessing it will cause weird layout changes if you just set the pixmap on the label when mouseovering it. Rather use a tooltip or just display a pixmap freely on the canvas.

Resources