rahul
rahul

Reputation: 406

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);
      ...
    }
    

Upvotes: 2

Views: 9534

Answers (2)

thuga
thuga

Reputation: 12901

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

Upvotes: 5

buster
buster

Reputation: 1048

When creating a QGraphicsScene, I usually add all of the widgets to the scene before calling QGraphicsView::setScene(scene). You can remove the call to scene()->setSceneRect() in this case because it will automatically be defined by the geometry of the widgets that it contains. The modified code would look like this:

ClrWidget::ClrWidget(QWidget *parent) : QGraphicsView(parent)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFrameStyle(QFrame::NoFrame);

QGraphicsScene *scene=new QGraphicsScene((QRect)geometry,this);
_dialog = new QColorDialog();
_dialog->setOption(QColorDialog::NoButtons, true);

//here you should call setGeometry unless it is called in the constructor
 _dialog->setGeometry(rect);
//

setMinimumSize(_dialog->size());
setMaximumSize(_dialog->size());

QGraphicsProxyWidget *proxyWidget = scene->addWidget(_dialog);

setScene(scene);
}

Upvotes: 1

Related Questions