在Linux下使用C++进行图形界面开发,可以选择多种图形库和框架。以下是一些常用的库和框架:
sudo apt-get install qt5-default
在Fedora系统中,可以使用以下命令安装Qt:sudo dnf install qt5-qtbase-devel
#include <QApplication>
#include <QMainWindow>
#include <QPushButton>
#include <QMessageBox>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QMainWindow window;
QPushButton *button = new QPushButton("Click Me", &window);
button->setGeometry(100, 100, 100, 30);
QObject::connect(button, SIGNAL(clicked()), &window, SLOT(close()));
window.show();
return app.exec();
}
sudo apt-get install libgtkmm-3-dev
在Fedora系统中,可以使用以下命令安装GTKmm:sudo dnf install gtkmm3-devel
#include <gtk/gtk.h>
static void activate(GtkApplication *app, gpointer user_data) {
GtkWidget *window;
GtkWidget *button;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Hello, GTKmm!");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
button = gtk_button_new_with_label("Click Me");
gtk_container_add(GTK_CONTAINER(window), button);
g_signal_connect(button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
}
int main(int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
sudo apt-get update
sudo apt-get install libfltk1.3-dev
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
void button_cb(Fl_Widget* w, void *data) {
printf("Button clicked!\n");
}
int main(int argc, char **argv) {
Fl_Window* window = new Fl_Window(300, 200, "Hello, FLTK");
Fl_Button* button = new Fl_Button(100, 80, 100, 40, "Click Me");
button->callback(button_cb);
window->end();
window->show(argc, argv);
return Fl::run();
}
sudo apt-get update
sudo apt-get install libwxgtk3.0-dev
#include <wx/wx.h>
class MyApp : public wxApp {
public:
virtual bool OnInit() {
MyFrame *frame = new MyFrame("Hello wxWidgets", wxPoint(50, 50), wxSize(450, 340));
frame->Show(true);
return true;
}
};
class MyFrame : public wxFrame {
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size) {
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string shown in status bar for this menu item");
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_EXIT);
wxMenu *menu = new wxMenu;
menu->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string");
menu->AppendMenu(menuFile, "File");
menu->AppendMenu(menuHelp, "Help");
SetMenu(menu);
}
};
enum { ID_Hello = 1 };
bool MyApp::OnInit() {
MyFrame *frame = new MyFrame("Hello wxWidgets", wxPoint(50, 50), wxSize(450, 340));
frame->Show(true);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size) {
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string shown in status bar for this menu item");
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_EXIT);
wxMenu *menu = new wxMenu;
menu->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string");
menu->AppendMenu(menuFile, "File");
menu->AppendMenu(menuHelp, "Help");
SetMenu(menu);
}
这些工具和框架都有详细的文档和示例代码,可以帮助你快速上手并进行图形界面开发。