简体   繁体   English

Qt C++ 初始化一个 QStringList 类成员

[英]Qt C++ initialize a QStringList class member

I have a class member of type QStringList .我有一个QStringList类型的类成员。 I had a guess that when this class is instantiated, nex instance get such a QStringLsit with no elements in it.我有一个猜测,当这个类被实例化时, nex实例会得到这样一个没有元素的QStringLsit
I have a function that should fill in this QStringList on user interaction.我有一个函数应该在用户交互时填写这个QStringList However, I see with debugger that class member does not exist at function call.但是,我在调试器中看到函数调用时不存在类成员。 What could it be all about?这到底是怎么回事? Is there a way to initialize the QListString that I may be missing here?有没有办法初始化我可能在这里丢失的QListString

QStringList should get initialized on the creation of your object automatically (unless it is a pointer). QStringList应该在创建对象时自动初始化(除非它是指针)。 I see two possible explanations for the debugger's behaviour:对于调试器的行为,我看到了两种可能的解释:

  • Your object (of which you call the function) doesn't get created before the function call;您的对象(您调用该函数的对象)不会在函数调用之前创建; that means that the this pointer is invalid when the function gets entered.这意味着当函数进入时this指针是无效的。 This may be the case if you call the function on an uninitialised pointer to your class.如果您在指向您的类的未初始化指针上调用该函数,则可能就是这种情况。
  • The debugger just doesn't show the QStringList member correctly.调试器只是没有正确显示QStringList成员。
  • If your member is of type QStringList* (a pointer), it doesn't get automatically initialised on object creation.如果您的成员属于QStringList*类型(指针),则它不会在对象创建时自动初始化。 Then you'd need to do list = new QStringList();然后你需要做list = new QStringList(); in the constructor.在构造函数中。 But I doubt that you need a pointer to a string list here.但我怀疑您是否需要一个指向字符串列表的指针。

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

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