Problems Receiving QSerialPort Signals - c++

I'm trying to write a simple program to test out the serial port, but am having trouble getting QSerialPort to work. I never receive signals from the QSerialPort object. I get 3 runtime warnings/errors that I think probably have something to do with it.
The errors are:
QApplication::qAppName: Please instantiate the Qapplication object first
QSocketNotifier: Can only be used with threads started with QThread
QSocketNotifier: Can only be used with threads started with QThread
The code snippet below is the smallest sample I can give to recreate the errors, it doesn't show me connecting signals but I think these errors are why I don't see them. If I don't call the open function none of the errors occur.
#include <QtGui/QApplication>
#include <QSerialPort>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
PowerBoardGUI w;
w.show();
QSerialPort* serial = new QSerialPort();
serial->setPortName("/dev/ttyS1");
serial->setBaudRate(QSerialPort::Baud9600);
bool isOpen = serial->open(QIODevice::ReadWrite);
serial->close();
delete serial;
return a.exec();
}
The test system is Redhat 5.6. A static version of QT 4.7.4. And the latest version of QSerialPort (built from GIT today).

Related

QEventloop canot be used without QApplication

I'm currently developing a Console Unit Test Client with Google Test.
The Tests involves a step where a binary file is downloaded from a server.
To download this file I thought about using Qt.
Now I took the example code from
https://wiki.qt.io/Download_Data_from_URL
This gives me the error QEventloop canot be used without QApplication
I found this thread QEventLoop: Cannot be used without QApplication
However this works for a UI application.
My googletest main is predefined
GTEST_API_ int main(int argc, char **argv) {
printf("Running main() from %s\n", __FILE__);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Where would be a good place to add the QApplication?
Could you help me out?
thx for your help :)

Can input audio be processed by more apps at once with Qt?

I would like to use the QAudioRecorder to record audio from microphone. My question is, should there be an ongoing Skype call or another application would try to use the microphone, would this result in some error or would both that app and my app receive same audio input data?
Similarly, can I use the QCamera and QMediaRecorder to access webcam that is being used already?
Yes, that will work just fine, there won't be any error. Both applications will receive the same audio input data. I tested to record the same thing at the same time using Windows' voice recorder program and a simple Qt program built with the following code, and the result was that both output files were recorded the sound correctly:
#include <QApplication>
#include <QAudioRecorder>
#include <QPushButton>
#include <QUrl>
int main(int argc, char **argv){
QApplication app(argc, argv);
QAudioRecorder *audioRecorder = new QAudioRecorder;
QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/amr");
audioSettings.setQuality(QMultimedia::HighQuality);
audioRecorder->setEncodingSettings(audioSettings);
audioRecorder->setOutputLocation(QUrl::fromLocalFile("C:\\Users\\dduck\\Desktop\\test.amr"));
QPushButton b("Start");
QObject::connect(&b, &QPushButton::clicked, [&](){
if(b.text() == "Start"){
audioRecorder->record();
b.setText("Stop");
}
else{
audioRecorder->stop();
app.quit();
}
});
b.show();
return app.exec();
}
I assume it would also work if you run that program at the same time as Skype.
So yes, input audio can be processed by a Qt program at the same time as it is processed by other programs.

QTcpSocket not emit any error or connected signals

I have a small project which send some data over network using QTcpSocket. The server works fine but the client(code here) seems does nothing. If I set breakpoint at tcpSocket.connectToHost("127.0.0.1",port); it does jump in, but not any slots I defined.
I can't figure out what's wrong. I think the environment is ok because I can build 2 working examples from Qt GUI Programming
Any ideas are appreciated.
You do not have a QApplication instance and thus no event loop which does all the event / signal&slot handling.
So you at least need a QCoreApplication instance like this in main.cpp:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Client client;
client.connectToServer();
return a.exec();
}

Program crash after setting QApplication::style twice

#include <QApplication>
int main() {
QApplication::setStyle("windows");
QApplication::setStyle("windows");
}
This program produces Segmentation fault (core dumped). My qmake version is 4.7.2. Is this a Qt bug or my version is too old?
You must create an instance of QApplication before you set it's style. From the documentation
Ownership of the style object is transferred to QApplication, so
QApplication will delete the style object on application exit or when
a new style is set and the old style is still the parent of the
application object.
I'm assuming it's crashing because there is no QApplication to take ownership of the style. In general, creating the QApplication is one of the first things you should do.
#include <QApplication>
int main() {
QApplication a(argc, argv);
QApplication::setStyle("windows");
QApplication::setStyle("windows");
}

C++ QNetworkAccessManager (Qt) in conjunction with openGL

I'm really nooby with C++ (as my previous posts mention), however my friend suggested I work with QNetworkAccessManager if I want to send a HTTP GET request to send information.
I am currently working with openGL-es and want to do the following two lines of code to send the get request:
QNetworkAccessManager* netMan = new QNetworkAccessManager(this);
netMan->get(QNetworkRequest(QUrl("something/?userID=1")));
However, it does not like the "this" because it is in the main() method and it does not reference a QObject (I'm guessing QApplication). When I get rid of the "this" my application builds, but just never loads (I put a "printf(1)" at the top which doesn't even run).
Any suggestions or alternatives on how to fix this? Thanks in advance.
-James
The parameter in the QNetworkAccessManager constructor is only needed to specify a QObject based parent which will be responsible for cleaning up (deleting) your object later and isn't necessary if you plan to call delete on it yourself.
I'm not quite sure what you are referring to by "never loads" or where you put a printf but in order to get anything back, you need to actually keep the QNetworkReply pointer that is returned by the call to get().
And to get anything from that, you need an event loop running. If your application is console only (no GUI), you can use a QCoreApplication object.
Try this minimal code:
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QNetworkAccessManager *netMan = new QNetworkAccessManager();
QNetworkReply *reply = netMan->get(QNetworkRequest(QUrl("http://google.com")));
a.connect(reply, SIGNAL(finished()), SLOT(quit()));
a.exec();
qDebug() << reply->readAll();
delete netMan;
}