findChildren返回具有公共基类的子类

findChildren返回具有公共基类的子类

本文介绍了为什么QObject :: findChildren返回具有公共基类的子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用QObject作为复合模式的基类。



假设我有一个父类File(在一个设计的例子中),我添加了不同类型的子类,HeaderSection和PageSection。 File,HeaderSection和PageSection都是Sections。 Section的构造函数获取一个父对象,它被传递给QObject的构造函数,设置父对象。



例如:

  class Section:public QObject {
Q_OBJECT

//父子关系由QObject设置
Section(QString name,Section * parent = NULL):QObject(parent)
{setObjectName(name); }
QString name(){return objectName();}
};

类文件:public Section {
public:
//可能与这个例子不相关,但我也填充这些列表
QList< Section *>标题
QList< Section *>页面;
};

class Header:public Section {
Header(QString name,File * file):Section(name,file){}
};

类页面:public Section {
Body(QString name,File * file):Section(name,file){}
};

定义中的构造语法可能不正确,道歉,我习惯在外面做。无论如何,当我这样做:

  File * file = new file(); 
Header * headerA = new Header(Title,file);
Header * headerB = new Header(Subtitle,file);
页* page1 =新页(PageOne,文件);
页* page2 =新页(PageTwo,文件);

QList< Page *> pages = file-> findChildren< Page *>();

for(int i = 0; i< pages.size(); i ++)
qDebug()< pages.at(i) - > name();

我得到以下输出:



标题



字幕



PageOne



/ p>

我在这里缺少什么?当然,如果findChildren寻找共同的基类,那么它只会返回一个Widget的每一个孩子(例如),我知道它不正常使用。



此外,如果我遍历所返回的子节点列表,并在每个返回的节点上使用 dynamic_cast< Page *>

$ p

解决方案

答案是@Mat和@ratchet怪胎告诉我 - 我需要Q_OBJECT在每个子类中,不只是基类。


I am using QObject as a base class for a composite pattern.

Say I have a parent class File (in a contrived example) to which I am adding children of different types, HeaderSection and PageSection. File, HeaderSection and PageSection are all Sections. The constructor for Section takes a parent object which is passed through to QObject's constructor, setting the parent.

e.g:

class Section : public QObject {
 Q_OBJECT

 // parent:child relationship gets set by QObject
 Section(QString name, Section *parent=NULL) : QObject(parent)
 { setObjectName(name);}
 QString name(){return objectName();}
};

class File: public Section {
public:
 // probably irrelevant to this example, but I am also populating these lists
 QList<Section *> headers;
 QList<Section *> pages;
};

class Header : public Section {
Header(QString name, File *file) : Section(name, file){}
};

class Page: public Section {
 Body(QString name, File *file) : Section(name, file){}
};

Syntax for construction in the definition may be incorrect, apologies, I'm used to doing it outside. Anyway, when I do this:

File *file = new file();
Header *headerA = new Header("Title", file);
Header *headerB = new Header("Subtitle", file);
Page *page1 = new Page("PageOne", file);
Page *page2 = new Page("PageTwo", file);

QList<Page*> pages = file->findChildren<Page*>();

for(int i=0; i < pages.size(); i++)
  qDebug() << pages.at(i)->name();

I get the following output:

Title

Subtitle

PageOne

PageTwo

What am I missing here? Surely if findChildren looked for common base classes then it would only ever return every single child of a Widget (for example), which I know it doesn't in normal use.

Also, if I iterate over the list of children returned and use dynamic_cast<Page*> on each returned child, I get the expected two Page items.

解决方案

The answer is as @Mat and @ratchet freak tell me - I needed Q_OBJECT in every subclass, not just the base class.

这篇关于为什么QObject :: findChildren返回具有公共基类的子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:20