Why does QCoreApplication a(argc, argv) exist inside main(int argc, char *argv[]) in a Qt console application? What does it do? What is its purpose?
if I remove it, a program like the following runs without issues
#include <QCoreApplication>
#include "animal.h"
int main(int argc, char *argv[])
{
//QCoreApplication a(argc, argv);
{ // this is just a dummy scope for the destructor to work
// create instances of class animal
animal cat(NULL, "Garfield");
animal dog(NULL, "Azor");
// perform actions with the object's functions
cat.weight = 10;
qDebug() << "Cat Weight in Lbs:" << cat.weightInLbs();
cat.speak("meow");
// perform actions with the object's functions
dog.weight = 35;
qDebug() << "Dog Weight in Lbs:" << dog.weightInLbs();
dog.speak("wooof");
}
//return a.exec();
return 0;
}
Related
I'm trying to get global position of the mouse however the functions mentioned in older topics are either deprecated or not working as intended. I have tried QCursor::pos() as seen below but it didn't work as intended.
#include <QtCore/QCoreApplication>
#include <QCursor>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while(1)
{
std::cout << QCursor::pos().x() << " " << QCursor::pos().y() << std::endl;
}
return a.exec();
}
Output: 2147483647 2147483647
It is very simple. QCursor is in the Gui module of Qt see here, so you have to change QCoreApplication to QGuiApplication and then it works (already tested it).
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();
}
My program alerts the user when something has happened. To get his attention, a alert sound plays. It stops when the user enter something to confirm receipt.
But the QTextStream input blocks the sound !
When I remove it, the sound plays perfectly.
Besides, the "alert" QSound object doesn't work. The only way to play is to use the static function QSound::play("file.wav"). But it can't be stopped.
Here is my code :
#include <QCoreApplication>
#include <QSound>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
/*
QSound alert("../SoundAlerte/alert.wav");
alert.setLoops(QSound::Infinite);
alert.play();
*/
QSound::play("../SoundAlerte/alert.wav");
qDebug() << "ALERT";
qDebug() << "Enter Something to confirm receipt" ;
QTextStream s(stdin);
QString value = s.readLine();
qDebug() << "Received !";
//alert.stop();
qDebug() << "Sound stopped";
return a.exec();
}
It seems like it can't play a sound and wait for input at the same time !
Do you have have an idea on how to proceed ?
Thanks
QSound::play is asynchron, but
QString value = s.readLine();
contains a do-while and will block the audio file. See scan function called by readLine()
A working example would be QtConcurrent, but you can't stop the audio file, so you might want to switch to a real QThread approach.
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFuture<void> future = QtConcurrent::run([]() {
QSoundEffect effect;
QEventLoop loop;
effect.setSource(QUrl::fromLocalFile("C:\\piano2.wav"));
effect.setVolume(0.25f);
effect.play();
QObject::connect(&effect, &QSoundEffect::playingChanged, [&loop]() { qDebug() << "finished"; loop.exit(); });
loop.exec();
});
QTextStream s(stdin);
QString value = s.readLine();
return a.exec();
}
That is how i tried it at first but i get an error "no operator found that takes a left hand operand of type 'QTextStream' or there is no acceptable conversion"
QList<QString>lNamesList;
void write(){
QFile data("E:/Test/output.h");
if (data.open(QFile::WriteOnly))
{
QTextStream out (&data);
nameList.append("Name1");
out << NameList;
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
write();
return a.exec();
}
You cannot feed your nameList directly to your QTextStream. You can give it a string or QString. So you might want to loop over your QList which would look something like this:
for(const QString& str : nameList){
out << str;
}
note: see the docs for more info
It seems that Qt changes the 'interpretation' of the returned string of std::to_string(double).
Example:
#include <iostream>
#include <QApplication>
#define VERSION 1.0
#define VSIZE 3
int main(int argc, char** argv) {
std::string version = std::to_string(VERSION).substr(0, VSIZE);
std::cout << version << std::endl; // everything fine
QApplication app(argc, argv);
std::cout << std::to_string(VERSION).substr(0, VSIZE); // no good
return 0;
}
The output is:
1.0
1,0
I think it's Qt because this is the thinnest example where this happens, and removing the QApplication declaration makes it work again. How can I fix this?