1

I am implementing a program in C (also in C++) to sort files using threads, I need to implement a GUI in C++ or C to select the file to sort, without indicating the path through input standard (a equivalent of JFileChooser in Java). What tutorials do you recommend? I was reading about Qt but I'm not very familiar with this IDE. If you have any example would help me a lot.

unwind
  • 391,730
  • 64
  • 469
  • 606
franvergara66
  • 10,524
  • 20
  • 59
  • 101

6 Answers6

9

Very simple with QT:

 void openFile()
  {
    QFileDialog::getOpenFileName( this, tr("Open Document"), QDir::currentPath(), tr("Document files (*.doc *.rtf);;All files (*.*)"), 0, QFileDialog::DontUseNativeDialog );

    QString filename = QFileDialog::getOpenFileName( 
        this, 
        tr("Open Document"), 
        QDir::currentPath(), 
        tr("Document files (*.doc *.rtf);;All files (*.*)") );
    if( !filename.isNull() )
    {
      qDebug( filename.toAscii() );
    }
  }
TheHorse
  • 2,787
  • 1
  • 23
  • 32
3

you don't have to use a full gui with a main loop just for that. windows and unix offers function calls that display the required file dialogs. On windows you would call GetOpenFileName or GetSaveFileName. If you need a cross-platform way to do it, try tiny file dialogs on sourceforge.

wwwreturn
  • 31
  • 1
  • 2
0

You have two options:

  1. If you want to use QT, you should use QFileDialog and set Options flag to QFileDialog::ShowDirsOnly.

  2. If you want to code only for Windows you can use FolderBrowserDialog class which is part of Win32.

Ioan Paul Pirau
  • 2,733
  • 2
  • 23
  • 26
0

Yes you can use QT. You can use the QDir class of QT for browsing folders and QFile class for file handling.

See the QT doccumentaion for examples. It has a lot of them.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

There is also QFileSystemModel, which can be mapped to QTreeView item and will do many things you need with its internal implementation. It is multithreaded, allows sorting and have many other nice features. look for documentation here

Raiv
  • 5,731
  • 1
  • 33
  • 51
-2

If sorting is the only thing your program does and if it is to be used in a unix-y environment, then I suggest making the program a command line tool and create a GUI wrapper in some script language to call that program. It will be simpler to code as well as more useful (you get two tools for the price of half a tool), it will also make it easier to port to other GUI environments as well as make it easier to distribute.

This shellscript opens a file-chooser dialog and run your program (creatively named yoursort) (not tested and I'm not used to writing shell scripts, I prefer other scripting languages, I'm sure other stackoverflow contributors can find a lot of things to improve upon):

    #!/bin/sh

    FILE=`zenity --file-selection --title="Choose file to sort"`

    if [ $? -eq 0 ]; then
        yoursort -f "$FILE"
    fi

Zenity is a command line tool to create simple GUI dialogues, it also includes a progress dialog.

If you want to create a more advanced GUI, use a scripting language like tcl, Python or Ruby.

The Ruby GUI toolkit Shoes is extremely easy to learn, it has a flow based layout engine (kinda like a web-page). You can do most things you can do with QT (which seems to be the favoured in other answers), or at least all the things you need for 99% of your applications. As Shoes is built as layer on GTK it is as portable as GTK and you can extend it with most of the grunt work already laid out for you (i.e. you don't have to deal directly with X servers, font rendering, or other fatiguing stuff like that).


The rest of this post is kind of of-topic, but since someone downgraded me because I suggested using a scripting language, I feel that I have to motivate why coding a GUI in raw C or C++ is almost always a bad idea (unless you do it with the sole purpose of learning how to do it).

Using raw C or C++ to create a GUI is for people that enjoy driving nails through their body, it is especially painful the day you have to change the look of your application or add new functionality. Use some scripting language, either as a builtin scripting language within your application [tcl, lua, guile and pike is designed to be easy to incorporate into your application], or create a GUI wrapper around a command line tool that you create.

There is also a lot of GUI toolkits (e.g. GTK, QT) that can load GUI:s from some kind of GUI description files, like an XML file. Those files are a hell of a lot easier to write and modify (or create and modify with a GUI builder) then writing and modifying raw C or C++.

If speed is important and as a last resort, use some high level language to generate C or C++ code for your GUI, it is way more flexible to changes then writng raw code. But if you do this you still have to do most of the painful grunt work for the first version of the GUI, as you would if you would have written it in raw C or C++. Langauges like TCL, M4 and Ruby excel at this kind of meta programming. Another last resort is to write your own GUI loader, then you can make the syntax of the GUI description file more specialist and that will make it faster than the GUI loaders that come ready-made with GUI toolkits (and sometime provide UI widgets the ready-made GUI loaders don't support) and, again, this will give you more flexibility to change your GUI then coding them in raw C or C++.

PS. QT use a preprocessor to ease the pain of GUI programming in C++, but it is still very low level and still unnecessary painful to use.

mja
  • 262
  • 3
  • 8