我正在尝试将QgraphicsView(QColorDialog)
窗口小部件添加到Palette
对话框中,但与QGraphicsScene
窗口小部件对应的QColorDialog
始终是空白的,它会非常棒如果读者可以帮我纠正错误,请提供帮助。
Qt的4.8.4-Linux操作系统(CentOS的)
将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());
}
PaletteDialog
构造函数
PaletteDialog::PaletteDialog(QWidget *parent)
: QDialog(parent),
ui(new Ui::PaletteDialog),
{
//PaletteDialog sets up the ClrWidget
ui->setupUi(this);
...
}
答案 0 :(得分: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);
答案 1 :(得分: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);
}