Почему не происходит сбоя при назначении виджета-члена в качестве центрального виджета в QMainWindow ⇐ C++
-
Anonymous
Почему не происходит сбоя при назначении виджета-члена в качестве центрального виджета в QMainWindow
I wrote the following code which doesn't provoke any error:
main.cpp
#include "main_window.hpp" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } main_window.hpp
#pragma once #include "ui_main_window.h" #include #include class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); setCentralWidget(&view); } private: Ui::MainWindowClass ui; QGraphicsView view; }; However, Qt documentation for QMainWindow::setCentralWidget says that
Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.
Shouldn't there be a crash on finishing app because of double deleting of view: by Qt's ownership model and and by ~MainWindow()?
Источник: https://stackoverflow.com/questions/780 ... n-qmainwin
I wrote the following code which doesn't provoke any error:
main.cpp
#include "main_window.hpp" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } main_window.hpp
#pragma once #include "ui_main_window.h" #include #include class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); setCentralWidget(&view); } private: Ui::MainWindowClass ui; QGraphicsView view; }; However, Qt documentation for QMainWindow::setCentralWidget says that
Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.
Shouldn't there be a crash on finishing app because of double deleting of view: by Qt's ownership model and and by ~MainWindow()?
Источник: https://stackoverflow.com/questions/780 ... n-qmainwin
Мобильная версия