RubyQt 在 QTableWidget 上崩溃

发布于 2024-08-25 02:22:41 字数 1093 浏览 7 评论 0原文

使用 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 上运行脚本时也会发生这些崩溃。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖阳 2024-09-01 02:22:41

当您应该使用 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文