[Qt-interest] QWidget background color

Leonardo M. Ramé l.rame at griensu.com
Thu Sep 23 21:20:40 CEST 2010


Hi, my app consists of a main window with a QGridLayout, where I add
custom QWidgets dynamically with layout->addWidget(new MyWidget(), x,
y). 

The user can change the layout choosing from 1x1, 1x2, 2x1, 2x2 and 3x3.
My problem is, whenever the user changes the layout, I remove the
previous disposition and recreate it again with the user's selection,
but the widgets are not painted correctly, until I click on them.

How can I solve this?.

This is the code to remove and re-generate the layout:

    // remove
    for(int i = layout->count() - 1; i >= 0; i--)
    {
        QWidget * widget = layout->itemAt(i)->widget();
        delete widget;
        delete layout->itemAt(i);
        layout->removeItem(layout->itemAt(i));
    }

    // re-create
    switch(i)
    {
      case 0: 
          layout->addWidget(new DCMWidget(), 0, 0);
          break;
      case 1: 
          layout->addWidget(new DCMWidget(), 0, 0);
          layout->addWidget(new DCMWidget(), 0, 1);
          break;
      case 2: 
          layout->addWidget(new DCMWidget(), 0, 0);
          layout->addWidget(new DCMWidget(), 1, 0);
          break;
      case 3: 
          layout->addWidget(new DCMWidget(), 0, 0);
          layout->addWidget(new DCMWidget(), 0, 1);
          layout->addWidget(new DCMWidget(), 1, 0);
          layout->addWidget(new DCMWidget(), 1, 1);
          break;
      ...
    }

-- 
Leonardo M. Ramé
http://leonardorame.blogspot.com




More information about the Qt-interest-old mailing list