Qt Image won't show up in button - c++

I am creating a Qt application and I have an image that I want to use for a button instead of text. Unfortunately all that shows up is an empty button.
I've tried two different methods to get it to show up with the same results for both methods.
Code for Method 1:
ui->setupUi(this);
QPixmap pix(":/svg/resources/menu.svg");
int w = ui->menuButton->width();
int h = ui->menuButton->height();
ui->menuButton->setMask(pix.scaled(w,h,Qt::KeepAspectRatio).mask());
I found the info for the second method here: Adding image to QPushButton on Qt
Code for Method 2:
ui->setupUi(this);
QIcon icon(":/svg/resources/menu.svg");
ui->menuButton->setIcon(icon);
Could someone please help me figure out why my image isn't showing up and the button is just empty?

In my project using .svg images as button icons are no problem, maybe the button size is missing, try:
ui->menuButton->setIcon(QIcon(":/svg/resources/menu.svg"));
ui->menuButton->setToolTip("optional tooltip");
ui->menuButton->setFixedSize(QSize(28,28));
Assuming you stored your icons correctly in a resource file. If not, create a new:
right click on your top project folder (in the project-tree) -> Add new.. -> choose Qt on the left an Qt Resource File on the right window -> a new Window apears.
Add Prefix -> Add Files (your icon)

You use .svg image format. Are you sure your application load image format plugin for .svg? Image plugins must be in directory "imageformats" in current directory of your application. Avaliable plugins you can find in Qt directory .../Desktop/Qt/<version>/<mingw or msvc>/plugins/imageformats

Related

QIcon not being displayed on QPushButton

I'm learning how to use Qt for C++ programming. I have this image I want to display after when I click at a button and the position on a matrix related to that button is equal to -1, I also want to clear the text on that same button, my code up to now for that part is:
if(Tabuleiro[x][y] == -1){
this->Botoes[x][y]->setText("");
this->Botoes[x][y]->setIcon(QIcon("bomba.png"));
}
Being that Tabuleiro is a matrix of int, Botoes is a matrix of pointers to QPushButtons and "bomba.png" is the image I want to display. The image is in the same folder as the project, but once I run it is not displayed. I also tryed using Qt Resource system, I created a new resource called imagens.qrc on it I created a prefix /Imagem and placed my image there, this is what the code looked like after this:
if(Tabuleiro[x][y] == -1){
this->Botoes[x][y]->setText("");
this->Botoes[x][y]->setIcon(QIcon(":/Imagem/bomba.png"));
}
But it still won't work. What am I doing wrong? Also, I tryed using
this->Botoes[x][y]->text().clear();
instead of
this->Botoes[x][y]->setText("");
But it didn't work, do you know why?
Please include <QApplication> and <QStyle> and try:
this->Botoes[x][y]->setIcon( qApp->style()->standardIcon( QStyle::SP_MessageBoxWarning ) );
If it works (warning icon being displayed): then, it means you're not loading your resources correctly.
You can also check this:
QPixmap foo( ":/Imagem/bomba.png" );
bool found = !foo.isNull(); // true if png file was found, false if it was not
If false, again, it means you're not loading your resources correctly, if true, icon should be displayed in the button.
Morevover, you can also try this->Botoes[x][y]->setIconSize( QSize(16,16) ) because if someone earlier did this->Botoes[x][y]->setIconSize( QSize(0,0) ); your button icon will not be displayed!

QT QIcon properties for custom widget in designer

