Pregunta

In my project, I have 256 PushButtons and I made a function that adds them all to a QButtonGroup.

void MainWindow::AddBlocksToGroup()
{
    QButtonGroup* blockGroup = new QButtonGroup(this);
    blockGroup->addButton(ui->Oblock_0, 0);
    blockGroup->addButton(ui->Oblock_1, 1);
    blockGroup->addButton(ui->Oblock_2, 2);
    ...
    blockGroup->addButton(ui->Oblock_255, 255);
}

Yes I know there are better ways to do this other than one at a time, but this makes my brain happier. lol

Later in my program (in a later function), I want to cycle through all the buttons and change some parameters for each one. (Changing text for example)

for(int i=0; i<=255; i++)
{
    blockGroup->button(i)->setText("Test");
}

But I get an undeclared identifier for blockGroup in my loop. Can someone tell me why and/or how to fix this?

Thanks for your time :)

¿Fue útil?

Solución

Declare the QButtonGroup* blockGroup; as a member in the MainWindow class, instead of declaring it a local variable of void MainWindow::AddBlocksToGroup() and that will make it accessible from your later member function. //don't forget to add a forward declaration for class QButtonGroup into MainWindow header file

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top