Customise QPushButton with an image - qt

I am using Qt5 and having some problems in setting the back ground image of a QPushButton. I am using Qt Creator and Design to drag and drop the QPushButton. After that I created a source folder and placed an image in that directory and I googled and tried doing
ui->pushButton_play->setStyleSheet(QString::fromUtf8("background-image: url(:/source/play.png);"));
but no success...am I creating the folder in a wrong place?..I created in D:\Qt5_PROJECT_NAME\source or will I make the url(:\\source\\play.png). Nothing is working...please help...

Did you add the images file into .qrc file? A URL start with : has indicated that It will be searched in .qrc file. If .qrc file didn't used, URL should not start with :. Maybe you should access the image file as
ui->pushButton_play->setStyleSheet(QString::fromUtf8("background-image: url(source/play.png);"));
This is The Qt Resource System documentation.

Related

No headers or source files in QT creator

I'm having a problem with creating new header and source files in QT. I can only add new files to the build, it's grayed for the "main" project as you can see below.
I can create them specifically for the build and they only appear once in the documents area. There is no header tree branch. After I close qt I have to create new ones. I want them to appear in the tree and be connected with the main project. I have "hide source and headers files" option off.
I've obviously tried creating them the way it was shown on the yt. I've searched through settings but didn't found anything I could use.
You will have to add the files manually in CMake.
Go to File>New File and select what type of file you want. Typically it will be a source, header, or both (class).
Edit the CMakeLists.txt file:
qt_add_executable(myapp
myapp.qrc
main.cpp
new_file.cpp
new_file.h
)
Resources like .qml files and things like images (*.png *.jpg) are added in the myapp.qrc file

How to add the path of images in a QML project correctly

I'm going to practice this example and for that I created a Qt Quick Console 2 Application project named Main2 using Qt Creator 4.3.0 on a windows 7 machine.
The code for main.qml looks like this:
and I get the following errors in the Application Output window:
The program has unexpectedly finished.
C:\Users\Abbasi\Documents\QML\Main2\Main2\build-Main2-Desktop_Qt_5_9_0_MinGW_32bit-Debug\debug\Main2.exe crashed.
Starting C:\Users\Abbasi\Documents\QML\Main2\Main2\build-Main2-Desktop_Qt_5_9_0_MinGW_32bit-Debug\debug\Main2.exe...
QML debugging is enabled. Only use this in a safe environment.
What is the problem with the paths or anything please?
The images are in a folder named images on the Desktop. How to move them to a folder under the project, a step-by-step means please?
copy the images folder to your project folder
right click on your project - add new - Qt - resource file - images.qrc
right click on that - add existing files, goto /images and select what you want to add
alternatively, you can directly use the "add existing directory" option to add all files in a directory
Then from QML you simply:
Image { source: "images/whatever.jpg" }
This way the images are neatly packed inside your executable. Which is OK in most cases, unless you have gigabytes of images. It also helps prevent people replacing your stock images with profane versions ;)
Creating a separate resource file for images will do wonders for your build times.
If you are that keen on file system access, be that absolute (please don't do that ever in production) or relative, you will have to prepend a file:// to the path to tell Qt you want the file system rather than internal resources.
Update: As revealed by your main.cpp file, the actual problem is you are using a project template that uses QQmlApplicationEngine. The tutorial you are following dates back to the days before that, and uses QQuickView. The latter can have any QML element as a root, but the former needs to have an ApplicationWindow, which is the reason you are not getting any output. So you need to wrap your existing code in an ApplicationWindow:
ApplicationWindow {
visible: true
width: 640
height: 480
// your code goes here
}
use qrc: with image URL might work
Image { source: "qrc:images/whatever.jpg" }

Qt Image from resource file

I'm trying to insert an image to my program via resource file, which is like:
<RCC>
<qresource prefix="/">
<file>green.png</file>
<file>other files</file>
</qresource>
</RCC>
and when I'm trying to load it using QImage or QPixmap, like:
QImage *green = new QImage(":/green.png");
if(green->isNull()) qDebug("null");
I always see that null message, indicating that I'm doing something wrong. One solution may be using absolute path like
"C:\\Users\\user\\Documents\\project\\green.png",
which works of course, but I'd prefer implement it using resource file. Could you please give me some advice?
All this will work if your png files are located in the same folder as .pro, .qrc, and .cpp files of your project.
Usually it is convenient to put all images to special subfolder Resources, for example. Then in .qrc the line will look like:
<file>Resources/green.png</file>
And in .cpp file:
QImage *green = new QImage(":/Resources/green.png");
First of all, you need to create a .qrc file and add image folder to it
(image folder must be contained inside of the project folder)
Right-click on the project file
Add New
Qt
Qt Resource File press Choose and do other steps
after opening .qrc file you must press Add > Add Prefix > change prefix name if you want
again Add > Add File > and choose your images
then go to mainwindow.cpp (in my project ) file and call the images as below code
in my case the icon folder is Playericons
ui->play->setIcon(QIcon(":/Playericons/icons8-pause-30.png"));
Did you remember to run qmake after adding the resource file?

Opening a specified folder in Qt

Hello can anyone tell me how can I open a specified folder in Qt?
Suppose in a lineEdit I define the folder path like
D:/MyFolder
Then how can i open this folder using push button?
What should I use for that?
You can use:
QDesktopServices::openUrl(QUrl::fromLocalFile(yourFolderPath));
inside a slot connected to you button.

Qt how to access resources

Qt Creator give the possibility to attach some resource to the project.
I created in the project directory a folder called: Images. inside i have the file splash1.jpg
I created then the Resources file and attached the file as shown in the following figure:
Which is now the correct way to access from code such a file?
I tryed
QPixmap pixmap( "Images/splash1.jpg" );
QPixmap pixmap( "./Images/splash1.jpg" );
but none of them worked.
if i put just ./Images/splash1.jpg work at least if i compile by hand with qmake and make because has the correct path at runtime but no way to make it work within qt creator
Any idea??
Cheers,
Qt5 resources explains all you need. You have to put the colon before the path in the source tree. You also have placed a prefix, so :/Images/Images/splash1.jpg. This will give you a path.
If you need a URL, then use the qrc: scheme.
The correct way to load with Qt resources is: :/Images/Images/splash1.jpg.
What you could also do, is assign an alias to the resource. This way you don't need the .jpg: :/Images/splash
You can use ":/prefix/name of your image".

Resources