Saturday, July 4, 2009

Transparent QtWebView #1

When I read Qt Labs Blogs, I found interesting post titled Transparent QWebView (or QWebPage). As reading this, it's easy that adding a few lines makes background of Webkit transparent, and it can make also by Python (via PyQt).

Then I learn example source codes from Qt's gitorius repository and start to try it by QtRuby. I write sample codes in rubyish like below:

require 'Qt4'
require 'qtwebkit'

class Container < Qt::Widget

  def initialize parent = nil
    super

    @view = Qt::WebView.new self
    transparent_palette = palette
    transparent_palette.setBrush(Qt::Palette::Base, Qt::transparent)
    @view.page.palette = transparent_palette
    @view.set_attribute(Qt::WA_OpaquePaintEvent, false)
    @view.load Qt::Url.new('http://en.mobile.wikipedia.org/')
    @view.zoom_factor = 0.8

    Qt::Object.connect(@view, SIGNAL( 'titleChanged(const QString&)' ),
      self, SLOT( 'setWindowTitle(const QString&)' ))

    @layout = Qt::VBoxLayout.new self
    @layout.add_widget @view

    @gradient = Qt::LinearGradient.new
    @gradient.set_color_at(0.0, Qt::Color.new(249, 247, 96))
    @gradient.set_color_at(1.0, Qt::Color.new(235, 203, 32))
    @gradient.set_coordinate_mode(Qt::Gradient::ObjectBoundingMode)

    self.resize 320, 480
  end

  protected
  def paint_event event
    @painter = Qt::Painter.new self
    @painter.fill_rect(event.rect, Qt::transparent)
    @painter.pen = Qt::NoPen
    @painter.brush = @gradient
    @painter.opacity = 0.6
    @painter.draw_rounded_rect(self.rect, 10, 10)
    @painter.end
  end

  alias :paintEvent :paint_event
end

Qt::Application.new(ARGV) do
  Container.new do
    self.set_attribute(Qt::WA_TranslucentBackground, true)
    self.window_flags = Qt::FramelessWindowHint
    show
  end
  exec
end

Let's run it!
> /opt/ruby-1.9.1/bin/ruby transparentweb.rb
transparentweb.rb:10:in `method_missing': undefined method `setBrush' for #<Qt::Palette:0x00000000bd3738> (NoMethodError)
from transparentweb.rb:10:in `initialize'
from transparentweb.rb:45:in `new'
from transparentweb.rb:45:in `block in <main>'
from /opt/ruby-1.9.1/lib/ruby/site_ruby/1.9.1/Qt/qtruby4.rb:2568:in `instance_eval'
from /opt/ruby-1.9.1/lib/ruby/site_ruby/1.9.1/Qt/qtruby4.rb:2568:in `run_initializer_block'
from /opt/ruby-1.9.1/lib/ruby/site_ruby/1.9.1/Qt/qtruby4.rb:431:in `initialize'
from /opt/ruby-1.9.1/lib/ruby/site_ruby/1.9.1/Qt/qtruby4.rb:431:in `initialize'
from transparentweb.rb:44:in `new'
from transparentweb.rb:44:in `<main>'

Oops, undefined method! Really?
> /opt/ruby-1.9.1/bin/rbqtapi -s Qt::Palette | grep setBrush
Qt::Palette#setBrush(Qt::Palette::ColorRole, Qt::Brush)
Qt::Palette#setBrush(Qt::Palette::ColorGroup, Qt::Palette::ColorRole, Qt::Brush)

Qt::Palette#setBrush exists. Umm... to be continued...

No comments:

Post a Comment