# "Segmentation fault" with QtRuby

**URL:** <https://www.ruby-forum.com/t/segmentation-fault-with-qtruby/194426>\
**Category:** Ruby\
**Created:** [September 5, 2010, 12:50pm UTC](https://www.ruby-forum.com/t/segmentation-fault-with-qtruby/194426 "2010-09-05T12:50:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonechi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bonechi](https://www.ruby-forum.com/u/bonechi)\
**Post date:** [September 5, 2010, 12:50pm UTC](https://www.ruby-forum.com/t/segmentation-fault-with-qtruby/194426/1 "2010-09-05T12:50:47Z")

</div>

Hello everyone!

Using QtRuby, I tried to implement the “Read-only example model”  
(Creating new models chapter) of the “Model/View programming” technology  
section of Qt Assistant.

But when I launch the example, I got this error :  
main.rb:5: [BUG] Segmentation fault  
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]

Abandon

Here is the code :

# slmodel.rb

class SLModel \< Qt::AbstractListModel  
def initialize string\_list, parent = nil  
super parent

```
@string_list = string_list

```

end

def rowCount parent = Qt::ModelIndex.new  
@string\_list.length  
end

def data index, role  
return Qt::Variant.new unless index.isValid  
return Qt::Variant.new if index.row \>= @string\_list.length

```
return @string_list[index.row] if role == Qt.DisplayRole

Qt::Variant.new

```

end

# def headerData section, orientation, role = Qt.DisplayRole

# return Qt::Variant.new if role != Qt.DisplayRole

# 

# if orientation == Qt.Horizontal

# “Column #{section}”

# else

# “Row #{section}”

# end

# end

end

# window.rb

require ‘slmodel’

class Window \< Qt::Widget  
def initialize parent = nil  
super parent

```
@list = %w[Alpha Beta Gamma Omega]
@model = SLModel.new @list, self
@view = Qt::ListView.new

@view.setModel @model

layout = Qt::VBoxLayout.new
layout.addWidget @view

setLayout layout

```

end  
end

# main.rb

require ‘Qt4’  
require ‘window’

a = Qt::Application.new ARGV  
window = Window.new  
window.show  
a.exec

As the error message doesn’t give me that much explanations, I don’t  
know what is the origin of the problem…  
Is it because I forgot to implement some virtual methods from  
Qt::AbstractListModel?

Thanks!

---

<div class="post-metadata">

**Author:** ![bonechi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bonechi](https://www.ruby-forum.com/u/bonechi)\
**Post date:** [September 6, 2010, 7:40pm UTC](https://www.ruby-forum.com/t/segmentation-fault-with-qtruby/194426/2 "2010-09-06T19:40:28Z")

</div>

Thank you for the reply!

> The problem with your code is that the #data method must always return a  
> Qt::Variant. You correctly return an invalid Qt::Variant when the index  
> doesn’t correspond to a valid data, but when you pick a value from  
> @string\_list, you return the value as it is. From the name of the  
> variable  
> (and also from the code in the Window’s constructor) it’s clear that the  
> contents of this instance variable are strings, not Qt::Variant, so you  
> get  
> the error. The third line of the data method should be:
> 
> return Qt::Variant.new @string\_list[index.row] if role == Qt.DisplayRole

Indeed, it works!  
So, I will be more careful with the C++ return type now.

> By the way (in case you aren’t already aware of this), qtruby allows you  
> to  
> write code in a more rubysh way (rather than C+±ish). This means you  
> can:
> 
> - use snake\_case instead of camelCase from method names (for example,  
> layout.add\_widget instead of layout.addWidget)
> - use assignment instead of “setter” methods (but only for setter  
> methods  
> taking one argument). For example, self.layout = layout instead of  
> setLayout  
> layout

Yes, I’m aware of this, but since I’m a beginner both with Ruby and Qt,  
I use CamelCase so that I know that is a Qt-method or Qt-attribute.  
Maybe at some point, when I’ll be comfortable enough with Qt, I’ll  
switch to “snake\_case”…

> I hope this helps

Yes, a lot! Thanks once again!

---

<div class="post-metadata">

**Author:** ![bonechi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bonechi](https://www.ruby-forum.com/u/bonechi)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/segmentation-fault-with-qtruby/194426/3 "2010-09-06T16:39:40Z")

</div>

On Sunday 05 September 2010, Arturo Bonechi wrote:

> |Hello everyone!  
> |  
> |Using QtRuby, I tried to implement the “Read-only example model”  
> |(Creating new models chapter) of the “Model/View programming” technology  
> |section of Qt Assistant.  
> |  
> |But when I launch the example, I got this error :  
> |main.rb:5: [BUG] Segmentation fault  
> |ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]  
> |  
> |Abandon  
> |  
> |Here is the code :  
> |# slmodel.rb  
> |class SLModel \< Qt::AbstractListModel  
> | def initialize string\_list, parent = nil  
> | super parent  
> |  
> | @string\_list = string\_list  
> | end  
> |  
> | def rowCount parent = Qt::ModelIndex.new  
> | @string\_list.length  
> | end  
> |  
> | def data index, role  
> | return Qt::Variant.new unless index.isValid  
> | return Qt::Variant.new if index.row \>= @string\_list.length  
> |  
> | return @string\_list[index.row] if role == Qt.DisplayRole  
> |  
> | Qt::Variant.new  
> | end

The problem with your code is that the #data method must always return a  
Qt::Variant. You correctly return an invalid Qt::Variant when the index  
doesn’t correspond to a valid data, but when you pick a value from  
@string\_list, you return the value as it is. From the name of the  
variable  
(and also from the code in the Window’s constructor) it’s clear that the  
contents of this instance variable are strings, not Qt::Variant, so you  
get  
the error. The third line of the data method should be:

return Qt::Variant.new @string\_list[index.row] if role == Qt.DisplayRole

> |# def headerData section, orientation, role = Qt.DisplayRole  
> |# return Qt::Variant.new if role != Qt.DisplayRole  
> |#  
> |# if orientation == Qt.Horizontal  
> |# “Column #{section}”  
> |# else  
> |# “Row #{section}”  
> |# end  
> |# end  
> |end  
> |  
> |# window.rb  
> |require ‘slmodel’  
> |  
> |class Window \< Qt::Widget  
> | def initialize parent = nil  
> | super parent  
> |  
> | @list = %w[Alpha Beta Gamma Omega]  
> | @model = SLModel.new @list, self  
> | @view = Qt::ListView.new  
> |  
> | @view.setModel @model  
> |  
> | layout = Qt::VBoxLayout.new  
> | layout.addWidget @view  
> |  
> | setLayout layout  
> | end  
> |end  
> |  
> |# main.rb  
> |require ‘Qt4’  
> |require ‘window’  
> |  
> |a = Qt::Application.new ARGV  
> |window = Window.new  
> |window.show  
> |a.exec  
> |

By the way (in case you aren’t already aware of this), qtruby allows you  
to  
write code in a more rubysh way (rather than C+±ish). This means you  
can:

- use snake\_case instead of camelCase from method names (for example,  
layout.add\_widget instead of layout.addWidget)
- use assignment instead of “setter” methods (but only for setter  
methods  
taking one argument). For example, self.layout = layout instead of  
setLayout  
layout

I hope this helps

Stefano
