添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am attempting to change Gtk::Image -derived object by giving it pixbuf, but i cannot figure out how to approach that.

The simple setup can be mimicked as:

#include <gtkmm.h>
#include <iostream>
class MyImage : public Gtk::Image
public:
    void setPixBuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf);
void MyImage::setPixBuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf)
    // How can i override the existing pixbuf here?
void freeImagePixelData(const guint8* data)
    delete[] data;
Glib::RefPtr<Gdk::Pixbuf> generateTestImage()
    guint8 *data = new guint8[40*40*4];
    for(int i=0; i<40*40*4; )
        data[i++] = (guint8)255;    // R
        data[i++] = (guint8)255;    // G
        data[i++] = (guint8)0;      // B
        data[i++] = (guint8)255;    // A
    Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_data(
        data, Gdk::Colorspace::COLORSPACE_RGB, true, 8, 40, 40,  40*4, sigc::ptr_fun(&freeImagePixelData));
    return pixbuf;
int main(int argc, char** argv)
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "com.almost-university.gtkmm.image.pixbuf");
    Gtk::Window window;
    MyImage im1;
    im1.setPixBuf(generateTestImage());
    window.add(im1);
    window.show_all_children();
    app->run(window);
    return 0;

(Please note that this is an oversimplified version of what i am trying to figure out, i do know that i should be using Gtk::manage and not add things directly to the window without another container, this is just a mock-up).

I know that if i were to generate the image using a constructor as so:

    Gtk::Image im2(generateTestImage());
    window.add(im2);

then i would in fact be getting a yellow square.

Somehow i refuse to believe that one can only use pixbuf at the time of object creation. There must be a way to set the image data somehow, and i just cannot find the needed function.

To set Pixbuf in an Gtk::Image you can use Gtk::Image::set(const Glib::RefPtr< Gdk::Pixbuf >& pixbuf) method:

void MyImage::setPixBuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf)
    set(pixbuf);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.