Stefano Crocco
6/14/2009 5:46:00 PM
On Sunday 14 June 2009, Damjan Rems wrote:
> |def create_central_widget
> | cw = Qt::Widget.new self
> | self.central_widget = cw
> |# Input search text
> | @search_text = Qt::LineEdit.new
> | @search_text.text = 'Search?'
> |# Search result list
> | @search_result = Qt::ListWidget.new
> |# Dummy fill
> | 1.upto(100) { |i| @search_result.add_item i.to_s }
> |# Middle window is editor
> | @big_editor = Qt::TextEdit.new
> | @big_editor.setPlainText("Just a text")
> |
> | search_area_widget = Qt::Widget.new
> |
> | l = Qt::VBoxLayout.new search_area_widget cw
> | l.add_widget @search_text
> | l.add_widget @search_result
> |
> | splitter = Qt::Splitter.new cw
> | splitter.add_widget(search_area_widget)
> | splitter.add_widget(@big_editor)
> |
> | end
> |##########################################################
> |
> |This is part of program which creates main window dialog.
> |
> |I would like to have two areas splitted by splitter. On the left side
> |there a two widgets. Input search LineEdit widget and search result
> |ListWidget widget. They both reside on VBoxLayout.
> |
> |On the second area there is big_editor TextEdit widget.
> |
> |My problem is that I expect that dialog will fill whole dialog window.
> |It will be streched from to to bottom and from left to right. But it
> |doesnt. It has it's default size (which I was unuble to resize). I have
> |tried all kind of streches but nothing helps.
> |
> |Splitter works as expected. It resizes left and right part of split area
> |and widget resize ok.
> |
> |What am I doing wrong.
You aren't adding a layout to your central widget. As a rule, every time you
create a container widget (that is, a widget which contains other widgets),
you have to give it a layout. If you do, the widget will take care of managing
the size of child widgets; if you don't you'll find that child widgets won't
fill it or, worse, will extend outside its borders. In your case, you're only
adding a single widget to the layout, so you could use either a Qt::VBoxLayout
or Qt::HBoxLayout. To do so, simply add the following two lines before the end
of the method:
cw.layout = Qt::VBoxLayout.new cw
cw.add_widget splitter
However, I think you use cw only to contain the layout. In this case, it would
be much more simple to use the splitter itself as central widget. Doing so,
your code would become something like this:
def create_central_widget
splitter = Qt::Splitter.new self
self.central_widget = splitter
# Input search text
@search_text = Qt::LineEdit.new
@search_text.text = 'Search?'
# Search result list
@search_result = Qt::ListWidget.new
# Dummy fill
1.upto(100) { |i| @search_result.add_item i.to_s }
# Middle window is editor
@big_editor = Qt::TextEdit.new
@big_editor.setPlainText("Just a text")
search_area_widget = Qt::Widget.new splitter
l = Qt::VBoxLayout.new search_area_widget
l.add_widget @search_text
l.add_widget @search_result
splitter.add_widget(search_area_widget)
splitter.add_widget(@big_editor)
end
Note that there's a mistake in your original code. The line
l = Qt::VBoxLayout.new search_area_widget cw
should become
l = Qt::VBoxLayout.new search_area_widget
At any rate, usually one doesn't build container widgets by hand, but uses the
Qt Designer to visually create them.
I hope this helps
Stefano