I have been working for a little while now on creating a QT custom designer widget for GUI menus. The idea being that you simply drag it into the designer, select the number of frames you'd like, how many buttons per frame, etc. and it generates and sizes everything for you.
The way the widget is structured there are properties to configure each button for the frame you are in. For example, you would use the button0Text field to enter text under Button0 while editing in frame 0, then use it again to edit Button0 which is in frame 1. Both buttons would retain the individual changes for each frame.
The Problem
Normally when I switch frames all of my properties are updated to reflect the status of the frame. The exception being QIcon. The correct icon is retained in the actual graphical representation and builds correctly, however the file path in the property list is always of the last edited for that property. I think this will be extremely confusing to an end user and I have found no way to fix it. So for example, if I set text and icons in frame 0 then switch to frame 1 the text in the property list will update to reflect the state of frame 1 but the icon path names will still show my last edit in frame 0 and not the actual icon in frame 1.
I have tried things as simple as:
setProperty("button0Icon", getButton0Icon());
That code works on properties like text, but not for the icon. I try executing it immediately after changing frames.
I have also tried:
#ifndef Q_WS_QWS
QDesignerFormWindowInterface *form = QDesignerFormWindowInterface::findFormWindow(this);
if(form){
QDesignerFormEditorInterface *editor = form->core();
QExtensionManager *manager = editor->extensionManager();
QDesignerPropertySheetExtension *sheet;
sheet = qt_extension<QDesignerPropertySheetExtension*>(manager, this);
int propertyIndex = sheet->indexOf("button0Icon");
sheet->setChanged(propertyIndex, true);
sheet->setProperty(propertyIndex, getButton0Icon());
}
#endif
And:
int propertyIndex = this->metaObject()->indexOfProperty("button0Icon");
QMetaProperty property = this->metaObject()->property(propertyIndex);
property.write(this, QIcon());
Nothing seems to update the property list in the designer.
I have all properties, including all QIcon properties properly declared in the header file with Q_PROPERTY and assigned getter and setter functions.
To be clear, the icon values are indeed retained through each frame when compiled. So it is functioning, just unclear for most users.
If anyone has any experience with this or any ideas please let me know. Thanks.
I have discovered that QIcon does not store file names/paths. The file names are only used for the creation of the QIcon. I think this is most likely the reason I do not get any feedback in the Property Browser for my QIcon properties.
Instead I have chosen to hide this property in the designer and add three new ones. Three QUrl properties, each of which is used to supply an image file. I use three because I want to construct a QIcon that contains Modes/States for normal, disabled, and pressed operations.
I take each of these QUrls and save them in QStringLists behind the scenes so their values are stored. I then construct my QIcon using the file names provided from the QUrls.
I would much prefer to be using the native QIcon in the designer for this, any thoughts or feedback are appreciated.

How to apply an image to a QPushButton in QT Creator

I can't manage to view any picture on Qobjects, like a QPushButton, using the setIcon() function.
This probably has to do with the image url being erroneously specified, inside the QPixMap pix("url") function. This is what I've got:
QPixmap pix(":/Resources/flag.png");
QIcon icon(pix);
button->setIcon(icon);
I have a flag.png file inside the directory "Other files/Resources/flag.png".
I really don't know how resources should be placed.
You have to create a Qt Resource file (File > New > Qt > Resource File). Then add a prefix into the resource file, then add a file to the resource file.
After all of that is done, right click on the listing for that file and select Copy resource path. Then paste that path into your cpp file, and surround it in quotes.
Hope that helps.

Setting icons in Unity3d standalone player

I am trying to use Unity, with a build script that creates my application by ultimately invoking BuildPipeline from the script. I am trying to figure out how to set up the icons, however. After calling PlayerSettings.SetIconsForTargetGroup and invoking BuildPipline.BuildPlayer, the appropriate icon does not show up for the executable file produced, nor display when the program is running.
I am currently using the following code.
Texture2D texture = AssetDatabase.LoadMainAssetAtPath(iconFile) as Texture2D;
int [] sizeList = PlayerSettings.GetIconSizesForTargetGroup(BuildTargetGroup.Standalone);
Texture2D[] iconList = new Texture2D[sizeList.Length];
for(int i=0;i<sizeList.Length;i++)
{
int iconSize = sizeList[i];
iconList[i] = (Texture2D)Instantiate(texture);
iconList[i].Resize(iconSize,iconSize,TextureFormat.ARGB32,false);
}
PlayerSettings.SetIconsForTargetGroup(BuildTargetGroup.Standalone,iconList);
What am I doing wrong?
Any assistance would be greatly appreciated. Thank you
Using BuildTargetGroup.Unknown works for me.
This also worked for me:
BuildTargetGroup.Unknown
But with an unwanted outcome: Unity does not override all icon sizes when setting one single Texture2D PNG, so Windows Explorer swaps the icon of my application EXE between the Unity default icon and my icon for different resolutions. Definitely, seems like a bug in Unity3D BuildPipeline.BuildPlayer(..) or SetIconsForTargetGroup(..).
If you're calling BuildPipline.BuildPlayer for OSX, then you need to specify full folder:
/MyPath/MyApp.app
i.e. don't leave out the ".app" part, because without that Unity builds the app, but doesn't include icons or splash image in resolution dialog.

Failing to remove attributes of Tree View in Qt

I am a newbie in QT. I am working on a app where I need to display the FileSystem using a treeview.
Basically I have a widget in my .ui file on which I have put a Treeview. Then in my .cpp file I have written the following code:
model = new QFileSystemModel(this);
model->setRootPath(QDir::homePath());
ui->treeView->setModel(model);
In my .h file I have put the following:
QFileSystemModel *model;
When I run the app, it displays the file system inside the treeview but it also shows Name, Type, Size, DateModified above it. I want to get rid of these.
Here is the sample Image:
How can I achieve it?
I think that QTreeView::hideColumn does it.