Adding an image in Qt Designer?

  • I want to make a button with an image on it, but apparently I need a QRC file or something?
    Why can't I just add jpegs? :c

  • Moderators

    No, "setIcon": requires only a valid QIcon object. The source file can be any file on your local drive or inside QRC resource.

  • You can

    create a new resource file

    add the jpeg to the resource file

    run qmake

    add the resource as icon to your button

  • but when I create a "push button" by dragging one across and right click it then click "Change StyleSheet..." I get a box up, and from that box it looks like I can add images, but I can only add qrc files, so how do I get a qrc file with images in it? =]

  • To get the resource file:

    create a new resource file in your project

    add the image file, then

    run qmake (or the images will not show up in the UI designer)

    In the UI designer

    • select an icon for your button, you should get a dialog where you can choose the image that should be on that button.

  • You must look at the properties on the bottom right hand side of the QtCreator window for the selected QObject. There is a setting to set the icon by clicking the "..." button, as well as changing the icon dimensions on the button towards the bottom of the settings.

    QToolButton as a little more flexibility with icon / text positioning but is mostly used in a toolbar.

    You can also set the icon on the button programmatically if you want to by referencing the image in a resource file or on the local system if you plan on shipping it with the application.

  • Moderators

    You can also use non-resource images in stylesheets. There's just no button for it in the designer so you need to type it manually, eg.
    background-image: url(whatever\image.jpg)
    but this is discouraged, because you need to be careful not to use absolute paths (will break deployment) and there are more files to move around. Resources are packed into the executable so there're few less problems to remember later.

  • I was just using Designer, I didn't even open up Creator, I feel abit stupid now :L
    Thanks everyone =]

  • How do I run Qmake?

  • From command line depends on your OS. Form example in Linux

    From QtCreator

    • Right click on a project and select "Run qmake"
    • Menu "Build" then "Run qmake"

  • Moderators

    [quote author="Fatal" date="1399908737"]How do I run Qmake?[/quote]

    If you use Qt Creator, it will do it automatically. If not, you can run it from the console (qmake is either in your PATh or in <qt dir>/qtbase/bin).

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.