Fornax-A
Fornax-A

Reputation: 1032

For-based loop for large number of QMenus

being a noobie with Qt I couldn't figure out how to create a vector of QMenu.

Using C++ I got the following (simple) idea:

std::vector<QMenu> *subMenus;

and then use a for-loop to create a certain number of menus.

for (int i = 0; i < List.size(); ++i)
{
   subMenus[i] = new QMenu('some name',MainMenu);
   MainMenu->addMenu(subMenus[i]);
}

Where the size of List is a large number (~40). The problem is that in doing this I face the following error message refering to the index i:

expression must have a constant value.

I am not sure if I should use append or push back to subMenus to avoid this error.

Thanks

Upvotes: 0

Views: 53

Answers (1)

Hubi
Hubi

Reputation: 1629

You can use something like this:

QVector<QMenu*> menuVector;
for (int i = 0; i < 40; ++i)
{
   auto* menu = new QMenu('some name', MainMenu);
   menuVector.push_back(menu );
   MainMenu->addMenu(menu);
}

Upvotes: 2

Related Questions