1

RubyからQTツリービューにテキストを打ち消したツリービューアイテムのテキストを表示する必要があります。QTのドキュメントと多くのコーディングを読んだ後、フォントを太字でレンダリングした場合にのみ、取り消し線もレンダリングされることがわかりました。

結果のツリービューレンダリング。

だから私はどこで間違っているのだろうか?これは、上記の結果を達成するためのコードです。すべての偶数行アイテムに取り消し線を設定していることに注意してください。MandrivaLinuxでRuby1.8.7とQt4.6.2およびqt4ruby4.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
4

1 に答える 1

0

最終的に、私はこの問題を克服するためのハックなトリックを見つけました。列挙型QFont::Weightの説明をもう一度読んだ後に遊んでみました

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

それ以外の

ret_val.weight = Qt::Font.Normal

そして魔法のように通常のテキストが打ち消されて表示されます!

たぶん、この奇妙な振る舞いはQTのバグが原因ですか?

于 2011-04-04T08:07:00.340 に答える