2014-01-17 51 views
1

GTK使用cairo进行绘制。所以我试图创建一个hello world应用程序,它写入图像(svg,png,...)而不是X11。我遇到了两个问题: - 图像为空 - 启动时没有运行X11(这是实际的目标),我得到错误“**(a.out:9021):警告**:无法打开X显示“试图渲染GTK +窗口到图像

代码是草稿!

#include <string> 
#include <iostream> 
#include <thread> 
#include <chrono> 
#include <cmath> 

#include <cairo.h> 
#include <cairommconfig.h> 
#include <cairomm/context.h> 
#include <cairomm/surface.h> 
#include <gtk/gtk.h> 
#include <gdk-pixbuf/gdk-pixbuf.h> 

int main(int argc, char *argv[]) 
{ 

    gtk_init(&argc, &argv); 
    GtkWidget *window; 
    GtkWidget *button; 
    // GtkWidget *main_window = gtk_initialize(); 

    window = gtk_offscreen_window_new(); 

    button = gtk_button_new_with_label ("Hello World"); 
    gtk_container_add (GTK_CONTAINER (window), button); 
    gtk_widget_show (window); 
    GdkWindow *gdk_window = gtk_widget_get_window(GTK_WIDGET(window)); 
    std::cout << "gdk window: " << gdk_window << std::endl; 
    cairo_surface_t * surfp = gdk_offscreen_window_get_surface(gdk_window); 
    std::cout << "Created Window will now draw to png" << std::endl; 

    std::string filename = "image.svg"; 
    double width = 600; 
    double height = 400; 

    Cairo::SvgSurface srfobj(surfp); 

    Cairo::RefPtr<Cairo::SvgSurface> refptr(&srfobj); 

    Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(refptr); 

    cr->save(); // save the state of the context 
    cr->show_page(); 

    std::cout << "Wrote SVG file \"" << filename << "\"" << std::endl; 
    std::chrono::milliseconds dura(200); 
    std::this_thread::sleep_for(dura); 

    return 0; 
} 
  • 为什么不工作的代码?
  • 我可以在没有运行X11的情况下运行gtk应用程序,还是应该忽略警告?
+1

可能相关:http://stackoverflow.com/questions/4944441/how-to-draw-any-gtk-widget-on-top-of-cairo-surface – drahnr

回答

1

答案到这两个问题是,你不能没有某种形式输出的运行GTK +应用程序。您正在使用需要XServer的gtk-x11。你可能对DirectFB后端有一些好运,但我不会屏住呼吸,因为我不知道它是否已经被保存了。

由于Gtk在没有XServer的情况下运行,因此生成的映像为空。