English 中文(简体)
(Qt Publishing - Cpp Based Application) Q<Objects> VS 使用点器?
原标题:(Qt Creator - Cpp Based Application) Q<Objects> VS using Pointers?

我的守则是:

//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)

谢谢!

问题回答
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;
};

而分类法也较少,因为你不必重写成员类别名称,以在建筑商中开始。

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.

此外,

  • 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);
}

在删除“MainWindow时,其非法定类别成员按其在该类声明的相反顺序删除。 http://code>scrollArea,首先销毁textEdit,但scroll 《区域还销毁了儿童,包括<>编码>textEdit,从而有效销毁了造成坠毁的两倍。

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签