ryanmelt / qtbindings

An easy to install gem version of the Ruby bindings to Qt
http://github.com/ryanmelt/qtbindings
Other
340 stars 63 forks source link

Overloading operators of Qt classes? #148

Closed maca89 closed 6 years ago

maca89 commented 7 years ago

In Qt there is QTableWidgetItem::operator< method that is called by QTableWidget when sorting items. Is there any way how to overload this method in Ruby? I am trying this with no luck.

class MyTableItem < Qt::TableWidgetItem
  def <(other)
    p "This is never called!"
  end
end

table = Qt::TableWidget.new
item = MyTableItem.new("foo")
table.set_item(0, 0, item)
table.sort_items(0)

I tried to look into sources of qtbindings and qtruby, but it seems to me there is no operator overloading done anywhere. Apart from custom operators for Points etc. But the smoke itself seems to be somehow loading operator methods...

    <functions>
        <!-- include functions starting with 'q' -->
        <name>^q.*</name>
        <name>.*::q.*</name>
        <!-- and operators -->
        <name>.*operator.*</name>
    </functions>

Maybe I could implement it and post a PR with some guidance. But is it even possible?

jmthomas commented 7 years ago

I think the problem is that you're trying to overload a basic operator in the C code with a Ruby method which obviously doesn't work. I think you'll just have to reimplement the TableWidget sort_items method to do what you want. I believe this method uses the TableView sortByColumn method to do the work. You might want to read up on how QT splits the Model and View code.

maca89 commented 7 years ago

I see. I thought I already was overloading C++ methods with Ruby code, eg. when extending from Qt::AbstractItemDelegate and overloading createEditor method, isn't it actually kind of overloading C++ method? Which is called from deep inside of C++ Qt code? Or is this call handled somewhere manually in bindings layer?

In such case I see no problem in writing manual call of operator< where I would call the Ruby code. And otherwise – if it is overloading C++ code automatically – I see no problem in overloading operator as long as it's virtual. Because an operator is virtual method as any other, just with a funny name in the virtual table. But maybe it's just not possible for operators in Smoke or I got it all wrong. :-)

ghost commented 6 years ago

I don't think overloading operators is possible. sorry.