문제

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 :)

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top