(Qt C++) QPushButton setIcon() not working - c++

I'm working on a project that has 40 checkable push buttons in a group and I want the icon of one button to change depending on a value I define. I'm not creating a new pushbutton, just changing the icon of the current one. So I'm testing it by trying to change one button's icon.
Here is my current code:
QPixmap b1d0(":/textures/blocks/textures/blocks/stone.png");
QIcon ButtonIcon(b1d0);
ui->slot_0->setIcon(ButtonIcon);
ui->slot_0->setIconSize(b1d0.rect().size());
The resource path was copied directly from my resource file so it is correct, I've messed with it like crazy but no change so...
slot_0 is my pushButton.
What did I do wrong? Or better yet, am I even allowed to change the icon of an existing pushButton?
Thanks for your time :)

If you are on Windows using MSVC as your compiler, and want to use Qt's resource system, you will need to add
Q_INIT_RESOURCE(res)
to the beginning of main(), where res is the name of your .qrc file without the ".qrc".
To get a full view of this, look at this gist:
https://gist.github.com/alexreinking/5992821

Related

Qt c++ : Dynamic GUI from multiple .ui files

I have been developing an GUI application in Qt. In which i need to load the GUI from different form files (.ui).
Please be patient as I am going to write a lot of things.
The idea is to keep my application completely flexible that the user can change the interface by simply editing .ui files and there will be no need to modify the source code for new element added to .ui files.
I am also trying to divide the software in multiple layers. (I'll try to clarify below.)
For example: (Please see the attached image. I have merged all the images in single file because my account is not authorised to add more than two images.)
I have the main window as show in image (Main)
The first.ui file contains primary widget which is tab widget and looks like as shown in image (First)
The second.ui file contains second widget which I need to add into the tab widget.
the second form looks like as shown in image (Second)
At the end the complete interface will look something like as shown in image (Final) Image Link Here
In the code I have done the following:
/*
Created a Main Window with menubar.
The menubar has an action to select and open a first.ui file.
The File opened is loaded by QUiLoader loader.
First.ui file containes a QTabWidget with two tabs and a label.
In label, i have given a path to another file second.ui
loader loads the opened file and returns the QWidget
The returned QWidget has been set as a CenntralWidget in MainWindow.
Then I have read the first.ui file to find the text given in label (which is the path to another file second.ui).
Once I have obtained that path, I have loaded the second.ui file using loader.
This file contais a widget having form layout and few buttons.
Now I am trying to add this obtained widget to the Tab loaded from previous file.
CASE 1:
I successfully managed to do it by using:
selectedTab->layout()->addWidget(otherWidgetObainedFromSecondFile);
but in this case, the problem is that, I cannot access the element of form and buttons.
That is, The buttons and form fields doesnt look like an active element.
They just look like a snapshot fixed in the tab.
CASE 2:
So, in contrary I decided not to directly add the widget to tab.
But i created another MainWindow, set the second widget as centralWidget to this new main window.
And then added this mainwindow to tab.
but it also gave me the same result.
*/
Please suggest me a solution.
I am open to different suggestions but I want to keep the idea same that is the GUI has to remain in layered form (loads from multiple ui files)
I will really appreciate your help.
You can use QFormBuilder for this.
Assuming your Tab Widget is named TabPane
QFormBuilder loader;
QFile file(":second.ui");
file.open(QFile::ReadOnly);
QWidget *pane=loader.load(&file,this);
file.close();
qDeleteAll(ui->TabPane->findChildren<QWidget *>(QString(),Qt::FindDirectChildrenOnly)); //remove any existing child widgets from TabPane
ui->TabPane->layout()->addWidget(pane);
If I understand you correctly you have to use addTab method of QTabWidget

how can change tittleBar button like close and minimize button in Qt QWidget

i want to change style of QWidget tittleBar button like close and minimize in windows but there is no similar stylesheet elements.
i am using Qt5.1 and windows7.
can any body help me?
If you are talking about how to do this windows, you can do:
this->setWindowIcon(QIcon(":myiconfile.png"));
You will have to add the file in your resources.
You can set the window icon on your widget as per documentation with the following property:
windowIcon : QIcon
This property holds the widget's icon.
This property only makes sense for windows. If no icon has been set, windowIcon() returns the application icon (QApplication::windowIcon()).
So, your code would look like something like this:
myWidget->setWindowIcon("/path/to/your/icon/file");
The icon file path can be relative and absolute as well as one coming from the resource system. The easiest way to test it quickly is just to use the absolute path, but if you plan to ship assets with your application to be self-contained, then you can use resource file with the resource system.

GLUT_ICON not working on top left window

am trying to add an icon application that uses freeglut, according to the doc here it seems is enough to have an icon resource GLUT_ICON.
Although it worked, it only appears on the icon of the console window and the executable in windows explorer, but the main window remains with the default windows icon (as shown in the image). I tried building without the console window mode, but I get same behavior, is this the way it suppose to work or am getting a wrong behavior?
This might help clear things for others running into the same problem:
https://www.gamedev.net/forums/topic/151647-opengl-glut-icon/
Basically, in the "file.rc"resource-file (right click view code) that appears after adding an ico resouce, where you should have
MAINICON ICON "compiler_assets\\icon-name.ico" //set main icon to "file"
You should add another line
GLUT_ICON ICON DISCARDABLE "compiler_assets\\icon-name.ico"
Which is basically setting GLUT_ICON to a specific .ico file
Finally fixed this problem. Whether this issues come from the VStudio2012 environment or not is uncertain.
But generating the resource id like this; GLUT_ICON creates a non-defined behavior. The exe is unable to identify this tag as a valid one, therefore no icon can be indexed and found.
By just changing to lower case the string, all problems get fixed.
I did this process manually and the fix seems to be consistent between PC's.

retranslateUi() clears QLabel text

My qt4-based application dynamically changes the GUI language by calling retranslateUi() function. It works like a charm, but when it processes the QLabel which text changes by the program, it tries to translate original text - the one, specified in Qt Designer, not the one, set by my program.
I understand it happens because retranslateUi() is auto-generated during build process so it couldn't possibly know about my changes, but I'd prefer to skip such QLabels completely. Is there a way to tell Qt to skip certain widgets in retranslateUi()? Alternatively, maybe I can disable content change for QLabel in my program?
I know I can sub-class QLabel and by overriding setText() solve this problem, but I prefer to use standard QLabel to be able to manipulate it using Qt Designer.
As I remember, in Designer you can uncheck on QLabel should it be translated. By default it is. Choose label you don't want to be translated, in property editor scroll to "text" property, expand it and uncheck translate checkbox. Then you should generate ui_*.h file again. After that your label shouldn't be in retranslateUi code

c++ icon in tray

i used qt to make an application in the tray. on my pc its a good project and i see the icon in the traybar, but when i release it to give it to sombody else they dont see the icon, its just an invissible square that can be used but it dont show the icon. but on my pc it shows the icon. :S i know it sounds weird but does anybody know how to solve it? or how to set the traybar icon in the resource file?
this->setWindowIcon(QIcon("favicon.ico"));
a.start();
createActions();
createTrayIcon();
trayIcon->setIcon(QIcon("favicon.ico"));
trayIcon->setVisible(true);
trayIcon->show();
Well, I assume that you forgot to provide the icon together with the executable. But in any case, it would be better to embed the icon as resource in the executable.
Have a look at the Qt resource documentation. Basically, you just need to create a resource file, add the icon and then change the filename to something like this ":/favicon.ico". By adding the ":/" in front of the filename, Qt knows to load this from the embedded resources.