Checking for existing .exe file Qt - c++

why when i want to check .exe file this program will print false but when i add .txt file and check prints True?
i mean its only recognize .txt file not .exe
#include <QCoreApplication>
#include <QFile>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile read("C:\\Users\\Ramin\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Time.exe");
if(read.exists()){
qDebug()<<"True";
}
else{
qDebug()<<"False";
}
return a.exec();
}
thanks in advance

Related

Incomplete Type is not Allowed QApplication

I am learning C++ with Qt. I have installed Qt 5.15 and am using VS2019. I have the below code (as per an example in a textbook I am working through):
#include <QtGui>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QTextStream cout(stdout);
//declarations of variables
int answer = 0;
do{
//local variables
int factArg = 0;
int fact(1);
factArg = QInputDialog::getInt(0, "Factorial Calculator", "Factorial of:", 1);
cout << "User entered: " << factArg << endl;
int i = 2;
while (i <= factArg) {
fact = fact * i;
++i;
}
QString response = QString("The factorial of %1 is %2.\n%3").arg(factArg).arg(fact).arg("Do you want to compute another factorial?");
answer = QMessageBox::question(0, "Play again?", response, QMessageBox::Yes | QMessageBox::No);
} while (answer == QMessageBox::Yes);
return EXIT_SUCCESS;
}
However, I am recieving the below error when creating an instance of QApplication as app:
Incomplete Type is not Allowed
I am also recieving the below error for the QInputDialog and QMessageBox classes:
name followed by '::' must be a class or namespace name
I am not sure why this is happening - presumably something with a namespace, but I am not sure what scope to provide. I have searched the web but to no avail.
UPDATE
Adding the below header references give cannot open source file error for each.
#include <QApplication>
#include <QInputDialog>
#include <QMessageBox>
I have also added suggestions from the comments to my code, now below:
#include <QtGui>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QTextStream cout(stdout);
//declarations of variables
int answer = 0;
do{
//local variables
int factArg = 0;
int fact(1);
factArg = QInputDialog::getInt(0, "Factorial Calculator", "Factorial of:", 1);
cout << "User entered: " << factArg << endl;
int i = 2;
while (i <= factArg) {
fact = fact * i;
++i;
}
QString response = QString("The factorial of %1 is %2.\n%3").arg(factArg).arg(fact).arg("Do you want to compute another factorial?");
answer = QMessageBox::question(0, "Play again?", response, QMessageBox::Yes | QMessageBox::No);
} while (answer == QMessageBox::Yes);
return EXIT_SUCCESS;
}
But I am still recieving the same errors.
You have to include some qt headers in the app... that is the meaning of the message
you just need to add this to your code
#include <QApplication>
#include <QInputDialog>
#include <QMessageBox>
The correct headers to include are the following:
#include <QtWidgets/QApplication>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QMessageBox>
Once declaring these, the compiler accepts the code.
I'm going to cut and paste a working program for you.
Qt seems to have a bunch of forward definitions. I can tell I'm missing the right include file if I try to get the IDE to actually help me select a method call. In short, you pretty much have to #include every widget type or other class you're going to use. I haven't run into any shortcuts.
#include <QApplication>
#include "MainWindow.h"
using namespace std;
/**
* Main entry point.
*/
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}

Unable to translate GUI in QML

I have written following code to load .ts file
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QTranslator>
#include <QDebug>
#include <QDir>
int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QTranslator translator;
if(translator.load("<absolute-path>/translating-qml_ge.qm"))
{
qDebug() << "Loaded";
app.installTranslator(&translator);
}
else
qDebug() << "failed";
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
I have checked return value of 'installTranslator' which is true.
but still UI is not getting translated.

How to play a wma audio file using Qt or any other cpp framework on linux?

To make the code below work:
#include <QApplication>
#include <QPushButton>
#include <QMediaPlayer>
#include <iostream>
int main(int argc, char **argv) {
QApplication application(argc, argv);
QPushButton button("Click Me");
button.show();
auto player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile("/home/bj/m/y.wma"));
player->setVolume(50);
QObject::connect(&button, &QPushButton::clicked, [player](bool) {
std::cout << "xxx" << std::endl;
player->play();
});
return application.exec();
}
Now this code can play mp3 by installing gst-libav. But how to play wma?
I solved. ugly plugin is required. sudo pacman -S gst-plugins-ugly

Reading Windows encoded files in Qt

I have been struggling for an embarrassingly long time now to correctly read Umlaute from a Windows encoded file. The file is saved using the Windows-1252 codec and consists of one single line:
123 äöü 456
My simple test program to isolate the behavior I don't understand is;
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QTextCodec>
#include <QByteArray>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QStringList ids;
QFile inputFile("test.txt");
QTextCodec *codec = QTextCodec::codecForName("Windows-1252");
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while (!in.atEnd())
{
QByteArray line = inputFile.readLine();
QString string = codec->toUnicode(line);
qDebug() << string;
}
inputFile.close();
}
else
qDebug() << "Cannot open file";
return a.exec();
}
The console output is:
"123 ??? 456\r\n"
So all the Umlaute get eaten up.
I am running this in Linux, the terminal emulator is set to use UTF-8. What can possibly go wrong here.

Why messages printed after closing main window in Qt C++?

With this code
#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include <QDir>
#include <QTextStream>
int main(int argc, char *argv[]){
QApplication a(argc, argv);
QTextStream out(stdout);
out << QDir::currentPath();
std::cout << "Why is that?";
MainWindow mainWindow;
mainWindow.show();
return a.exec();
}
Both messages printed only after closing Main Window of my app, why is this?
I tried to debug, debugger thinks that he done with this line, but I see no messages.
extern std::ostream cout; is buffered, so it may choose when to flush its buffer to stdout. In your case, it is doing it when your program is terminating.
You can tell std::ostream to flush using std::flush, as such:
std::cout << "Why is that?" << std::flush;