How to run application as administrator in Qt


How can I start my Qt application as administrator or get a prompt when admin privilege is needed? I have tried everything in the posts that I could find about it and most of them are a few years old. Nothing has worked and everyone seems to have a different spin on it. I have downloaded the windows SDK to get mt.exe, created the manifest files, the RC file and every other methods including right click and set the application to start in administrator mode. Nothing has worked. I need it because a task in my program requires administrator privilege. QFile::errorString() says access denied. Does anyone know how to make this work?


Okay so I found the answer to my question. I had found some solutions before, but they were not working. I found the reason why they were not working. The reason was very stupid: It was because the folder that my files were in had a space between the first and second word like (my app) which causes an error about not being able to find the location.It should be (myapp) with no spaces. The error about location not found was only happening when trying to add the adim privileges. Otherwise everything worked fine and there were no errors. So here is the solution and make sure there are no spaces in folder names.

1) Create the rc file :Open notepad and paste the following text inside then
save the file as yourappname.rc This creates the rc
file. Put the file in the folder that has all of your sourcecode and pro

#include <windows.h>

2) Create the manifest file with notepad, paste the following text, and save it
as yourappname.exe.manifest and put it in the same location as above.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity version="" processorArchitecture="X86"
  name="yourappname.yourappname" type="win32" />
  <description>A discription of your app</description>
  <dependency />
  <!-- Identify the application security requirements. -->
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">

3) In your pro file put the following text:

win32 {
    RC_FILE = yourappname.rc

That’s it. Now your application should start with administrator privileges.

There are similar post that describe this process too. Just make sure there are
no spaces in your folder names or it won’t work. Also you MUST run qt with
administrator privileges for it to work. Right click Qt and run as
administrator before trying to compile.

Answered By – kn0w0n3

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published