简体   繁体   English

使用QThread和QWidget进行多重继承(使用QObject进行多重继承)

[英]Multiple inheritance with QThread and QWidget ( Multiple inheritance with QObject )

I'm going to design OgreWidget class -A portable renderer widget with Qt. 我将使用Qt设计OgreWidget类 - 一个可移植的渲染器小部件。

(With my design), I think my class need to be inherited with QThread (for infinite render loop ) and QWidget ( target widget for ogre to render there) . (根据我的设计),我认为我的类需要继承QThread (用于无限渲染循环)和QWidget (用于在那里渲染的ogre的目标小部件)。

But according to many documentation and articles (for instance this ), Virtual inheritance with QObject is not supported . 但是根据许多文档和文章(例如这个 ),不支持使用QObject虚拟继承。 Result of this inheritance will be such a error: 这种继承的结果将是这样一个错误:

QObject is an ambiguous base of OgreWidget QObjectOgreWidget的模糊基础

How should I resolve this problem ? 我该如何解决这个问题?

PS: In my old design , I create a separate QWidget , and Send It's WId to my OgreWidget as target widget. PS:在我老的设计,我创建一个单独QWidget ,并发送它WIdOgreWidget为对象的微件。 However, I'm now going to design a better and cleaner interface. 但是,我现在要设计一个更好,更清洁的界面。

That's impossible, because both QThread and QWidget in the end resolve to QObject base class 这是不可能的,因为QThreadQWidget最终都解析为QObject基类

This thread answers your question: how can i inherit from both QWidget and QThread? 这个主题回答了你的问题: 我如何从QWidget和QThread继承?

The QThread documentation is misleading, you don't need to and shouldn't be sub classing QThread here for your widget. QThread文档具有误导性,您不需要也不应该在这里为您的小部件分类QThread

"You're doing it wrong" - http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/ “你做错了” - http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/

You should either: 你应该:

  • Create a QThread ogreThread , create your ogreWidget and ogreWidget.moveToThread(&ogreThread) , or 创建一个QThread ogreThread ,创建你的ogreWidgetogreWidget.moveToThread(&ogreThread) ,或者
  • Create a QThread wrapper that allows you to tell it to create a new object of type T directly in the new thread. 创建一个QThread包装器,允许您告诉它直接在新线程中创建类型为T的新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM