1

我正在尝试将QgraphicsView(QColorDialog)小部件添加到Palette对话框中,但与小部件QGraphicsScene对应的QColorDialog始终为空白,如果读者能帮助我纠正我的错误,那将有很大帮助。

Qt-4.8.4-Linux(CentOS)

  1. GraphicsView包含在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构造函数

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

2 回答 2

4

尝试这样的事情:

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

并删除这一行:

proxyWidget->setWidget(_dialog);

您做错的是将小部件设置为 QGraphicsProxyWidget ,然后调用QGraphicsScene::addWidget(..)做同样的事情,但这不起作用。您可以通过调用QGraphicsScene::addItem()将 QGraphicsProxyWidget 添加到场景中。

例子:

setScene(new QGraphicsScene(this));
QColorDialog *_dialog = new QColorDialog;
_dialog->setOption(QColorDialog::NoButtons, true);
QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
proxyWidget->setWidget(_dialog);
scene()->addItem(proxyWidget);
于 2013-05-14T12:09:21.613 回答
1

在创建 时QGraphicsScene,我通常在调用之前将所有小部件添加到场景中QGraphicsView::setScene(scene)。在这种情况下,您可以删除对的调用,scene()->setSceneRect()因为它将自动由它包含的小部件的几何形状定义。修改后的代码如下所示:

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);
}
于 2013-05-14T13:19:41.683 回答