How to create a QPicture from a resource in Qt? - qt

I have a resource at :images/pic.gif
Can I make a QPicture from this resource or does it have to be loaded from file?

QPicture is for recording QPainter commands, not for image data.
If you want to load an image, use QPixmap or QImage. You can use their load() methods.
To access an image file bundled as a resource, use ":/" to access the resource: ":/someFile.png". The exact paths and filenames are specified in the qrc files.

I think your question is like after adding images/pic.gif into the resource file whether it has to be accessed from the resource file or the directly the location of images/pic.gif.
Now my answer for the question is, even if you have added the images into resource file and accessing it, it will indirectly access the physical file itself. To illustrate this, after adding the images/pic.gif into the resource file, try removing the images/pic.gif from the location and access within your application. You will not able to get the image. So, even if you add it into the resource file it is similar to access the physical file present in its original location. Check out the Qt Resources page.
But it is a good practice and logical to organize your required files into a resource file so that all the required files are present in the same location.
Using QPicture you can load it into your application through
bool QPicture::load ( const QString & fileName, const char * format = 0 )

you have to use Qicon or Qpixmap to load the images from resource.
QPIcture is used to save the picture.

Related

How to specify path with ImageIO write in JavaFX?

I have a JavaFX program in which the user may choose a profile picture. This profile picture is applied and should then be saved to a resources folder at the same level as my bin and src folders. I have a few questions:
How do you define the file path? This is my current attempt. I get a NullPointerException, but I think that has something to do with the output stream. It's fixable, so I am mostly worried about the path. This works when accessing an image, so why doesn't it work when saving one?
// Store chosen profile picture
File outputFile = new File("/resources/profilePicture.png");
ImageIO.write(SwingFXUtils.fromFXImage(croppedProfilePicture, null), "png", outputFile);
When I go to deploy the application, will I get issues with saving to a resource folder at the same level as bin and src? Do bin and src even exist when you deploy an application?
Thanks. I can supply any more information if needed.

set default location to download to using the file class in flex

I have a exe file I want to download, using a url.
I'm using the File class and calling the download(url) function to download the file.
This lets me choose a place to download the file, but the default is either the path of the application directory or the last place the user choose to save a file. This sometimes causes problems because the user will try to save it to their application directory, but we don't allow them to have write access to the folder.
I want to be able to set the default to somewhere like the downloads folder. Is there a work around to achieve this?
update: Actually i found the answer. you just place the path in the file creation. var file:File = new File("path");
I'm guessing you meant to ask about the FileReference class because the File class doesn't have a download function.
From the asdoc on FileReference:
The FileReference and FileReferenceList classes do not let you set the default file location for the dialog box that the browse() or download() methods generate

Creating a new file without using a ServletContext

Assume I want to write to a new file created within the space of my webapp.
One way would be use getServletContext().getRealPath("/") and use that String to create a new file on the server. However, often I come across advice like not using getServletContext().getRealPath("/").
Can someone please let me know of another way to write a new file within my webapp?
Many thanks.
Have some configuration property containing the absolute path of a directory outside of the webapp and web server path, read this path from the configuration property, and write to this directory.
To serve files from this directory, write a servlet that takes the name or ID of this file as parameter, reads the file from the directory, and sends its content to the response.
This will
work even if the app is deployed as a war file and never unzipped to the file system
allow you to redeploy the next version of the app or server without deleting all the uploaded/created files
allow you to add whatever control you want on the uploaded/created files, instead of making them available to everyone
In short, treat these files as data, stored in a database which happens to be the file system instead of a SQL database.

Java EE - Best way to get real path to uploaded files? [duplicate]

This question already has answers here:
Recommended way to save uploaded files in a servlet application
(2 answers)
Closed 6 years ago.
I have a web application with an upload form where users can upload files.
I'd like to store the files a folder 'files'. And i'd like this folder to be placed directly under the webapp-root.
Using struts2, in my uploadFileAction, i can easily set this path with String uploadDir = ServletActionContext.getServletContext().getRealPath("/files")+ "/";
But when i'm trying to retrieve the files, apparently my bean which tries to access the file does not have access to getServletContext() and thus throws a nullpointer on getRealPath
Now im trying to figure out how i should approach this. I've heard about spring resource is the way to go, but i cant find any relevant examples.
Is there an easy way to get my paths? I just want the rootpath to my webapp. Nothing more nothing less...
I'd like this folder to be placed directly under the webapp-root.
This isn't going to work. All those files will get lost whenever you redeploy the webapp or even when you restart the server. Those files are namely not contained as part of the original WAR.
You need to store them in a fixed path outside the webapp's context. E.g. /var/webapp/uploads. You can configure this path in a properties file or as a system property.
I've found that a few ways to accomplish this regardless of what OS platform you're using. I typically like to store my files in some way relative to my web application. This being said, the easiest way to do this is using your class file as a reference to get the real path. I use something similar to the following to store image uploads for a few web applications that I've built:
public class FileLocationTest {
public static void main(String[] args) {
String path = FileLocationTest.class.getProtectionDomain().getCodeSource().getLocation().getPath();
System.out.println(path);
}
}
This can be done using a static utility class in your web app, or any class for that matter to obtain the real path of your application regardless of OS. Alternatively in a servlet I've also used the request class to get the Tomcat location if my appbase is in a different area, something like this:
String location = request.getClass().getProtectionDomain().getClassLoader().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
String path = location.substring(0, location.indexOf("/tomcat")) + "/data/events/images/";

Flash XML config file problems with asp.net MVC

I'm creating an asp.net MVC app, first time I've done this. I have a flash component I need to use in a view. I have included the SWF files etc in the Contents folder and referenced it from my view, the flash file loads when you get to the view, great.
The problem occurs because the flash file references and XML file for its configuration data, and I'm getting an error accessing that XML file. I'm guessing this is because flash is looking for a relative path and is using the URL for the page, which is obviously an MVC url and so does not refer to an actual location on disk, so the XML file is not there.
I guess the obvious answer is the alter the flash file to look in the contents folder for the XML file, but that means re-compiling the flash, and I know very little about flash so I'd like to avoid doing that. So is there any way to get the XML file to show up in the same URL as the view, so at the moment, the page with the flash component on is located at htttp://localhost/upload/ so I guess the XML file needs to be accessible from http://localhost/upload/flash-settings.xml?
If there's any other better way to do this, without editing the flash file, im open to that too,
Add this Action to the FlashUpload Controller:
public class FlashUploadController : Controller
{
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult FlashSettings()
{
var fileName = Server.MapPath("~/Contents/flash-settings.xml");
return new FilePathResult(fileName, "text/xml");
}
}
And this route to the RouteTable:
routes.MapRoute("FlashSettings", "upload/flash-settings.xml",
new { Controller = "FlashUpload", Action = "FlashSettings" });
You'll need to either set the routing mechanism to allow for direct files access to the /upload/ folder, or create a Controller Action which will return an XML stream (dynamic or the one read from the physical XML file), and point your SWF to that Route. I'd go with the second option, as it is much flexible.

Resources