Is there a right or wrong to own in Qt

I would like to write “modern C ++” Qt applications, keeping in mind as much RAII as possible. Therefore, I ask myself if automatic distribution can be safely used when possible:

#include <QApplication>
#include <QtWidgets>
int main(int argc, char **argv) {
    QApplication app{argc, argv};
    QWidget window{};
    window.setWindowTitle("Der eine Knopf");
    QPushButton button{"Ende"};
    QObject::connect( &button, SIGNAL(clicked()), &app, SLOT(quit()));
    QVBoxLayout layout{};
    layout.addWidget(&button);
    window.setLayout(&layout);
    window.show();
    return app.exec();
}

While the source code for the tutorial had a lot of pointers and heaps:

#include <QApplication>
#include <QtWidgets>

int main(int argc, char **argv) {
  QApplication app{argc, argv};
  QWidget window{};
  window.setWindowTitle("Hallo Qt");
  QPushButton button = new QPushButton("Ende");
  QObject::connect( button, SIGNAL(clicked()),
      &app, SLOT(quit()));
  QVBoxLayout *layout = new QVBoxLayout;
  layout->addWidget(button);
  window.setLayout(layout);
  window.show();
  return app.exec();
}

I know the concept of owning Qt QObjectin general. Therefore, I assume that the second example is correct. I suppose that setLayoutthey addWidgetalso change the ownership, and therefore deleteI do not need the explicit one as a client.

, - ? , delete ? , , ? ( , . , ?)

, , , , . , , : "Qt ", ""? ?

, , Qt, QObject . / . Qt?

+4
1

, , . .

, :

int main(int argc, char **argv) {
    QApplication app{argc, argv};
    QWidget window{};
    window.setWindowTitle("Der eine Knopf");
    QPushButton button{"Ende"};
    QObject::connect( &button, SIGNAL(clicked()), &app, SLOT(quit()));
    QVBoxLayout layout{};
    layout.addWidget(&button);
    window.setLayout(&layout);
    window.show();
    return app.exec();
}

QWidget window. QPushButton. button . window button. , button. window. .

, button window, window button, . , button. button , . .

+2

Source: https://habr.com/ru/post/1678140/


All Articles