PDA

View Full Version : QGraphicsLinearLayout does not behave like QVBoxLayout



jobrandt
5th September 2009, 11:18
The left side(QGraphicsView,QGraphicsLinearLayout based)of the window should behave like the right side(QWidget,QVBoxLayout based) of the window.
This means QTreeWidget and QPushBotton should fill the whole area of QGraphicsView. Also if the window is resized.

Any ideas how to fix this code? Or is there a complete different way?


GraphicWidgetTest::GraphicWidgetTest(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

// left side
QGraphicsView *view = new QGraphicsView(ui.centralWidget);
QGraphicsScene *scene = new QGraphicsScene;
view->setScene(scene);

//scene->setBackgroundBrush(QBrush(QColor(Qt::red)));
QGraphicsLinearLayout *vboxl = new QGraphicsLinearLayout;
vboxl->setOrientation(Qt::Vertical);
vboxl->addItem(scene->addWidget(new QTreeWidget));
vboxl->addItem(scene->addWidget(new QPushButton("buttonl")));

QGraphicsWidget *form = new QGraphicsWidget;
form->setLayout(vboxl);
scene->addItem(form);

// right side
QWidget *right = new QWidget(ui.centralWidget);
QVBoxLayout *vboxr = new QVBoxLayout;
vboxr->addWidget(new QTreeWidget);
vboxr->addWidget(new QPushButton("buttonr"));
right->setLayout(vboxr);

//
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(view);
hbox->addWidget(right);

ui.centralWidget->setLayout(hbox);
}

wysota
5th September 2009, 11:53
To do that you have to scale the scene according to the size of the view (i.e. in it's resizeEvent).

bunjee
5th September 2009, 12:45
You have to be cautious with QGraphicsLinearLayout.

It's not called QGraphicsBoxLayout for a reason. It doesn't behave exactly the same.

jobrandt
7th September 2009, 09:59
I have implemented the resizeEvent(). It works good if the size of the window is increased. But it does not decrease the size of QTreeWidget,QPushBotton if the windows size is decreased.
Do i have to resize any other Widget?


GraphicWidgetTest::GraphicWidgetTest(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

// left side
view = new QGraphicsView(ui.centralWidget);
scene = new QGraphicsScene;
view->setScene(scene);

scene->setBackgroundBrush(QBrush(QColor(Qt::red)));

left = new QGraphicsWidget();
left->resize(view->size());
QVBoxLayout *vboxw = new QVBoxLayout;
Tree = new QTreeWidget;
Button = new QPushButton("buttonl");
QGraphicsProxyWidget *gTree = scene->addWidget(Tree);
QGraphicsProxyWidget *gButton = scene->addWidget(Button);
QGraphicsLinearLayout *vboxl = new QGraphicsLinearLayout;
vboxl->setOrientation(Qt::Vertical);
left->setLayout(vboxl);
vboxw->addWidget(Tree);
vboxw->addWidget(Button);
scene->addItem(left);
//vboxl->addItem(scene->addWidget(left));
vboxl->addItem(gTree);
vboxl->addItem(gButton);

//QGraphicsWidget *form = new QGraphicsWidget;
//form->setLayout(vboxl);
//scene->addItem(form);

// right side
right = new QWidget(ui.centralWidget);
QVBoxLayout *vboxr = new QVBoxLayout;
vboxr->addWidget(new QTreeWidget);
vboxr->addWidget(new QPushButton("buttonr"));
right->setLayout(vboxr);

//
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(view);
hbox->addWidget(right);

ui.centralWidget->setLayout(hbox);

connect(Button,SIGNAL(clicked()),this,SLOT(buttonr Clicked()));
}

GraphicWidgetTest::~GraphicWidgetTest()
{
}

void GraphicWidgetTest::buttonrClicked()
{
left->resize(view->size().width()-3,view->size().height()-3);
}

void GraphicWidgetTest::resizeEvent(QResizeEvent *event)
{
left->resize(view->size().width()-3,view->size().height()-3);
}