开发者

(Qt Creator - Cpp Based Application) Q<Objects> VS using Pointers?

here is my code:

//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui>

class MainWindow : public QWidget
{
    Q_OBJECT

publi开发者_如何学Cc:
    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!


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

MainWindow::~MainWindow()
{
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