PDA

View Full Version : QSplitter in reconfigurable gui not scaling as expected



wynand
17th October 2010, 09:37
I’m currently writing an IDE with a reconfigurable gui system. Basically I’m allowing the user to split the screen vertically and horizontally similarly to how it’s implemented in vim using “split” and “vsplit”. I’m currently using QSplitter(s) to implement the splits. If all the new splits occur in the same direction, i.e. horizontally only or vertically only, I simply add the new QTabWidget to the existing splitter. However if there is a change of direction, I create a new QSplitter and add the new and current QTabWidget to the newly created splitter. Using this mechanism I build up a complete tree of widgets in a custom widget_tree class I wrote. Everytime the tree change, I un-parent every widget in the tree, i.e. all the QSplitters and all the QTabWidgets, by setting the widget’s parent to NULL, then I re-parent the widgets using the tree structures to ensure the widgets are added to the QSplitters in the right order, and the correct parent-child relationship exist. This appears to be all working properly. (The code for this is quite long so I described it rather than post it)

The problem is that when the widget is split, it creates an area which is consistently about 10px wide/high and the other one 300 – 400 px. I tried correcting this by setting the scaling factor of each child to 1, to indicate an even distribution in the splitter. I would expect the gui to be completely split in half if it only had two QTabWidget and single QSplitter. It doesn’t, and I’m out of ideas. I’m wondering if I’m maybe doing something out of order, or just plainly wrong. I've attached the code for re-parenting below, and also the image of what it generates. Any ideas would be appreciated.


void rtcpp::code_tab_widget_tree::reparent_gui_widget(n ode * cur_node, QWidget * cur_parent)
{
// Check if the current node is a root node
if(cur_node->is_root() == true)
{
// Check if the base grid is valid
if(_base_grid == NULL)
_base_grid = new QGridLayout(cur_parent);

// Set the parent of the current widget
cur_node->widget()->setParent(cur_parent);

// Add the cur_node's widget to the grid layout
_base_grid->addWidget(cur_node->widget());
}

// Reparent all the emediate children
for(std::vector<node*>::iterator i = (cur_node->children()).begin(); i != (cur_node->children()).end(); i++)
(*i)->widget()->setParent(cur_node->widget());

// Set the stretch factor for each emediate child
if(cur_node->type() == node::node_splitter)
{
QSplitter * splitter = ((QSplitter*)(cur_node->widget()));
for(int i = 0; i < splitter->count(); i++)
splitter->setStretchFactor(i, 1);
}

// Reparent each child widget
for(std::vector<node*>::iterator i = (cur_node->children()).begin(); i != (cur_node->children()).end(); i++)
reparent_gui_widget((*i), cur_node->widget());
}

The result (Wrong):
http://www.freeimagehosting.net/uploads/f021d397b7.png

What sometimes happen (Right):
http://www.freeimagehosting.net/uploads/de466c14b2.png