Den 2014-09-02 01:23, Phil Wolff skrev:
Can anybody tell me why this code returns a properly sized Pixbuf with no image data?

When Gtk::OffscreenWindow::get_pixbuf() is called in Test::Test, the OffscreenWindow has not yet been drawn. Try my modified version of your code. When it's started, the window is empty. If you press a mouse button in the window, Test::on_button_press_event() is called, and the label's text is shown.

Kjell

#include <gtkmm.h>
#include <iostream>

class Test : public Gtk::Window
{
public:
    Test ();
private:
    // Override virtual function
    virtual bool on_button_press_event(GdkEventButton* event);

    Gtk::OffscreenWindow m_pw;
    Gtk::Image* m_pi;
};

Test::Test ()
{
    set_size_request ( 160, 90 );
    set_position ( Gtk::WIN_POS_CENTER );
    try {
        // Put something offscreen
        Gtk::Label* pl = new Gtk::Label ( "Test OffscreenWindow" );
        m_pw.add ( *pl );
        m_pw.show_all ();
        // Retrieve it
        Glib::RefPtr<Gdk::Pixbuf > refPixbuf = m_pw.get_pixbuf ();
        // Show pixbuf dimensions
        set_title ( Glib::ustring::format ( refPixbuf->get_width (), " x ",
                refPixbuf->get_height () ) );
        // Show pixbuf contents onscreen
        m_pi = new Gtk::Image ( refPixbuf );
        add ( *m_pi );
        //refPixbuf->save ( "pixbuf.png", "png" );
        //system ( "eog pixbuf.png" );
        show_all_children ();
    }
    catch ( const Glib::FileError& ex ) {
        std::cerr << "File error: " << ex.what () << std::endl;
    }
    catch ( const Gdk::PixbufError& ex ) {
        std::cerr << "Pixbuf error: " << ex.what () << std::endl;
    }
}

bool Test::on_button_press_event(GdkEventButton*)
{
  Glib::RefPtr<Gdk::Pixbuf> refPixbuf = m_pw.get_pixbuf();
  m_pi->set(refPixbuf);
  return false;
}

int main ( int argc, char *argv[] )
{
    Glib::RefPtr<Gtk::Application> refApp = Gtk::Application::create ( argc,
            argv, "org.gtkmm.example" );
    Test window;
    return refApp->run ( window );
}

_______________________________________________
gtkmm-list mailing list
gtkmm-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtkmm-list

Reply via email to