使用 TableWidget 时,我对 QtRuby 感到有些奇怪。表格小部件会加载,但是当您单击行中的元素时,应用程序会出现段错误并崩溃。
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
回溯似乎暗示它在 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
如果我重写 mousePressEvent 和 mouseMoveEvent,则不会再发生此类崩溃。我在这里做错了什么吗,或者我可以将其归结为 QtRuby 中的错误吗?
我使用的是 fedora11,安装了以下软件包:
QtRuby-4.4.0-1.fc11.i586 ruby-1.8.6.369-1.fc11.i586
在 Windows 上运行脚本时也会发生这些崩溃。
最佳答案
当您应该使用 Qt::TableView 时,您却使用了 Qt::TableWidget。以下代码为我解决了崩溃问题。除了从 Qt::TableWidget 切换到 Qt::TableView 之外,我还重新实现了 index 方法,以防万一。 :)
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
关于RubyQt 在 QTableWidget 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2461784/