i am wondering if its possible to solve this problem.
Ive got qt application and if user tick the checkbox, i want this application to launch on startup of operating system.
Ive already googled, and ive come up with this solution>
my QT application needs admin privileges in order to modify registry, so
create manifest file ( <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>)
2.apply this command
mt -manifest manifestfile -outputresource:binfile.exe;1
3.use this piece of code in QT to modify registry
void MainWindow::set_on_startup() {
QSettings settings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
if (ui->checkBox->checkState()) {
QString value = QCoreApplication::applicationFilePath(); //get absolute path of running exe
QString apostroph = "\"";
#ifdef DEBUG
ui->textEdit->append(QCoreApplication::applicationFilePath ());
#endif
value.replace("/","\\");
value = apostroph + value + apostroph + " --argument";
#ifdef DEBUG
ui->textEdit->append(value);
#endif
//write value to the register
settings.setValue("name", value);
}
else {
settings.remove("name");
}
}
So, this looks good right ?
BUT... application with default admin priveleges cant be launched on startup of operating system, BUT application without admin priveleges cant modify registry. So , there is one solution - tell a user, that if he wants to set this "startup" option, he first needs to start application as admin, then the application will be able to modify registry, and default privileges will remain "asInvoker", but this seems really impractical and i think that users will be discouraged by this.
So, how to solve this problem ? how other applications solve this problem ?
You won't need admiministrator privileges if you use following key:
QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
Notice
HKEY_CURRENT_USER
instead of using
HKEY_LOCAL_MACHINE
My 2 cents! : )
Why not simply put the app shortcut in "Startup" folder.
Qt provides a cross-platform way of determining the paths to many default system directories using the QDesktopServices class.
(Source: Thanks to Dave Mateer for his answer to this question.)
The method is:
QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation)
This gives (on my Win 7):
C:\Users\user_name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
and all we need is:
C:\Users\user_name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Simple!
I use this without any hassle of UAC or any kind of rights problem in most of my apps.
This may not be the best way... but it is certainly an easy way.
(Please pitch in thoughts/comments if this approach has any big disadvantages.)
Update:
To create the short-cut for the application in startup folder, use this code:
QFileInfo fileInfo(QCoreApplication::applicationFilePath());
QFile::link(QCoreApplication::applicationFilePath(), QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation) + QDir::separator() + "Startup" + QDir::separator() + fileInfo.completeBaseName() + ".lnk");
I hope this helps! : )
Include this header QSettings
#include <QSettings>
And add this into your code.
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run",
QSettings::NativeFormat);
settings.setValue("YourApplicationName",
QCoreApplication::applicationFilePath().replace('/', '\\'));
For everybody who are trying to solve the problem, this is the 100% working solution:
How can I ask the user for elevated permissions at runtime?
create app1.exe
create app2.exe with admin priveleges - tutorial is in the first post (manifest file, mt.exe, etc..)
when user tick the checkbox in app1.exe, i call the the app2.exe (for example with no arguments) - you can find all function for this # the link ive just posted above
// well, in fact, you dont have to use the function from the example above:
i find this call much better
QObject *parent = new QObject();
QString program = AppToExec; //"/path/to/the/app2.exe"
QStringList arguments ;
arguments << ""; //just in case we want arguments
QProcess *myProcess = new QProcess(parent);
myProcess->start(program);
app2.exe, for example
QApplication a(argc, argv);
MainWindow w;
// w.show();
if (argc == 1) {
w.test();
a.quit();
}
problem solved.
Using this link
create stratup app and code:
void make_startup_app(){
QString appName = "app.exe";
QString appNameLink = appName+".lnk";
QFile::link(appName, appNameLink);
QString userName = QDir::home().dirName();
QString dir_startup = "C:/Users/" + userName +
"/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/"+ appNameLink;
QFile::copy(appNameLink, dir_startup);
}
Related
I'm developing an app which have to implement a WiFi connection mechanism (it'll be a fullscreen touchscreen app). To do that I'm using the DBus to communicate with Network Manager. I managed to successfully make it scan access points and list them in the QTableView. Next thing will be to connect to the chosen wifi network. First thing I do when trying to connect is checking if there is an existing connection to that access point already in the system so I do this:
settings = new QDBusInterface("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings", "org.freedesktop.NetworkManager.Settings", dbusConnection, this);
QDBusMessage msg = settings->call("ListConnections");
QDBusArgument arg = msg.arguments().at(0).value<QDBusArgument>();
QList<QDBusObjectPath> pathsList = qdbus_cast<QList<QDBusObjectPath>>(arg);
foreach (QDBusObjectPath p, pathsList) {
QDBusInterface *conn = new QDBusInterface("org.freedesktop.NetworkManager", p.path(), "org.freedesktop.NetworkManager.Settings.Connection", dbusConnection);
QDBusMessage msg = conn->call("GetSettings");
qDebug() << "Reply: " << msg;
}
I receive the message and can read it with qDebug(). The message is in d-bus format: a{sa{sv}} as the documentation for Network Manager says. And I have problem to parse it to any usable form. From what I understand, the equivalent Qt classes I should put the data to are:
QList<QMap<QString, QMap<QString, QVariant>>>
And I think I should do it with the QDBusArgument class but can't figure out how exactly it should be done. Can someone tell me what is the best way to do this?
Thanks.
I managed to solve my problem, it turned out to be quite easy. I created the map object and used >> operator:
QDBusArgument arg = args.at(0).value<QDBusArgument>();
QMap<QString, QMap<QString, QVariant>> map;
arg >> map;
I am using QProcess::start to launch Qt Assistant with my custom help project file. It works fine until i load project(not help project file) to my programm. Programm generates images from specific data using custom library. Even when all processes ends and i see generated images and nothing else happens, when i trying to launch Qt Assistant, my programm hangs at QProcess:start function when trying to start process. The code is:
show() function(public):
if (!run())
return false;
QByteArray ba("setSource ");
ba.append("qthelp://insyn_help/doc/");
proc->write(ba + page.toLocal8Bit() + '\n');
return true;
run() function(private):
if (!proc)
proc = new QProcess();
if (proc->state() == QProcess::Running)
return true;
QString app = QString(QT_BIN_DIR) + QDir::separator() + QString("assistant");
QString path = QString(PREFIX) + QString(HELP_INSTALL_PATH) + QString("/help_project.qhc");
QStringList args;
args << QLatin1String("-collectionFile")
<< QLatin1String(path.toLatin1())
<< QLatin1String("-enableRemoteControl");
QFileInfo help_project(path);
if (help_project.exists()) {
proc->start(app,args);
if (!proc->waitForStarted()) {
m_exitCode = 1;
emit closed();
return false;
}
}
This code is a part of AssistantLauncher class which was registered using qmlRegisterType and added to main.qml as a member of application window. My programm doesn't touch it anywhere (except calling a method show()). It is separate object (except it is a part of appWindow). The question is why does the process can not start only after my programm did some work? And why QProcess::start even dont have timeout.
UPD: I moved proc->start(app,args); to the child process, which i getting by using fork() and now my programm hangs on pid_t child = fork(). So the problem is that new process can not be created.
The answer is to do not use fork() because it is dangerous in big projects. More at http://www.evanjones.ca/fork-is-dangerous.html . posix_spawn also hangs my project. Now i decided to fork() new process at the beginning and send commands to it through the pipe.
I'm getting the following error when trying to use the Direct Show 9 backend with qt's phonon framework:
Pins cannot connect due to not supporting the same transport. (0x80040266)
Does anyone know what this error means and/or how to fix it? Is this a problem with the Direct Show 9 backend for phonon?
Apparently the problem has to do with bad metadata. If the Id3 tags aren't just right, the direct show 9 backend chokes on them. I solved the problem by writing the following function:
void removeTags(UDJ::DataStore::song_info_t& song){
static int fileCount =0;
if(song.source.fileName().endsWith(".mp3")){
UDJ::Logger::instance()->log("On windows and got mp3, copying and striping metadata tags");
QString tempCopy = QDesktopServices::storageLocation(QDesktopServices::TempLocation) + "/striped" + QString::number(fileCount) +".mp3";
if(QFile::exists(tempCopy)){
UDJ::Logger::instance()->log("Prevoius file existed, deleting now");
if(QFile::remove(tempCopy)){
UDJ::Logger::instance()->log("File removal worked");
}
}
bool fileCopyWorked = QFile::copy(song.source.fileName(), tempCopy);
if(!fileCopyWorked){
UDJ::Logger::instance()->log("File copy didn't work");
return;
}
TagLib::MPEG::File file(tempCopy.toStdString().c_str());
file.strip();
file.save();
Phonon::MediaSource newSource(tempCopy);
song.source = newSource;
if(fileCount == 3){
fileCount =0;
}
else{
fileCount++;
}
}
}
song_info_t is just a struct with a Phonon::MediaSource member in it called source. The function works by using taglib to strip off all of the metadata for a song and save the new song as a temporary file. The function also rotates the filename is uses for the temporary file so that it doesn't create an infinite number of temporary copy files. I hope this helps anyone else who is having this error.
I need to play a sound when a button is clicked, I have this:
Phonon::MediaObject *clickObject = new Phonon::MediaObject(this);
clickObject->setCurrentSource(Phonon::MediaSource("Click/sound.wav");
Phonon::AudioOutput *clickOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
Phonon::createPath(clickObject, clickOutput);
and
void MainWindow::on_pushButton_clicked()
{
clickObject->play();
}
but no sound is played?
Where am I wrong?
Thanks.
EDIT: It works now, it was the wrong path.
Probably the file path "Click/sound.wav" doesn't point where you think it points.
Try this before calling the setCurrentSource()-function:
bool exists = QFile::exists("Click/sound.wav");
If the Click directory is supposed to be in the same directory as your exe, create the path like this:
QString filePath = QCoreApplication::applicationDirPath() + "/Click/sound.wav";
clickObject->setCurrentSource(Phonon::MediaSource(filePath));
And I would suggest using Qt resource system. Then you would point to the sound file like this:
clickObject->setCurrentSource(Phonon::MediaSource(":/Click/sound.wav"));
You should at least connect the signal stateChanged(Phonon::State, Phonon::State) from your MediaObject object to a custom slot to detect errors: if the state changes to Phonon::ErrorState the reason of the error might be accessible through QMediaObject::errorString().
I'm having a bit of trouble with my function for displaying pdf's with the poppler library. The code below is the function in which the problem occurs.
const QString &file is the path to the file
int page is the page on which it has to open
When i set file to a real path (e.g. "/Users/User/Documents/xxx.pdf"), it is no problem to open it. But when i give the path to a qrc file (":/files/xxx.pdf"), it won't work. I want to use it for displaying a user manual for instance, within the application.
I've also tried first making a QFile out of it, opening it and doing readAll, then loading the QByteArray received by doingPoppler::Document::loadFromData(the qbytearray), but it errors already when opening the QFile in ReadOnly mode.
void class::setPdf(const QString &file, int page)
{
Poppler::Document *doc = Poppler::Document::load(file);
if (!doc) {
QMessageBox msgbox(QMessageBox::Critical, tr("Open Error"), tr("Please check preferences: cannot open:\n") + file,
QMessageBox::Ok, this);
msgbox.exec();
}
else{ /*Code for displaying the pdf, which works fine*/
}
}
I hope you can help me,
greetings,
Matt
I've also tried first making a QFile
out of it, opening it and doing
readAll, then loading the QByteArray
received by
doingPoppler::Document::loadFromData(the
qbytearray), but it errors already
when opening the QFile in ReadOnly
mode.
QFile f;
f.setFileName(":/skin/AppIcon16.png");
f.open(QIODevice::ReadOnly);
QByteArray r=f.readAll();
Perfectly reads all data from the resource, have checked it. So i suggest you did something wrong when tried that. Maybe path errors, maybe something else...