[导读]首先,去MSYS2官网下载MSYS2环境并安装在C:/mysys64下,我安装的是64位的。
首先,去MSYS2官网下载MSYS2环境并安装在C:/mysys64下,我安装的是64位的。
进入MSYS命令行执行:
pacman -S mingw-w64-x86_64-gtk3
pacman -S mingw-w64-x86_64-toolchain base-devel
然后在qt creator 在 "工具->选项->编译器" 中添加c和c++编译器,如下图:
进入Debuggers标签页,设置调试器
进入构建套件标签页,设置如下:
建立一个Empty qmake Project工程:
在工程的*.pro文件内加入头文件和库文件:
INCLUDEPATH += C:/msys64/mingw64/include/gtk-3.0 C:/msys64/mingw64/include/cairo C:/msys64/mingw64/include C:/msys64/mingw64/include/pango-1.0 C:/msys64/mingw64/include/atk-1.0 C:/msys64/mingw64/include/cairo C:/msys64/mingw64/include/pixman-1 C:/msys64/mingw64/include C:/msys64/mingw64/include/freetype2 C:/msys64/mingw64/include/libpng16 C:/msys64/mingw64/include/harfbuzz C:/msys64/mingw64/include/glib-2.0 C:/msys64/mingw64/lib/glib-2.0/include C:/msys64/mingw64/include C:/msys64/mingw64/include/freetype2 C:/msys64/mingw64/include C:/msys64/mingw64/include/harfbuzz C:/msys64/mingw64/include/libpng16 C:/msys64/mingw64/include/gdk-pixbuf-2.0 C:/msys64/mingw64/include/libpng16 C:/msys64/mingw64/include/glib-2.0 C:/msys64/mingw64/lib/glib-2.0/include C:/msys64/mingw64/include
LIBS += -LC:/msys64/mingw64/lib -lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lwinmm -ldwmapi -lsetupapi -lcfgmgr32 -lz -lpangowin32-1.0 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
建立main.cpp文件,输入以下代码:#include
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *label;
//初始化GTK+库
gtk_init(&argc, &argv);
//创建主窗口
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//设置窗口大小
gtk_widget_set_size_request(window,300,300);
//设置标题
gtk_window_set_title(GTK_WINDOW (window), "Hello World");
//当窗口即将被销毁,停止主GTK+循环
g_signal_connect(window, "destroy",G_CALLBACK (gtk_main_quit), NULL);
//创建一个"Hello World"标签
label = gtk_label_new("Hello, World");
//将标签加入到主窗口
gtk_container_add(GTK_CONTAINER (window), label);
//设置窗口中的所有元素可见
gtk_widget_show_all(window);
//开始主循环,让它在那里休息,直到应用程序关闭
gtk_main();
return 0;
}
运行结果: