PDA

View Full Version : Icon only button



bruccutler
19th January 2007, 17:28
I want a series of buttons <>^V (left, right, up, down). How do I create a button with only an icon/image, on it? When I use the QPushButton(icon, text) constructor all I get is the text.
- Bruce

jpn
19th January 2007, 17:38
One possibility is to use QToolButton and it's built-in arrow type (http://doc.trolltech.com/4.2/qtoolbutton.html#arrowType-prop) property.

bruccutler
19th January 2007, 18:01
I was attempting to use a 'gif" file. Once I converted it to a jpg file, it worked. I even gave the "gif" as the file type. The problem is, the file type for the load() API is not well documented. I couldn't tell what valid formats are handled on Windows and which are not. Anyway, once I converted the gifs to jpgs, it seems to work.

Thanks for your help.
- Bruce

bruccutler
19th January 2007, 18:02
I will also consider using the QToolButton as well in the future.

Again, thanks for your help
- Bruce

jacek
19th January 2007, 18:07
The problem is, the file type for the load() API is not well documented. I couldn't tell what valid formats are handled on Windows and which are not.
I guess you have just missed the "See also Reading and Writing Image Files." part of the docs.

flare
19th January 2007, 18:08
Are you sure the QIcon you pass in QPushButton has its image loaded? The following code should work:


#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton *pushButton = new QPushButton(QIcon("path_to_your_icon"), "text", 0);
pushButton->show();
return app.exec();
}

Please check that the path to the icon is valid (or better use the resource system of Qt). Next you may try it without the text and set the QPushButton's flat-property to true (might look better with icon-only buttons).

bruccutler
19th January 2007, 18:37
I still would like to know why it failed to load the .gif file when it succeeded with the .jpg. But for now, I'm moving on.

Thanks for all the help.
- Bruce

jpn
19th January 2007, 18:49
Qt doesn't support GIF by default, due to licensing issues. You have to recompile Qt with GIF support turned on. See "configure -help" for details.

VireX
19th January 2007, 20:14
I never am able to do this:
I did what the previous poster said,

#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{

QApplication app(argc, argv);
QPushButton *pushButton = new QPushButton(QIcon("TestQT.ico"), "text", 0);
pushButton->show();
return app.exec();

}



However, even though TestQT.ico and this program are in same folder, it does not work... neither does C:/blabla.... What do I do?

jpn
19th January 2007, 20:20
ICO is neither a supported format. It is available as a commercial solution (http://www.trolltech.com/products/qt/addon/solutions/catalog/4/Utilities/qticoimageformat/), though. As far as I remember, there is some open source implementation out there as well.

You can simply use supported image formats, like PNG for constructing a QIcon.


Use the QImageReader::supportedImageFormats() and QImageWriter::supportedImageFormats() functions to retrieve a complete list of the supported file formats.