简体   繁体   English

Qt QObject动态数组

[英]Qt QObject dynamic array

Is there a different way to create a dynamic array of QObject? 有没有其他方法可以创建动态的QObject数组? The following code would not compile: 以下代码无法编译:

QStringList labels = defaultScene->getLabels();
QAction* traceActions = new QAction[labels.size()];

The error is: 错误是:

C2512: 'QAction' : no appropriate default constructor available C2512:“ QAction”:没有适当的默认构造函数

You're seeing this because QAction doesn't have a default constructor. 您正在看到这是因为QAction没有默认的构造函数。

You could create an array of pointers to QAction and then instantiate each QAction on it's own. 您可以创建一个指向QAction的指针数组,然后自行实例化每个QAction。

Something roughly like: 大致类似于:

QAction** actions = new (QAction*)[labels.size()];
for(size_t i = 0; i<labels.size(); ++i)
{
  actions[i] = new QAction(constructor params ...);
}

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

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