Tuesday, July 7, 2009

Transparent QtWebView #2

(continued from phosphorescence: Transparent QtWebView #1)
This entry had rewritten in 2009/08/03

The biggest reason of this failure is the impedance between Qt that allows overload to Ruby that doesn't allow overload. But in this case, it's 2nd biggest reason: type of Qt class mismatches. A type of Qt::transparent is Qt::GlobalColor, but 2nd argument type of Qt::Palette#setBrush is Qt::Brush. So Qt::GlobalColor object have to be wrapped with Qt::Brush class.

In addition, Qt::Painter#setBrush method is also too. To see the document of QBrush, constructor can take QGradient(superclass of Qt::LinearGradient corrsponding class) and Qt::GlobalColor.

Then I rewrite the previous code like below:
require 'Qt4'
require 'qtwebkit'

class Container < Qt::Widget
  def initialize parent = nil

    @view = Qt::WebView.new self
    pal = @view.palette
    pal.set_brush(Qt::Palette::Base, Qt::Brush.new(Qt::transparent))
    @view.page.palette = pal
    @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

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

    self.resize 320, 480

  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)

  alias :paintEvent :paint_event

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

And run it.

Run successfully. I push this code to my gitorious clone repository, see here.

No comments:

Post a Comment