Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top