I have the following algorithm (working):
Acquire image from webcam
Process image
Send image to GUI and show it
The GUI interface is programmed with Qt, and all image acquirement and processing is been doing with OpenCV. There are 3 classes involved, call them Acquire, Process and Gui.
Acquire (Inherits from QObject) grabs the image and calls to Process (Does not inherit from QObject) to make the image processing. Process returns the result to Acquire, who emits a signal caught by Gui (Inherits from QObject), who converts the image (in Mat format) to QImage and draws it.
I am introducing changes into the Process class and I would like to have a visual feedback. As everything is been executed into the Qt's loop I can not use the cv::namedWindow and cv::imshow functions (nothing appears).
The question is: There is any quick method to make visual debugging to know what is happening inside Process without make Process and Gui friends, or connecting them using the signal/slot mechanism or any other solution that involves big changes into the program structure?
You can create another class and put all code for debug output into it. Connect Process to this class to send debug information.
Related
I have a 3D environment done in Unity which I wan't to have as an Item in Qt (QML). I've tried a few different paths, but none has proved to be efficient enough or I'm unable to get it to work.
My current working solution is to do the following each frame
In Unity, use ReadPixels of my RenderTexture (GPU) to get a regular Texture (RAM).
Encode to JPG and send bytearray through TCP socket.
In Qt, instantiate a QImage from the data and save it for later use.
In the render function of QQuickFramebufferObject::Renderer, use glTexImage2D to render the image to my active texture.
Obviously this is not an optimal solution. This performs maybe 10 fps with a 128x64 texture size (for testing). My understanding is that the bottleneck is transferring data from gpu and back.
In my latest attempts I have tried to get the ID of the RenderTexture using renderTexture.GetNativeTexturePtr(). Then in Qt I'm trying to get the pixel data through glGetTexImage, but I keep getting 0's in the data. When later using glDrawPixels, the Qt application crashes.
So my question now is, do anyone know if it's possible to share the texture between processes and if so, how?
I have a long running data processing application. Depending on the data it sees, I may want to start up a JavaFX application window (or it may never start-up one). How do I pass in my cached data?
e.g. in Swing, I'd have had a constructor that might look:
class MyFrame extends JFrame{
public MyFrame(BlobOfInMemoryCachedData manyMegabytesOfData){
this.data = manyMegabytesOfData;
// create JPanel, etc. using that data
...
}
}
Based on this answer: https://stackoverflow.com/a/24611918/155631, I can imagine non-standard-looking-java workarounds. Before I bake the usage of such workarounds into the application design, I want to verify: Is there really no cleaner way to directly pass the object handle?
Very quick because I haven't got time to write a proper answer with sample code:
In the main for your long running Java application, call the JavaFX application launch method once (and only once for the entire lifetime of your application).
Call Platform.setImplicitExit(false).
In the start method of your JavaFX application don't show a window.
Provide a static accessor on your JavaFX application show(data) which passes your blob data.
The show(data) method displays a JavaFX window for relaying data processing info in a UI.
When necessary call a static hide() method on your application which hides the JavaFX application window.
Continue processing, performing steps 5 and 6 (showing and hiding the window as needed).
When everything is finished call Platform.exit().
Key thing is that the JavaFX application is only launched once and you invoke a static accessor on it as needed to show the window. The set implicit exit false stuff prevents the default behavior of the JavaFX runtime shutting down when the last window of the application is hidden (so it just chugs along in the background waiting for a signal to show something again).
You could simplify things a little bit by having the your data processing application just extend the JavaFX Application class, but you might want to keep them separate for ease of testing or other design reasons.
Your other option is to use JFXPanel, but that adds an unnecessary Swing dependency, so I'd advise against that.
I'm running a qt embedded application and mplayer, both of them on framebuffer.
When I start video playing through mplayer, I get a lot of flickers around the movie.
See the following movie:
http://youtu.be/kbKpfjLHzTY
How to fix it?
For Qt 4 with QWS,the embedded linux graphics sub-system for writing directly to a frame buffer you can run the following in a the -qws server's GUI thread before invoking mplayer,
QWSServer *server = QWSServer::instance();
if(server)
server->enablePainting(false); // Suspend Qt's drawing.
You can use a SIGCHLD or something to figure out when mplayer is finished and re-anble painting. Another way would be to position mplayer's output window and use a QWSEmbedWidget to tell Qt not to draw there.
Both QWS and mplayer open the frame buffer and draw to it directly. There is nothing to marshal access to the display device. The QWS sub-system allows multiple Qt application to draw to the screen at the same time. However, it has no control over other processes accessing the frame buffer. For this reason, X11 or other display managers like Wayland, etc can be used. This is generally the method use in Qt5.
My scenario here is the following: I am using a pyqt widget to display a solid color fullscreen on a second display and observe this display with a camera that is continuously capturing images. I do some processing with the images and this is the data I am interested in. This works great when used interactively with ipython and matplotlib using the qt4agg backend like so
% ipython -pylab
# ... import PatternDisplay, starting camera
pd = PatternDisplay(); pd.show(); pd.showColor(r=255,g=255,b=255)
imshow(cam.current_image)
I need a similar behavior now in a console script though: it should display the PatternDisplay widget, capture an image, than change the color on the PatternDisplay and take a new image and so on.
The problem is now that the PatternDisplay is never updated/redrawn in my script, likely because PyQt never gets a chance to run it's event queue. I had no luck trying to move the linear worker part of my script into a QThread because I cannot communicate with the PatternDisplay Widget from another Thread any longer. I tried to replicate the implementation of ipython/matplotlib, but I didn't fully understand it, it is quite complicated - it avoids running the QApplication main loop via monkey patching and somehow moves QT into it's own thread. It then checks periodically using a QTimer if a new command was entered by the user.
Isn't there an easy way to achieve what I want to do? I am gladly providing more information if needed. Thanks for any help!
What you need is easier than IPython's job - IPython makes the Qt application and the command line interactive at the same time.
I think the way to do it in Qt is to use a timer which fires at regular intervals, and connect the signal to the 'slot' representing your function that gets the new image and puts it in the widget. So you're pulling it in from the event loop, rather than trying to push it.
I've not used Qt much, so I can't give specifics, but the more I think about it, the more I think that's the right way to do it.
I solved the same problem (i.e. interactive ipython console in terminal, and GUI thread running independently) in the following way with ipython 0.10 (code here)
1. Construct QApplication object, but don't enter its event loop explicitly
2. Run the embedded IPython instance
3. Run the UI code you need by instantiating your window and calling show() on it (like here with the yade.qt.Controller(), which I aliased to F12. (I did not find a way how to ask the embedded shell to run a command at the start of the session, as if the user had typed it)
(You can also show() your window first, then run the embedded ipython. It will provide event loop for Qt.)
(BTW I also tried running Qt4 from a background thread (using both python threads module, and Qt4.QThreads), but it refuses to run in such way stubbornly. Don't bother going that way.)
The disadvantage is that UI will be blocked while ipython is busy. I hope to finding something better for 0.11, which should have much better threading facilities (I asked on ipython-users about how to unblock the UI).
HTH, v.
I've got a Qt App that uses QPointers to bring up new UI Dialogs (Widgets). The main app can have numerous of the same widget loaded with different data. The problem I'm having is in deleting and freeing the memory for each widget. If I monitor the RAM usage of the program, each time I click the button to open one of these new widgets, it increases the ram and when I close the widget, it doesn't seem to be freeing the ram. I've tried using deleteLater and other solutions but keep getting crashes in the program.
Some example code is here:
QPointer<ListReservations> listResWindow = new ListReservations(resID);
listResWindow->setNum(numpeople);
listResWindow->show();
This will call the "ListReservations" widget which is declared as a QDialog (NOT modal). In that dialog I then have a button to close the window that calls the QWidget::close() slot.
I guess the question is how does my main program (that has the QPointer) know when the dialog is closed and then free the dialog and (if possible) delete the pointer to save even more memory...
I thought you might be able to do a QConnect() to the QPointer object, but I can't seem to find any signals or slots that would allow the passing of the pointer, much less send the signal once the dialog is indeed closed and ready for deletion.
Maybe I need some sort of function in the main program that takes a generic pointer object and then have the QDialog call that before calling it's own close slot? In that function it would pass itself to be destroyed? Just throwing out ideas that I've tried to implement but failed at....
I don't think I can reuse the same pointer elsewhere because in theory you could have multiple ListReservations windows open at the same time.
Make sure that you set the Qt::WA_DeleteOnClose attribute flag on your dialog using QWidget::setAttribute(). That should make sure that the dialog is properly destroyed when it is closed. See the Qt documentation for more details.
Assuming that memory is now properly freed, the pointer should invalidate itself, from the Qt documentation:
A guarded pointer, QPointer,
behaves like a normal C++ pointer T *,
except that it is automatically set to
0 when the referenced object is
destroyed (unlike normal C++ pointers,
which become "dangling pointers" in
such cases)