Search code examples
c++qtqt4

Widgets within a QGraphicsScene


I'm trying to add a QgraphicsView(QColorDialog) widget onto a Palette dialog, but the QGraphicsScene corresponding to the QColorDialog widget is always blank and it would be of great help if readers could help me correct my mistake.

Qt-4.8.4-Linux(CentOS)

  1. The GraphicsView widget which will be included in the PalletteDialog

    ClrWidget::ClrWidget(QWidget *parent) :
      QGraphicsView(parent)
    {
      setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      setFrameStyle(QFrame::NoFrame);
    
      setScene(new QGraphicsScene(this));
    
      _dialog = new QColorDialog();
      _dialog->setOption(QColorDialog::NoButtons, true);
      setMinimumSize(_dialog->size());
      setMaximumSize(_dialog->size());
    
      QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget();
      proxyWidget->setWidget(_dialog);
      //scene()->addItem(proxyWidget);
      //scene()->setSceneRect(proxyWidget->geometry());
    
      scene()->addWidget(_dialog);
      scene()->setSceneRect(_dialog->geometry());
    }
    
  2. PaletteDialog Constructor

    PaletteDialog::PaletteDialog(QWidget *parent)
      : QDialog(parent),
      ui(new Ui::PaletteDialog),
    {
      //PaletteDialog sets up the ClrWidget
      ui->setupUi(this);
      ...
    }
    

Solution

  • Try something like this:

    setScene(new QGraphicsScene);
    QColorDialog *_dialog = new QColorDialog();
    _dialog->setOption(QColorDialog::NoButtons, true);
    _dialog->show();
    QGraphicsProxyWidget *proxyWidget = scene()->addWidget(_dialog);
    

    And remove this line:

    proxyWidget->setWidget(_dialog);

    What you did wrong was set a widget to a QGraphicsProxyWidget and then called QGraphicsScene::addWidget(..) which does the same thing, and that doesn't work. You can add your QGraphicsProxyWidget to the scene though by calling QGraphicsScene::addItem().

    Example:

    setScene(new QGraphicsScene(this));
    QColorDialog *_dialog = new QColorDialog;
    _dialog->setOption(QColorDialog::NoButtons, true);
    QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
    proxyWidget->setWidget(_dialog);
    scene()->addItem(proxyWidget);