Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Gtk::Widget::get_window() returns a null Glib::RefPtr, since the widget has not been realized just yet.</p> <p>Based on the <a href="http://developer.gnome.org/gtk3/3.2/GtkDrawingArea.html" rel="nofollow">GtkDrawingArea documentation</a>, you need to hook onto the "draw" signal to handle drawing, where your Cairo context is already created and handed to you. Going back to the <a href="http://developer.gnome.org/gtkmm/unstable/classGtk_1_1Widget.html" rel="nofollow">Gtkmm reference</a>, you would use Gtk::Widget::signal_draw() to hook onto that, or you could overload the virtual on_draw() function to handle your drawing.</p> <p>Additionally, you also need to call .show() on each widget, i.e. your DrawingArea and your Window, and call ccontext->stroke() to get the line actually drawn.</p> <p>The result would look something like:</p> <pre><code>#include &lt;gtkmm.h&gt; bool draw (const Cairo::RefPtr&lt;Cairo::Context&gt; &amp;ccontext, Gtk::DrawingArea *drawarea) { Gtk::Allocation allocation = drawarea-&gt;get_allocation(); const int width = allocation.get_width(); const int height = allocation.get_height(); ccontext-&gt;set_source_rgb(1.0, 0.0, 0.0); ccontext-&gt;set_line_width(2.0); ccontext-&gt;move_to(0,0); ccontext-&gt;line_to(width, height); ccontext-&gt;stroke (); return true; } int main(int argc, char * argv[]) { Gtk::Main kit(argc, argv); Gtk::Window window; Gtk::DrawingArea drawarea; drawarea.signal_draw ().connect (sigc::bind (sigc::ptr_fun (&amp;draw), &amp;drawarea)); window.add(drawarea); window.show_all (); Gtk::Main::run(window); return 0; } </code></pre> <p>or alternatively:</p> <pre><code>#include &lt;gtkmm.h&gt; class LineBox : public Gtk::DrawingArea { protected: virtual bool on_draw (const Cairo::RefPtr&lt;Cairo::Context&gt; &amp;ccontext); }; bool LineBox::on_draw (const Cairo::RefPtr&lt;Cairo::Context&gt; &amp;ccontext) { Gtk::Allocation allocation = get_allocation(); const int width = allocation.get_width(); const int height = allocation.get_height(); ccontext-&gt;set_source_rgb(1.0, 0.0, 0.0); ccontext-&gt;set_line_width(2.0); ccontext-&gt;move_to(0,0); ccontext-&gt;line_to(width, height); ccontext-&gt;stroke (); return true; } int main(int argc, char * argv[]) { Gtk::Main kit(argc, argv); Gtk::Window window; LineBox drawarea; window.add(drawarea); window.show_all (); Gtk::Main::run(window); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload