Qt: Preventing memory leaks when closing windows [duplicate] - qt

This question already has answers here:
Destruction of QObjects
(2 answers)
Does a QWidget remove itself from the GUI on destruction?
(2 answers)
Closed 3 years ago.
All the example code I can find for Qt shows code that creates objects with new, but they don't get cleaned up in any window close situations. Unless Qt automatically destroys them, this seems false.
Will Qt automatically destroy all the GUI objects in the window when it's closed, or am I responsible for that myself just like I would normally be for everything else?
If I'm responsible for it, is there a "polite" way? Or can I just keep a vector of objects to destroy and run it in the reverse order they were created, calling delete on each?
For instance, here's the example for using BorderLayout:
Window::Window()
{
QTextBrowser *centralWidget = new QTextBrowser;
centralWidget->setPlainText(tr("Central widget"));
BorderLayout *layout = new BorderLayout;
layout->addWidget(centralWidget, BorderLayout::Center);
layout->addWidget(createLabel("North"), BorderLayout::North);
layout->addWidget(createLabel("West"), BorderLayout::West);
layout->addWidget(createLabel("East 1"), BorderLayout::East);
layout->addWidget(createLabel("East 2") , BorderLayout::East);
layout->addWidget(createLabel("South"), BorderLayout::South);
setLayout(layout);
setWindowTitle(tr("Border Layout"));
}
This code creates a layout and 6 widgets with new QTextBrowser or new QLabel. It doesn't even try to remember them for later destruction.

Related

Adding a high number of QML objects in a QWidget application using QQuickView or QQuickWidget poses performance problem

I'm developping a Qt application in which the user can add QML objects inside a QGraphicsScene. The available QML objects are listed and the user can add as many as he wants.
Until now, I used QQuickWidgets. The QGraphicsScene contains a top-level widget which is the parent of all the QQuickWidgets I create. It works fine, but I have a performance problem. With a high number of objects, the application starts to slow down, and takes too much space in RAM (more than 1.5 GB with the first example I created, containing 400 objects).
I thought it comes from the way QQuickWidgets are handled by Qt, and wanted to try another way, with QQuickViews. To do so I created a root view, converted in a QWidget so I can embed it in my view, which is a QWidget. Then I add a new QQuickView in the root view for each created object.
The creation of the root view, its container and the engine:
_rootView = new QQuickWindow();
_rootView->resize(1024, 720);
_rootView->show();
QWidget *container = QWidget::createWindowContainer(_rootView, this);
container->resize(_rootView->size());
container->setObjectName("TopLevelQmlViewWidget");
_layout->addWidget(container);
_engine = new QQmlEngine(_rootView);
The creation of the QQuickViews representing the objects:
QQuickView *view = new QQuickView(_engine, _rootView);
view->setSource(url);
view->setResizeMode(QQuickView::SizeViewToRootObject);
view->show();
It works, but the problem is that each QQuickView creates its own thread, which doesn't change the way I handle it but takes place in memory. I don't understand why, because I reparent them to the root view.
So my questions are the following :
1 - Is there a way to prevent the QQuickViews to create their own threads ?
2 - Is using QQuickViews, indeed, less memory-consuming than using QQuickWidgets ?
3 - If no, how can I handle adding a big number of QML objects in a QWidget view without consuming too much memory ?
I think using multiple QQuickViews is a bad idea. An application usually only needs one. I would take a look at QQmlComponent instead. Here is an example:
QQmlComponent component(_engine, QUrl::fromLocalFile("MyItem.qml"));
QQuickItem *childItem = qobject_cast<QQuickItem*>(component.create());
childItem->setParentItem(_rootView);
I'm by no means a QML expert. However, here are some pointers I can think of
Avoid mixing and matching QQuick: widget/View.
Consider creating objects dynamically
You can also make use of Loaders, but they have a small amount of extra overhead.
Consider using something like a stack/swipe view to minimize amount of loaded objects.
For best ROI, I'd first try implementing something like stack view and see how much it may help with the RAM. Then go on to create other objects dynamically as needed.
Finally I think QT has a tool that lets you see during runtime the amount of memory of the QML tree. You can look at that and see where your biggest memory hogs are.

emit signal if cell of QTableView is clicked [duplicate]

I have a QTableView in a PyQt application, and I want to keep track of when the selection changes. I've tried connecting the signal to a slot as follows (using the advice on this page:
self.view.selectionModel().selectionChanged.connect(self.selChanged)
where the slot it is connected to is defined as:
def selChanged(self, selected, deselected):
print "Sel changed"
However, whenever I load the QMainWindow on which the QTableView resides, I get an immediate segmentation fault.
Am I doing something silly here?
I was having a similar problem and the fix was here:
PySide: Segfault(?) when using QItemSelectionModel with QListView
Namely, replace:
self.view.selectionModel().selectionChanged.connect(self.selChanged)
with two commands:
selectionModel = self.view.selectionModel()
selectionModel.selectionChanged.connect(self.selChanged)
Not sure why this works, frankly.
This has been fixed now, it turned out that I was using an old version of Qt on that machine - which seemed to cause the crash.
The moral of the story is: if your code is crashing for no sensible reason, check all of your dependencies (in this case Qt and PyQt) are up-to-date.

