user2805119
user2805119

Reputation: 243

C++ simple open file dialog in linux

I was wondering if anyone could help me out on implementing a simple file open dialog in C++ in Ubuntu. I am using OpenGL for my GUI, but I would like the user to be able to select a file when the program loads. I have tried gtkmm and wxWidgets but they seem to be too complicated for what I want to do.

Upvotes: 10

Views: 10906

Answers (4)

Lecrapouille
Lecrapouille

Reputation: 131

This project can help you: https://github.com/samhocevar/portable-file-dialogs

It uses the same idea described in these answers but it is architecture agnostic and for Unix it wraps zenity, kdialog ...

Upvotes: 0

Wojciech T. Borkowski
Wojciech T. Borkowski

Reputation: 31

Here you have more complete code with zenity:

const char zenityP[] = "/usr/bin/zenity";
char Call[2048];

sprintf(Call,"%s  --file-selection --modal --title=\"%s\" ", zenityP, "Select file");

FILE *f = popen(Call,"r");
fgets(Bufor, size, f); 

int ret=pclose(f);
if(ret<0) perror("file_name_dialog()");

return ret==0;//return true if all is OK

Upvotes: 0

Vortico
Vortico

Reputation: 2749

I wrote osdialog for this purpose. See osdialog_gtk2.c for an example using GTK+ 2.

Upvotes: 1

Ziming Song
Ziming Song

Reputation: 1346

If you just need to select a file, then launch a separate program to do that. Like @Dummy00001 said in the comment, you can start zenity --file-selection as a child process and read its stdout.

char filename[1024];
FILE *f = popen("zenity --file-selection", "r");
fgets(filename, 1024, f);

Or you can also write your own program to do the task. That way you can customize the UI as you wish.

Upvotes: 9

Related Questions