QPointer and deleting objects - qt

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)

Related

Livereload after save but keep some objects

Livereload for development purposes: after save the application should reload the GUI entirely.
But there are some C++ objects (used in QML code) that should stay and find themselves in the new version of the GUI.
Possible approach is to add to such object a string property that will be the same in the newely-loaded QML code, so it'll attach to that. Obviously, the object has two parts: the QObject and Qt-independent implementation.
There is a problem with that: the other bindings need such object to be already attached. These signals/properties mustn't arrive earlier.
I'm thinking about setting that 'persist' property in the Component.onCompleted, so it'll be like atomic with the C++ constructor. Will it work? Other suggestions? How do you do it?

Debug image processing into Qt loop

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.

How can I stop Qt::ForbiddenCursor from appearing during a drag?

I'm implementing a drag and drop interface with Qt across X11 and Windows. The interface handles events such that it is not illegal for a user to drop a dragged object on an area which can't handle drops.
In this case, Qt::IgnoreAction should therefore not be treated as an incorrect potential action. To communicate this fact to the user I need a way to stop Qt::ForbiddenCursor from displaying if the current Qt::DropAction is Qt::IgnoreAction.
There are three ways I can see to achieve this (in order of preference):
To override the QCursor used for a drag with Qt::IgnoreAction to something other than Qt::ForbiddenCursor.
To override the bitmap used for Qt::ForbiddenCursor. This is pretty dirty but would be an acceptable solution as long as I don't have to delve into OS-specific configuration.
To override the call made by Qt when a drag leaves a valid drop area (I assume that Qt does the equivalent of QDropEvent::setDropAction(Qt::IgnoreAction) in this case).
Could anyone suggest ways to acheive any of the above?
Note: I have also attempted to use QApplication::setOverrideCursor() just before calling QDrag::exec(). This doesn't seem to have any effect.
Check if QDragEnterEvent comes to application itself (install event filter on QApplication object). If it does, simply accept it and cursor will appear normal.

Qt: Should I route my signals from an external object through my mainWindow or directly to the ui elements

Im building an application where a few objets outside the mainWindow will need to talk to be connected to the ui elements, just simple on click events triggering member functions. I was wondering whether to Add slots to the main window that connect to each ui element so that I only ever need to interface my external objects with one ui object, the main window, or whether I should directly connect my objects signals to the individual ui elements slots.
Also could someone share an example of how to connect a signal from an external object into the mainWindow, I'm very new to this and until now I've always added all my code to the mainWindow to keep things simple but i know thats bad practice so i think its time to move on.
Thanks all
j
There is no difference in syntax between connecting an "external" object or an "internal" one to a slot. The connect call takes two object pointer (from/to) and doesn't really care about where they are (except if you're using threads).
The Qt Signals and slots documentation has all you need to know about these connections, and the examples show real code that uses signals. (The analog clock for instance uses a QTimer which is "external" to its main window.)
There is no one-size-fits-all answer to the "where should I put my public slots" question. Do what is easier to maintain/more logical for your particular scenario. Having all your "external" signals routed to a single object that dispatches them is "clean" from an interface (API) point of view, but it means you'll need to maintain more code than if you routed the signals directly to the object that needs to react on them.

QT thread ans setPixmap function doesn't work

I have my own application in QT.It has one main GUI thread which will handling the event from the inputs but i have created one thread that will applicable to change images every 10 seconds (just like slideshow or screen saver). but when i call function setPixmap from thread then it gives me warning that it's not safe to use Pixmap from thread.
what is the solution ?. why i don't use setPixmap from thread ?
Thanks,
Neel
The why, is because that function is not thread safe.
The solution, is to use a QTimer to run your function every 10 seconds. QTimer is integrated in the Qt Event Loop, so you don't need another thread to do it.
I don't have an actual answer to this but I know that setPixmap() should only be called from the main GUI thread. I found this mailing list post from a few years back that also points to trolltechs docs. Reading through the thing quickly leads me to think that it has something to do with how different platforms render stuff and so on.
http://lists.trolltech.com/qt-interest/2008-11/thread00534-0.html
http://doc.trolltech.com/4.4/threads.html#painting-in-threads
Instead of having your worker thread call setPixmap(), have it emit a signal (something like newImagesReady()).
Then, connect that signal to your widget's update() slot. (Or make your own slot if you want to do more than refresh the widget).
This technique lets you cross thread boundaries.

Resources