2

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

mrg95
  • 2,371
  • 11
  • 46
  • 89

1 Answers1

1

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

Zlatomir
  • 6,964
  • 3
  • 26
  • 32
  • This solved the problem :) I'm now getting an error when I try changing the text, but that's a whole different problem. Thanks :) – mrg95 Aug 26 '13 at 03:57
  • Sorry to bother you, I'm having a frustrating read access violation error when I try to edit a button in the group. Perhaps I missed something here? I added QButtonGroup* blockGroup; in the public section of mainwindow.h. Is that all I needed to do? – mrg95 Aug 26 '13 at 05:38
  • Not really in the public section, a better option is to add it in the _private_ section of MainWindow and regarding the error you can debugg your application and see what happens (see especially the case where you try to navigate before you add buttons to the group) – Zlatomir Aug 26 '13 at 08:57
  • And also there is another way you can add buttons to a group in designer: select all the buttons that you want to add to a group, right click and select _Assign to button group_ (and you will than access your buttongroup trough _ui_ pointer: _ui->btnGprName_ ) – Zlatomir Aug 26 '13 at 09:02