简体   繁体   中英

Qt QObject dynamic array

Is there a different way to create a dynamic array of 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

You're seeing this because QAction doesn't have a default constructor.

You could create an array of pointers to QAction and then instantiate each QAction on it's own.

Something roughly like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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