奇怪的树景行为:只有大胆时,只有三振出版文字

发布于 2024-10-28 19:35:15 字数 1774 浏览 1 评论 0原文

我需要将一些树视图项目文本(删除了文本)显示到 Ruby 的 QT 树视图中。 在阅读了一些 QT 文档和大量编码之后,我发现只有在以粗体渲染字体时,才会渲染删除线。

结果树视图渲染。

所以我想知道我哪里做错了? 这是实现上面所示结果的代码。请注意,我为每个偶数行项目设置了删除线。 我在 Mandriva Linux 上使用 Ruby 1.8.7、Qt 4.6.2 和 qt4ruby 4.4.3-6。

require 'Qt4'
require 'date'

class MyStandardItem < Qt::StandardItem     
  def initialize(str = nil)
    super str
  end

  def data(role = Qt::UserRole + 1)
    return super(role) unless role == Qt::FontRole
    ret_val = Qt::Font.new()
    #parameters for "fromString":font family, pointSizeF, pixelSize, QFont::StyleHint, QFont::Weight, QFont::Style, underline, strikeOut, fixedPitch, rawMode
    ret_val.fromString "sans serif,-1,-1,0,0,0,0,0,0,0"
    case role
    when Qt::FontRole
      ret_val.setStrikeOut(true) if (index.row % 2) == 0
      if index.column == 1
    ret_val.weight = Qt::Font.Bold
      else
    ret_val.weight = Qt::Font.Normal
      end
      return Qt::Variant.fromValue(ret_val)
    end
    return ret_val
  end  
end

Qt::Application.new(ARGV) do
  treeview = Qt::TreeView.new do
    model = Qt::StandardItemModel.new self
    head = [MyStandardItem.new "Qt v. #{Qt.version}"]
    head << MyStandardItem.new("Ruby v. #{VERSION}")
    head << MyStandardItem.new("Qt4Ruby v. 4.4.3-6 (Mandriva)")
    model.append_row head
    (1..10).each do |i|
      col0 = MyStandardItem.new 'some text'
      col0.check_state = ((i % 3) == 0)? Qt.Checked : Qt.Unchecked
      col0.checkable = true
      col0.editable= false
      col1 = MyStandardItem.new "line ##{i}"
      col2 = MyStandardItem.new((Date.today + i).strftime '%d/%m/%y')
      model.append_row [col0, col1, col2]
    end
    self.model = model
    show
  end
  exec
end

I need to show some treeview item text striked out text into a QT treeview from Ruby.
After some reading on QT documentation and much coding, I found that only when rendering font in bold, also the strikeout was rendered.

Resulting treeview rendering.

So I wonder, where I'm doing wrong?
This is the code to achive the result shown above. Note as I set strikeout for every even row item.
I'm using Ruby 1.8.7 and Qt 4.6.2 and qt4ruby 4.4.3-6 on Mandriva Linux.

require 'Qt4'
require 'date'

class MyStandardItem < Qt::StandardItem     
  def initialize(str = nil)
    super str
  end

  def data(role = Qt::UserRole + 1)
    return super(role) unless role == Qt::FontRole
    ret_val = Qt::Font.new()
    #parameters for "fromString":font family, pointSizeF, pixelSize, QFont::StyleHint, QFont::Weight, QFont::Style, underline, strikeOut, fixedPitch, rawMode
    ret_val.fromString "sans serif,-1,-1,0,0,0,0,0,0,0"
    case role
    when Qt::FontRole
      ret_val.setStrikeOut(true) if (index.row % 2) == 0
      if index.column == 1
    ret_val.weight = Qt::Font.Bold
      else
    ret_val.weight = Qt::Font.Normal
      end
      return Qt::Variant.fromValue(ret_val)
    end
    return ret_val
  end  
end

Qt::Application.new(ARGV) do
  treeview = Qt::TreeView.new do
    model = Qt::StandardItemModel.new self
    head = [MyStandardItem.new "Qt v. #{Qt.version}"]
    head << MyStandardItem.new("Ruby v. #{VERSION}")
    head << MyStandardItem.new("Qt4Ruby v. 4.4.3-6 (Mandriva)")
    model.append_row head
    (1..10).each do |i|
      col0 = MyStandardItem.new 'some text'
      col0.check_state = ((i % 3) == 0)? Qt.Checked : Qt.Unchecked
      col0.checkable = true
      col0.editable= false
      col1 = MyStandardItem.new "line ##{i}"
      col2 = MyStandardItem.new((Date.today + i).strftime '%d/%m/%y')
      model.append_row [col0, col1, col2]
    end
    self.model = model
    show
  end
  exec
end

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

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

发布评论

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

评论(1

一念一轮回 2024-11-04 19:35:15

最终我找到了一个黑客技巧来解决这个问题。再次阅读 枚举 QFont::Weight 描述后进行尝试尝试设置

ret_val.weight = 51 # Qt::Font.Normal value is 50

而不是

ret_val.weight = Qt::Font.Normal

神奇地正常文本出现删除!

也许这种奇怪的行为是由于 QT 上的错误造成的?

Eventually I find an hackish trick to overcome this problem. Playing around after reading again the enum QFont::Weight description I tried to set

ret_val.weight = 51 # Qt::Font.Normal value is 50

instead of

ret_val.weight = Qt::Font.Normal

and magically the normal text appears striked out!

Maybe this strange behaviour is due to a bug on QT?

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