(Qt Creator - 基于 Cpp 的应用程序)QVS 使用指针?

发布于 2024-12-06 01:06:29 字数 852 浏览 3 评论 0原文

这是我的代码:

//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui>

class MainWindow : public QWidget
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    QTextEdit *textEdit;
};


#endif // MAINWINDOW_H

// MainWindow.cpp
#include "mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
{
textEdit = new QTextEdit();
}

MainWindow::~MainWindow()
{
    delete textEdit;
}

//main.cpp
#include <QtGui>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

它是否更有效(这是问题的“Q[Objects] VS使用指针?”部分):

1)像我实际做的那样使用指针或

2)使用对象(删除* +删除语句)

谢谢你!

here is my code:

//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui>

class MainWindow : public QWidget
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    QTextEdit *textEdit;
};


#endif // MAINWINDOW_H

// MainWindow.cpp
#include "mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
{
textEdit = new QTextEdit();
}

MainWindow::~MainWindow()
{
    delete textEdit;
}

//main.cpp
#include <QtGui>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

Is it more efficient (here's the "Q[Objects] VS using Pointers?" part of the question) to:

1) Use pointers as I am actually doing or

2) Use objects (removing * + delete statement)

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假装不在乎 2024-12-13 01:06:29
MainWindow::MainWindow(QWidget *parent)
{
textEdit = new QTextEdit(this);
}

MainWindow::~MainWindow()
{
}
MainWindow::MainWindow(QWidget *parent)
{
textEdit = new QTextEdit(this);
}

MainWindow::~MainWindow()
{
}
烏雲後面有陽光 2024-12-13 01:06:29

对于作为指针的 QObject 成员,您不应使用 deleteQTextEdit 可能是 MainWindow 的子级,所以它会被自动删除。

当然,理论上,使用非指针 QObject 成员会更快,并且间接层数少一级。像这样(对于那些不理解问题的人):

class MainWindow : public QMainWindow {
   ...
private:
   QTextEdit textEdit;
};

并且需要输入的代码也更少,因为您不必重新输入成员的类名来在构造函数中初始化它们。

但由于 QObject 本身已经大量使用间接(及其 d 指针),增益可能可以忽略不计。使用指针成员键入的额外代码可以让您在之间较低耦合你的头文件和 Qt,因为你可以使用前向声明而不是头包含,这意味着更快的编译(特别是如果你还没有使用预编译头)和重新编译。

此外,

  • 手动删除 QObject 指针成员或
  • 将 QObject 声明为非指针成员

如果您没有按照正确的顺序分别删除/声明它们, 可能会导致双重删除(孩子然后父母删除,或者父母然后孩子声明)。

例如:

class MainWindow : public QMainWindow {
   ...
private:
   QTextEdit textEdit; 
   QScrollArea scrollArea;
};

MainWindow::MainWindow() {
   setCentralWidget(&scrollArea);
   QWidget *scrolledWidget = new QWidget(&scrollArea);
   QVBoxLayout *lay = new QVBoxLayout(scrolledWidget);
   lay->addWidget(...);
   lay->addWidget(&textEdit); // textEdit becomes a grand-child of scrollArea
   scrollArea.setWidget(scrolledWidget);
}

MainWindow被删除时,它的非静态类成员按照它们在类中声明的相反顺序被删除。所以 scrollArea 首先被销毁,然后是 textEdit,但是 scrollArea 也销毁了它的子级,包括 textEdit,所以 textEdit 被有效地破坏了两次,导致崩溃。

因此,使用QObject成员作为指针,并且不删除自己有父级的QObject,更不容易出错。

For QObject members as pointers, you shouldn't use delete, the QTextEdit will probably be a child of MainWindow, so it will be deleted automatically.

It would, of course, be theoretically faster to use non-pointer QObject members, with one less level of indirection. Like this (for those who didn't understand the question):

class MainWindow : public QMainWindow {
   ...
private:
   QTextEdit textEdit;
};

and there is also less code to type, because you don't have to retype the class name of the members to initialize them in the constructor.

But since QObject are themselves already heavily using indirection (with their d-pointer), the gain will probably be negligible. And the extra code you type with pointer members allows you to have a lower coupling between your header files and Qt, because you can use forward declarations instead of header inclusion, which means faster compilations (especially if you are not using precompiled headers yet) and recompilations.

Also,

  • manually deleting QObject pointer members, or
  • declaring QObject as non-pointers members

can causes double deletion, if you don't respectively delete/declare them in the right order (children then parents for deletion, or parents then children for declaration).

For example:

class MainWindow : public QMainWindow {
   ...
private:
   QTextEdit textEdit; 
   QScrollArea scrollArea;
};

MainWindow::MainWindow() {
   setCentralWidget(&scrollArea);
   QWidget *scrolledWidget = new QWidget(&scrollArea);
   QVBoxLayout *lay = new QVBoxLayout(scrolledWidget);
   lay->addWidget(...);
   lay->addWidget(&textEdit); // textEdit becomes a grand-child of scrollArea
   scrollArea.setWidget(scrolledWidget);
}

When MainWindow is deleted, its non-static class members are deleted in the reverse order of their declaration in the class. So scrollArea is destroyed first then textEdit, but scrollArea also destroys its children including textEdit, so textEdit is effectively destroyed twice causing a crash.

So it is less error prone to use QObject members as pointers, and to not delete the QObject which have a parent yourself.

折戟 2024-12-13 01:06:29

尝试在堆栈上创建 QLineEdit,然后将其放入布局...退出应用程序...您看到什么?提示:在调试器中启动您的应用程序。

ksming 关于阅读文档的说法是正确的。这不是特定于语言的问题。如果您问哪个更快:堆分配或堆栈分配,那么请正确地提出您的问题。

Try creating QLineEdit on the stack and then put it into layout... Quit your application... What do you see? HINT: launch your application in debugger.

ksming is right about reading documentation. It is not language specific issue. If you are asking what is faster: heap or stack allocation then form your question properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文