QIcon not being displayed on QPushButton - c++

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!

Related

Qt Image won't show up in button

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

How to set text below the QToolButton in QT not below the icon

I'm using QToolButton and I set the icon.
Now I want Text "below the QToolButton", "Not below the icon".
Is there any way to achieve this in C++,QT in Linux ?
I've found myself in the same position a while back ago while I was making an application for an embedded Linux system.
I haven't found a straight forward solution (I was searching for a way to achieve it with CSS).
What I ended up doing, was creating a new QWidget (using the designer). Then placing the button in it with a QLabel under it.
Then added a simple static function
static void wdgCustomButton::create(const QString iconPath, const QString text)
{
// create a new button here, create some modification functions for
// text, image and optionally QStyleSheets.
// Call those here (pass the arguments)
// Then return the button
// pseudo code, (not tested):
wdgCustomButton button = new wdgCustomButton( /* could pass a parent */ );
button->setIcon( iconPath ); // function simply calls the ui->button->setIcon
button->setText( text ); // function simply calls the ui->label->setText
return button;
}
And then add those new QWidgets to your pannel using code (maybe someone knows how to get it in the default toolbar, but I haven't searched for that myself yet since I never needed it).
this->menuButtons[menuBtnsCount] = wdgCustomButton::create( ":/Images/Warning.png", "Delete everything" );
this->menuButtons[menuBtnsCount]->setGeometry( QRect( /* size and position here */ ) );
this->menuButtons[menuBtnsCount]->show();
I hope this might give you an idea to fix it in an easy way!
Edit:
I'm sorry, I forgot to add something about the click event. The click event was mainly why I made a QWidget out of it!
I just used the connect function [I belive on the whole button like: connect(this->menuButtons[0], ...]

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.

Use a pointer to switch between two other pointers in Qt Creator Ui

i do have two Labels in my applications. They are both in each tab. Now i want to witch between those labels by using an pointer that switchs between those labels when tab was changed.
my ui_mainwindow.h defines:
ProLabel *imageLabel;
ProLabel *imageLabel_1;
ProLabel *imageLabel_2;
but only imageLabel_1 and imageLabel_2 are shown in the GUI. I added a third Label (imageLabel) to use it as a variable to switch between the Labels (1/2). So I wrote follwing code in a slot which proves tab changed. So if tab is changed, the other imageLabel_1/2 (pointer) should be used in the hole code, when it says: imageLabel.
my slot when tab changed:
if(tab == 0)
{
this->ui->imageLabel = this->ui->imageLabel_1;
}
else{
this->ui->imageLabel = this->ui->imageLabel_2;
}
I also set the imageLabel_1 whe MainWindow is created. The Window disapear like normal. But if I try to load an image (than it should display the image in the imageLabel) it crashes.
Don't know if it was understandable. Does anybody has an idea if i can handle the pointers like that.. or how to do it different?
Thank you!
Two simpler ways: 1) Change the label contents, either the image or the text it contains, or 2) set the label to hidden with ui->label2->isVisible(false).

Using images in QListWidget, is this possible?

I am using QT to create a chat messenger client. To display the list of online users, I'm using a QListWidget, as created like this:
listWidget = new QListWidget(horizontalLayoutWidget);
listWidget->setObjectName("userList");
QSizePolicy sizePolicy1(QSizePolicy::Preferred, QSizePolicy::Expanding);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(listWidget->sizePolicy().hasHeightForWidth());
listWidget->setSizePolicy(sizePolicy1);
listWidget->setMinimumSize(QSize(30, 0));
listWidget->setMaximumSize(QSize(150, 16777215));
listWidget->setBaseSize(QSize(100, 0));
listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
Users are shown by constantly refreshing the list, like this: (Note: There are different channels, with different userlists, so refreshing it is the most efficient thing to do, as far as I know.)
void FMessenger::refreshUserlist()
{
if (currentPanel == 0)
return;
listWidget = this->findChild<QListWidget *>(QString("userList"));
listWidget->clear();
QList<FCharacter*> charList = currentPanel->charList();
QListWidgetItem* charitem = 0;
FCharacter* character;
foreach(character, charList)
{
charitem = new QListWidgetItem(character->name());
// charitem->setIcon(QIcon(":/Images/status.png"));
listWidget->addItem(charitem);
}
}
This has always worked perfectly. The line that I commented out is the one I have problems with: my current goal is to be able to display a user's online status with an image, which represents whether they are busy, away, available, etc. Using setIcon() does absolutely nothing though, apparently; the items still show up as they used to, without icons.
I'm aware that this is probably not the way this function needs to be used, but I have found little documentation about it online, and absolutely no useful examples of implementations. My question is, can anybody help me with fixing this problem?
This is how you may conduct your debugging:
Try the constructor that has both icon and text as arguments.
Try to use that icon in another context to ensure it is displayable (construct a QIcon with same argument and use it elsewhere, e.g. QLabel!).
Use icon() from the QListWidgetItem to receive back the icon and then look at that QIcon.
Create a new QListWidget, change nothing, and ordinarily add some stock items in your MainWidget's constructor. See if the icons show up there.