Where to declare object returned from Qt SIGNAL/SLOT? - c++

I've got a FTP application written in c++. Now I'm adding a simple GUI to my project via the Qt plugin for VS2010. The mechanism to connect to the server is very simple. After typing both IP and PORT the connection is stablished and an object is created. This object (from a class I've written) can call several functions such as: ask for the available files on server, download an specific file, etc.
My question is: if that object is created after pressing a button (and calling the function it is linked to) is there any way to return that object? Sure there is but, how and where in the code should it be stored/declared. Here is the code line:
connect(ui.btn_connect,SIGNAL(clicked()),this,SLOT(on_SayConnect()));
How should I call the function "on_SayConnect()" if I want an object to be returned using the connect (SIGNAL/SLOT) syntax? Thank you in advance.

There's no good way to return values using the connect method. If your goal is to have the this object (I don't think you actually gave it a name) send a value/object to other parts of the system, you could have the on_SayConnect slot emit a signal with the desired value. Any other objects that need to receive that value should implement and connect the proper slot to do so.

Related

How to access another BP actor's component in c++?

I am new to Unreal (switched from Unity) and still have some troubles understanding the main concepts, in this case how to access other objects and their components via c++ scriptig. I am using UE5 (but I guess solutions for UE4 should also work fine).
My Project looks as followed:
In my scene I have an "Target" Actor (Blueprintclass) that has a self written c++ "movement" component with some public function to update its position.
More over I have an "Experiment" BP Actor that has a "TrialProcedure" c++ component attached.
Here is what I want to do: I want to run the Target's movent component's update position function from this Actor's component.
I guess once I can access the Target Actor, I can use GetComponentByClass() to access the component I need and than run it's method. But how do I get access to that other actor without using blueprints? The Actor is already there so I don't want to spawn it from code.
Thanks in advance!
That is a sub-optimal solution. Use a collision and get the Other Actor, then try to cast it to your desired class if it's a collision event. If it fails, don't do anything but if it doesn't, pull a pin from that and execute your functions.
A better alternative to your current solution would be to use the Get All Actors of Class function if you only have a single target. Just use the Get (a copy) node and you'll have your target blueprint. There's a C++ version of the function as well. If you have more than one file, then you'll have to check stuff.

QtQuick QML SQL C++

I need to run an algorithm in Qt on data that originates from two different sources:
1- UART ( raw transducer data)
2- variables a user inputs into a gui
The result is then output to the gui for the user to see
Gameplan:
- User inputs variables via a gui (written in qml - i actually have no choice on this)
- The qml creates a database of all variables, then
- Invokes C++ routines for collecting raw data over a UART , saves it to a file,
- the C++ module opens up the database and extracts variables needed , opens the file reads in the data, runs the algorithm using both, calculates the answer and writes it to the database,
- program then jumps back to QML where, later, the QML will go into the database to extract that answer and display it.
If you see a flaw already please let me know before I spend hours finding out this wont work. If it is plausible.....
FOUR QUESTIONS:
(1) who should run the UART - C++ or QML? anyone knows pros /cons? The raw data will have to be post processed so i'm thinking it will need file saving (I'm thinking UART is a C++ thing)
(2) I need to create a database from user input variables from QML - any one have examples of this?
(3) - the QML must do a Q_INVOKABLE to the C++ routines (to run the algorithm) . I've already run a test program to prove I can do that so not too worried though if anyone has any other examples I'd love to see how others do this.
(4) BOTH the QML and the C++ need to access (read and write) the database created in (2).
Is this doable? If so...anyone have any examples of QML and C++ sharing the same database, any code, videos, anything? Should I keep opening and closing the database or is there a way to open it and have some pointer or something be passed around. I can't find anything about QML accepting or passing a pointer.
Thanks
Dubs
(using ARM® Cortex™-A9 embedded linux qt)
For SQL and QML Q_INVOKABLE refer to this answer and for UART use C++ and invoke it from QML.

Class Design in C++ program