Create UI Button from C# script in Unity 4.6 without any prefabs [duplicate]

This question already has an answer here:
Unity Create UI control from script
(1 answer)
Closed 5 years ago.
How can I create a normal Button with text label in Unity 4.6, without using any prefab or cloning existing game objects? I will use the button for debug purposes so I don't want to clutter up the design hierarchy with this button.
You can use this for UI 4.6
public void CreateButton(Transform panel ,Vector3 position, Vector2 size, UnityEngine.Events.UnityAction method)
{
GameObject button = new GameObject();
button.transform.parent = panel;
button.AddComponent<RectTransform>();
button.AddComponent<Button>();
button.transform.position = position;
button.GetComponent<RectTransform>().SetSize(size);
button.GetComponent<Button>().onClick.AddListener(method);
}
The following applies only to the legacy "gui" system from Unity3 (before about 2012). It is now unavailable.
Add this function to any script and it will display 100 x 100 pixel sized button on the left upper corner. Then just change the Debug.Log to your debug code.
void OnGUI() // deprecated, use ordinary .UI now available in Unity
{
if(GUI.Button(new Rect(0, 0, 100, 100), "Debug!")){
Debug.Log("Do some debugging");
}
}
you need to create a prefab of the element of UI you want to instantiate.
And after you can manipublate your ui element like a gameobject and use GetComponet method for manipulate properties and methods.
See the FAQ http://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html

QT: How to close multiple windows of the same widget?

I'm a beginner in programming in C++/Qt.
I created a widget called wexample in Qt. When displayed, there is an button event that will open another window of the same widget wexample, and so on. My question is how to close all the windows of that widget?
I open my first widget as follows:
wexample *w = new wexample;
w-> show();
Inside the widget, I also have these events:
void wexample::on_pushButton1_clicked()
{
wexample *w = new wexample;
w -> show();
}
void wexample::on_pushButton2_clicked()
{
QWidget::close();
}
So when button 1 is pressed, it will open a new window and so on. When button 2 is pressed, it will close the window where the button is. Is there a way to close all of the windows from that widget all at once? Or even better, is there a way to close specific windows (for example, all the windows after the 3rd one)?
I have tried using signal and slot but I can't connect them since they are all of the same name. I would have to declare all of the widgets beforehand for it to work but I cannot know how many of them the user will need.
I'm sorry if my question isn't clear. I am really a beginner and have been searching for a while but couldn't find an answer. Maybe the structure as a whole doesn't make sense. Please help. Thanks.
You should add your newly created wexample's to a list and then iterate through them when you'd like to close them:
class wexample : public QDialog
{
...
private Q_SLOTS:
void on_pushButton1_clicked() {
wexample *w = new wexample(this);
m_wexamples.append(w);
w->show();
}
void wexample::on_pushButton2_clicked() {
foreach (wexample *w, m_wexamples)
w->close();
}
private:
QList<wexample*> m_wexamples;
};
A few extra points here:
Notice that I added (this) to the wexample constructor above. This ensures that the dialog is properly parented (and therefore will be cleaned up if you don't manually delete the object yourself). Previously, you would have been leaking memory every time you showed the dialog
The slot for the second button simply iterates through the list of dialogs and closes them. This does NOT mean that the dialogs have been deleted (and in fact are still in the list after closing them). If you'd prefer to fully remove them, then use of the "take" methods of QList, removing the dialog from the list and then call dialog->close(); dialog->deleteLater(); on it
This is a somewhat contrived example, you probably won't be opening the dialog from within the dialog's implementation in practice

qt malloc(): smallbin double linked list corrupted

In my Qt widget I sometimes get this error:
malloc(): smallbin double linked list corrupted
It does not happen all the time but I think I have narrowed it down to when it starts.
I have a QGraphicsView and QGraphicsScene and there I'm drawing lines whos points are stored in a vector. Reason for this is I need to pass this points to another library. Once I draw the points I have an option if I click on a line I'm prompted to another window where I can change the coordinates of a line.
ResizeDialog *dialog = new ResizeDialog(this);
dialog->exec();
delete dialog;
The above code is the code I use to open a new QDialog. I know if I use this->close() the
qt malloc(): smallbin double linked list corrupted does not appear but then I lose the instance of QGraphicsView. Reason I need to keep the QGraphicsView window open if I need to chose to add further lines.
Any advice on how I can eliminate this issue wold be helpful.
Rather than using delete dialog;, use dialog->deleteLater();. I assume the small code portion is inside a slot of the object referenced by "this", and direct deletion is source of trouble as ResizeDialog *dialog = new ResizeDialog(this); affect the parent object this.

Resources