0

I have formed a grid layout with QGraphicsProxyWidgets which are Qlabel widgets added to a scene. I want to update the background color of the widgets with a button click.

How can I access the QLabel so I can change the background color? I tried using a palette, but it caused the program to crash.

void MainWindow::changeBackgroundOfButtons()
{

int itemCount = layout->count();

int numCols = 4;
int row = 0, col = 0;
for (int i = 0; i < itemCount; ++i)
{
    QGraphicsProxyWidget *currentWidget = dynamic_cast<QGraphicsProxyWidget*>(layout->itemAt(row, col));

    QPalette p(palette());
    p.setColor(QPalette::Base, Qt::lightGray);
    currentWidget->setPalette(p);

    col++;
    if (col == numCols) row++;
    col = col % numCols;
}

}
Cortex0101
  • 831
  • 2
  • 12
  • 28
surya sai
  • 11
  • 3
  • 3
    It's most likely crashing because `layout->itemAt(row, col)` does not return a `QGraphicsProxyWidget`, which means `dynamic_cast` will return `nullptr` – Tim Meyer Nov 19 '21 at 07:21
  • Thank you! this worked. but how can I get the `QGraphicsProxy` Widget from `layout->itemAt(row, col)`. When I'm adding to the layout I was using `QGraphicsProxy` only. – surya sai Nov 20 '21 at 05:54

0 Answers0