I am trying to implement GUI for a simple C++ using Qt to understand how it works. The C++ program and the GUI are in seperate projects in the same solution in VS 2015. The Qt program will call the C++ program using QProcess' start() function. The C++ console application will be running the background will the Qt program acts as an interface. My question is, how I do I pass values to the C++ program using QProcess and How do I obtain the output from the C++ program? Here is the sample program I am working with:-
The C++ Program
#include<iostream>
#include<fstream>
using namespace std;
void main() {
int a;
cout << "Enter a number" << endl;
cin >> a;
cout << "The Square of the number is " << (a*a) << endl;
ofstream write;
write.open("test.txt");
write << (a * a);
write.close();
}
The Qt Program
#include "FrontEnd.h"
#include <QtWidgets/QApplication>
#include <qprocess.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FrontEnd w;
w.show();
QProcess p1;
p1.start("Interface.exe");
p1.write("5",5);
return a.exec();
}
I tried to pass the value using the write() function but it did not seem to work as the test.txt file remained empty. The Qt program I have written lacks the GUI features as I will be adding them once I figure out how to send and recieve data using QProcess. Thank you for your assistance!
You have many problems in the code you are showing:
You are passing write two arguments "5" and 5, this means that it will use 5 chars from the the char* "5". Absolutely, this is not what you want as this will lead to undefined behavior resulted from accessing memory that does not belong to your data.
Instead you should be using the second version of write which accepts a zero-terminated string, like this: p1.write("5");
In order to make cin in your console program know that the number it should read is finished, you should pass a new line after your number, so your call will end up like this: p1.write("5\n");
You should use the readyRead signal in the Qt program to get notified when your process has new output that you can read, and then you may call readAll from a slot which is connected to that signal.
For completeness, Here is how your code should be:
interface.cpp
#include<iostream>
using namespace std;
void main() {
int a;
cin >> a;
//No need to use file output
//it is simpler and more appropriate to read the output from stdout
cout << "The Square of the number is " << (a*a) << endl;
}
The Qt program
#include <QApplication>
#include <QWidget>
#include <QProcess>
#include <QDebug>
#include "FrontEnd.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FrontEnd w;
w.show();
QProcess p1;
p1.start("Interface.exe");
p1.write("5\n");
QObject::connect(&p1, &QProcess::readyRead, [&p1](){
//output to qDebug, you may want to update some GUI component instead
qDebug() << p1.readAll();
});
return a.exec();
}
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'm trying to send in an input value within the CLion + Gtest system (MacOS 10.14.4), but I get infinite "loading icon" and cannot type in anything into the "Run" window.
I created the tests using a tutorial. Normal tests work flawlessly, only std::cin refuses to work. std::cin works outside the test framework, when just opening a basic project and immediately using it. For this simple example, I only use one file "test.cpp", so nothing else is imported.
#include <gtest/gtest.h>
#include <iostream>
using testing::Eq;
using namespace std;
namespace {
class basicTest : public testing::Test {
public:
basicTest() {
}
};
TEST_F(basicTest, test1) {
cout << "\nWrite a number here: " << endl;
int i;
cin >> i;
cout << "You wrote " << i << endl;
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Running this produces the following:
The icon keeps spinning, but there is nowhere for me to write an input. In my original code, the first cout did not even output, so it's just a blank screen until I terminate it with the stop button.
Stopping early produces "Process finished with exit code 137 (interrupted by signal 9: SIGKILL)" but I don't think it's relevant.
I have not tried to use debug mode because it's not compatible with my MacOS version (that's another problem I'll fix eventually).
I have a problem where I can't seem to get a output to display in a console when doing it through a function.
It works when doing it through Main(), but just blank when doing it through the function.
Below is some of my code:
#include "ConferencePaper.h"
#include "JournalArticle.h"
#include "Reference.h"
#include <QDebug>
#include <QTextStream>
QTextStream cout(stdout);
int main()
{
//QApplication app(argc, argv);
QStringList list1;
list1 << "This is a test";
Reference a("Marius",list1,1,"c"); //Instance of the Reference class created with parameter values
cout << "Title: " << a.getTitle(); //This works fine
a.toString();
return 0;
}
//Reference Function
#include <QString>
#include <QStringList>
#include <QTextStream>
#include "Reference.h"
Reference::Reference(QString ti, QStringList as, int ye, QString id): title(ti), authors(as), year(ye), refID(id){}
QString Reference::toString()
{
return QString("Title: %1\n") .arg(getTitle()); //Does not display anything
}
In your toString() method:
QString Reference::toString() {
return QString("Title: %1\n") .arg(getTitle()); //Does not display anything
}
there is nothing which could cause to print anything on the console. You are simply returning the string as a result of that method.
To display something, you need to output the string which is returned from the method, e.g. in your main() function like
cout << a.toString().toUtf8().constData();
or
cout << a.toString().toLocal8Bit().constData();
Note that you need to convert your QString to a data type for which a << operator is available for ostream. See also How to convert QString to std::string?
As mentioned above several times, X.toString(); would just return QString to a caller, then depending on what you're trying to achieve you may:
print it to console using cout << ...
print it to Application Output pane in your Qt Creator using qDebug() << ...
(see QDebug Class reference for details, it's pretty common debugging technique)
I want to create a thread with ACE_thread_manager, there is no error when I debug. but the result is not right. The function did not work; code like this:
#include "ace/OS.h"
#include "ace/Log_Msg.h"
#include "ace/Thread_Manager.h"
#include <iostream>
void thread_start(void* arg)
{
std::cout << "Running thread..\n";
}
int main(int argc, char *argv[])
{
ACE_Thread_Manager::instance()->spawn(ACE_THR_FUNC(thread_start), 0, THR_NEW_LWP);
return 0;
}
this demo should print "Running thread.." , but when I debug it ,it print nothing . These Chinese mean "Please press any key to continue" .
You have to wait in your main until your workers threads have finished. As you say, you have to add the following line before the return in main.
ACE_Thread_Manager::instance()->wait();
I wrote a program to output a random quote from a QStringList to a console window, program runs but I can't figure out why nothing appears in the window. Please help
here is the code:
#ifndef RANDOMADVICE_H
#define RANDOMADVICE_H
#include <QString>
#include <QStringList>
class randomAdvice
{
public:
randomAdvice();
QString returnAdvice();
private:
QStringList randomList;
QString output;
};
#endif // RANDOMADVICE_H
here is the cpp file randomadvice.cpp
#include "randomadvice.h"
#include "cstdlib"
#include "ctime"
#include <QString>
randomAdvice::randomAdvice()
{
randomList = QStringList()
<< "In order to succeed, your desire for success should be greater than your fear of failure. - Bill Cosby"
<< "Always be yourself, express yourself, have faith in yourself, do not go out and look for a successful personality and duplicate it. - Bruce Lee"
<< "A successful man is one who can lay a firm foundation with the bricks others have thrown at him. - David Brinkley"
<< "Strive not to be a success, but rather to be of value. - Albert Einstein"
<< "To succeed in life you need 2 things: Ignorance and confidence. - Mark Twain"
<< "Success is a lousy teacher. It seduces smart people into thinking they can't lose. - Bill Gates"
<< "Remembering that you are going to die is the best way I know to avoid the trap of thinking that you have something to lose. You are already naked. There is no reason not to follow your heart. - Steve Jobs";
}
QString randomAdvice::returnAdvice()
{
srand(time(NULL));
output = randomList.at(rand() % randomList.size());
return output;
}
and the main file:
#include "randomadvice.h"
#include <QtCore/QCoreApplication>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream out(stdout);
randomAdvice ra;
QString adviceString = ra.returnAdvice();
out << adviceString;
return a.exec();
}
QTextStream buffer the output until it is flushed or a newline is written.
You can either add out.flush() after out << adviceString or change out << adviceString to out << adviceString << endl.
Try QTextStream out(stdout, QIODevice::WriteOnly);