简体   繁体   English

我应该扩展QT小部件还是使用我自己的包含小部件的类作为成员?

[英]Should I extend QT widgets or use my own classes containing the widget as a member?

Learning QT (with C++), I have been defining my own classes, which are not QObjects derivates. 学习QT(使用C ++),我一直在定义自己的类,而不是QObjects派生。 Even if QT tutorial teach you to extend their widgets in order to make your own I don't because I was thinking that my code would be more “portable” and less QT inherent (if that makes sense) in case I switched to another graphical toolkit. 即使QT教程教你扩展他们的小部件以制作你自己的小部件我不喜欢因为我认为我的代码会更“便携”并且更少QT固有(如果这是有道理的)以防我切换到另一个图形工具包。

So my classes are something like this.- 所以我的课程是这样的.-

class mainWindow
{
    QVBoxLayout *winLayout;
    public:
        QWidget *winWidget;
        mainWindow();
}

...so, having the main widget as a public member and everything being operated from within the classes, adding methods to manipulate my widgets. ...所以,将主要小部件作为公共成员,并在类中操作所有内容,添加操作我的小部件的方法。

In case I need Signals and Slots, I make a class extending QObject and use the Q_OBJECT macro. 如果我需要Signals和Slots,我会创建一个扩展QObject的类并使用Q_OBJECT宏。

But now I wonder, is my notion (explained in the beginning) even vaguely right? 但现在我想知道,我的观念(在开头解释)甚至是模糊的吗? Do you think that I can face any issues in the future by doing this my way and not extending QT own widgets? 您是否认为我可以通过这样做而不是扩展QT自己的小部件来面对任何问题? What method should I stick to? 我应该坚持哪种方法?

Your clas is not a proper QWidget, but I don't think it would be a problem to Qt. 你的clas不是一个合适的QWidget,但我不认为这对Qt来说是个问题。 Unless you need to gain acces to some protected elements of wigets (eg. interact with events) you could go without ever inheriting qt widgets - the choice of design is yours. 除非你需要访问wigets的一些受保护元素(例如,与事件交互),否则你可以永远不会继承qt小部件 - 设计的选择就是你的。

The only reason for inheriting might be to save some memory taken by additional QObject, but if your class is not extensivly used in application, this should not be an issue. 继承的唯一原因可能是保存额外QObject占用的内存,但如果您的类在应用程序中没有被广泛使用,那么这应该不是问题。

About being less "Qt inherent" i can only say that moving application from one framework to another usually needs lots of changes, independently on your design, so this aregument doesn't really convince me. 关于不那么“Qt固有”我只能说将应用程序从一个框架移动到另一个框架通常需要进行大量的更改,独立于你的设计,所以这个问题并没有真正说服我。

On tth other hand this is generally bad idea to keep public field in a class like this - accidental modifications will cause difficult to track errors - think about making winWidget private and adding some accesor method perhaps? 另一方面,将公共字段保存在这样的类中通常是个坏主意 - 意外修改会导致难以跟踪错误 - 考虑将winWidget设为私有并可能添加一些访问方法?

When i was beggining to learn qt, I also had aversion to subclassing QWidgets, but soon i realised that it is simply more comfortable to inherit, mostly because you don't need heavy changes to your class if one day you need event-interaction. 当我开始学习qt时,我也厌恶继承QWidgets,但很快我意识到继承更简单,主要是因为如果有一天你需要事件交互,你不需要对你的课程进行大量修改。

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

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