views:

75

answers:

1

I'm getting some weirdness with QtRuby when using a TableWidget. The table widget loads, but when you click on the elements in the row, the app segfaults and crashes.

require 'Qt4'



class SimpleModel < Qt::AbstractTableModel

    def rowCount(parent)

        return 1

    end



    def columnCount(parent)

        return 1

    end



    def data(index, role=Qt::DisplayRole)

        return Qt::Variant.new("Really Long String") if index.row == 0 and index.column == 0 and role == Qt::DisplayRole

        return Qt::Variant.new

    end

end



Qt::Application.new(ARGV) do

    Qt::TableWidget.new(1, 1) do

        set_model SimpleModel.new

        show

    end



    exec

end

The backtrace seems to imply that it is bombing in mousePressEvent

#6  0x01624643 in QAbstractItemView::pressed(QModelIndex const&) () from /usr/lib/libQtGui.so.4

#7  0x016306f5 in QAbstractItemView::mousePressEvent(QMouseEvent*) () from /usr/lib/libQtGui.so.4

If I override mousePressEvent and mouseMoveEvent, these kinds of crashes no longer happen. Am I doing something wrong over here, or can I chalk this up as a bug in QtRuby?

I'm on fedora11, with the following packages installed:

QtRuby-4.4.0-1.fc11.i586

ruby-1.8.6.369-1.fc11.i586

These crashes also happen when running the script on Windows.

+1  A: 

You're using a Qt::TableWidget when you should be using a Qt::TableView. The following code fixed the crash for me. In addition to a switch from Qt::TableWidget to Qt::TableView, I also reimplemented the index method, just in case. :)

require 'Qt4'

class SimpleModel < Qt::AbstractTableModel

    def rowCount(parent)
        return 1
    end

    def columnCount(parent)
        return 1
    end

    def data(index, role=Qt::DisplayRole)
        return Qt::Variant.new("Really Long String") if index.row == 0 and index.column == 0 and role == Qt::DisplayRole
        return Qt::Variant.new
    end

    def index(row, column, parent)
        if (row > 0 || column > 0)
            return Qt::ModelIndex.new
        else
            return createIndex(row, column, 128*row*column)
        end
    end 
end

Qt::Application.new(ARGV) do
    Qt::TableView.new do
        set_model SimpleModel.new
        show
    end

    exec
end
Matt Rogers
I realise that QTableView does not crash when used. However, I would like to implement some other QWidget methods such as keypressEvent, etc...Would you recommend just wrapping my TableView in some other widget?Linking to the full backtrace as a comment to the original post
gja
I would not recommend wrapping your table view in another widget. Anything you can do with a TableWidget, you can do with a TableView. A TableView is a widget, so it's you can still handle events the same way. The inheritance chain for QTableView (C++) ultimately ends up with QWidget. QTableWidget actually inherits from QTableView.
Matt Rogers
OK, I'm convinced, that seems to work well.Still wondering why this is crashing, purely from an academic point of view though.Thanks for responding :-)
gja
To me, it's crashing because there is no QTableWidgetItem to wrap the model items in since you set the model directly, rather than using the item based API.
Matt Rogers