I'm currently working on a little project in C++. I'm fairly new to C++/Programming and wanted to ask how my classes should be designed.
To be specific: I want to write a little program for chatting. Just simple communication between two programs/computers. For that I want to use a good class design because, although it is only a small project (just for the sake of learning), I want it to be well designed and extensible.
My program should have about 5 classes (Handlers etc, the Updater and App - the main class only for this program).
I'll give you a few examples of ideas I have how I could design one part of the program. The first part should be the connection part (handled by Connection). The task is to build up a connection between the two programs. It will also set up local files, which will hold the information to print, and connect it to the 'server' file. Later in the program there should be access to the File_Handler class so it can edit/read the local file and read/request to write to the 'server' file. (How exactly it's gonna do it is already figured out, so as long as it is not necessary I'd like to keep this system as it is ;) ). But now let's get to the ideas I have:
Idea 1
App creates an instance of Connection to set up the connection.
Connection then creates an instance of File_Handler which will set up the files (in this case File_Hanlder would hold the paths with static variables) and then destroy that instance because it is no longer needed by Connection.
Another class (instance is held by App) then creats an own instance of File_Hanlder later and could work with the files since the variable for the path is static.
Idea 2
App creates an Instance of Connection and File_Handler (lets call them con and fil).
To set up the connection, fil is passed per reference to con to set up the variable paths for the files and create the files etc. .
For working with the files later, App would pass around fil to manage all the file handling for the other classes which would, for example, update the chat etc.
That would require a lot of classes to have a constructor or (a) function(s) which needs to have a File_Handler parameter and the same variable would be passed around a lot of times.
Idea 3
The last idea I have is that Connection, File_Handler and the other classes would be created very general without any or near to zero relation to other classes.
App then creates a lot of functions (or even 'subclasses') to work with these classes almost like working with Frameworks which were developed independently and thus could, theoraticly, be used in another program without any problems.
Which solution do you think would be the best? Or is there another solution you have for me which would be even better?

Why does qRegisterMetaType throw me an error?

I'm currently working with Qt in order to build an app able to send and receive data packets connected through QSerialPort.
For that, I have an object interfacing the communication between my app and the device.
In this class, I can call a run member function, which initiate the communication.
In that method, the connection is like this :
qRegisterMetaType<QSerialPort::SerialPortError>();
connect(this->_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this->_parent,
SLOT(handleSerialError(QSerialPort::SerialPortError)), Qt::QueuedConnection);
Here, my problem is that no matter what I tried, I always get an error from the included file from QtCore, qglobal.h, on the line 684, referring to the first line of code where I try to register the meta type SerialPortError.
The error :
Type is not registered, please use the Q_DECLARE_METATYPE macro to
make it known to Qt's meta-object system.
And that's what I did! From everywhere in my code, inside the class, inside the name space, or even in global, I called that macro Q_DECLARE_METATYPE(QSerialPort::SerialPortError), but nothing was going well :/
That's my third day looking on the web to solve this error, and I'm running out of time.
I can not figure out what I'm doing wrong or misunderstanding.
Can someone help me? Have you already faced that error?
Thanks you, David
QSerialPort inherits QObject, so the error type enum should already be registered via Q_ENUMS. You don't need to re-register.
I had this same error and i managed to fix it without using the Q_DECLARE_METATYPE.
have you tried: qRegisterMetaType<QSerialPort>();
because all i had to do in my code was just that... just register the class. Even though it was giving me the same error, telling me to use Q_DECLARE_METATYPE.

In QtWebkit, how does a webpage's QNetworkAccessManager::createRequest() get invoked?

I'm building a Browser application using the QtWebkit and QtNetwork modules.
Let's say that it's a requirement that each webpage only be able to access resources from only a specific folder, set aside specifically for it. In this scenario, each webpage would have some kind of ID to identify it which could be used to verify that it's accessing the correct folder.
The problem is that it's not clear how exactly the createRequest() method gets invoked. If it's a signal that's emitted or something then I would be able to intercept it and add a few parameters indicating webpage ID.
As such now the only option open to me is to create a separate QNetworkAccessManager for each QWebPage and overload the createRequest() function whereas I would really like to be able to share the QNetworkAccessManager across QWebPages.
Alternate solutions would be appreciated but generally I'm also really confused about how the createRequest() method is reached.
Reference :
QNetworkAccessManager::createRequest
It's not a big deal to have a separate access manager for each web page. You don't have any measurements to show it to be a problem, so in a true Don Quixote fashion, you're fighting windmills and imaginary enemies :)
The createRequest virtual method is called by the various non-virtual request methods: get, post and put. It's a good example of the non virtual interface (NVI) pattern.