Any tips for making a better looking GUI-free program?

Discussion in 'Mac Programming' started by adammcd, Aug 6, 2009.

  1. adammcd macrumors member

    Joined:
    Nov 15, 2007
    #1
    *warning: I am a beginner programmer*

    Here is the deal. I am creating a C++ program using Xcode's 'Command-Line Utility'. I am deciding *not* to make a GUI for two reasons:

    (1) I only know C++. So, unless I want go the Carbon route (which I've heard might not be a good idea), I would have to learn another language (Objective C/Cocoa, Python, etc.)

    (2) It will be more portable without a GUI. If I do learn Cocoa and make a GUI, it only runs on Macs! On the other hand, I should be able to compile my C++ code on a Windows or Linux machine (right?) to get executables that work on non-Macs as well.


    My program looks OK as is, but I am wondering what tips & tricks there might be for making my command-line program look nicer when it runs in the terminal.

    Right now it is all black & white, and there are 3 text fields boxed off by 'boxes' consisting of characters such as '|' and '-'. I am using a fixed-width font, and am assuming the user's terminal will be using one as well (otherwise things will not look right -- I hope this isn't a problem!).

    Any suggestions here or links to places where I could find out more info (I didn't have much luck searching...)?

    Thanks!

    [for example, is there a way for me to add color, or make better looking boxes (using longer dashes than '-', or L-shaped characters for corners, etc.)? And can I be certain that this will look the same in everyone's terminal window? I'm assuming almost everyone uses a fixed width font in their terminal, but what else can I assume? ...blah blah blah... I'll shut-up now]
     
  2. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #2
    I just thought I'd let you know that you don't have to use objective-C with cocoa, or Carbon/python/whatever, in order to make a GUI program on OSX. There are other ways to make GUI programs that will not only work with OSX but on Windows and Linux as well with little or no porting effort required. You can also stick to using C++ while doing this.

    I like to use the Qt toolkit/framework when I create gui applications. Actually, I pretty much use it for everything these days as it is a pretty extensive framework. It is a framework, that is written in and can be used with C++, that allows you to create platform independent programs. Not only is little or no work usually required to port a Qt app from, for example, a Mac to a Windows machine, the program will look and feel like a native application.

    I've worked on several C++/Qt apps and they all work on Windows, Linux and Macs with hardly any platform specific code (most have no platform specific code). There are several popular apps that use it. Google Earth and KDE are 2 examples.

    I use it along with a build system known as "cmake". The cool thing about using that build system is that it can create unix Makfiles, Visual Studio project files or XCode project files for your program. Qt + cmake allows me to easily do most of my development on a Mac even if the program is really targeted for Windows.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
  4. adammcd thread starter macrumors member

    Joined:
    Nov 15, 2007
    #4
    Lee: Someone else also suggested I look at <ncurses.h>

    I'm not sure if I'm too much of a newbie to use ncurses, or what. I've tried to read 3 different "tutorials" about ncurses and cannot figure out what is going on.

    One such tutorial had an example program (I'll give it below). When I try to paste this code into Xcode and run it as a C++ command-line utility I get "24 Fatal Build Errors".

    Here is the code:

    Code:
    #include <stdio.h>
    #include <ncurses.h>
    
    void main()
    {
            WINDOW *vin;
            initscr();
            start_color();
            init_pair(1,COLOR_YELLOW,COLOR_BLUE);
            init_pair(2,COLOR_BLUE,COLOR_YELLOW);
            init_pair(3,COLOR_BLUE,COLOR_WHITE); 
            vin=newwin(12,40,13,0);
            wmove(vin,0,5);
            wprintw(vin,"Hello, World.");
            wbkgd(vin,COLOR_PAIR(1));
            wrefresh(vin);
            getch();
            delwin(vin);
            endwin();
    } 
    Here are the errors I get:

    Code:
    Building target “ncurses_test” of project “ncurses_test” with configuration “Release” — (24 errors)
    	    cd "/Adam's Stuff/C++_stuff/ncurses_test"
        /Developer/usr/bin/g++-4.0 -o "/Adam's Stuff/C++_stuff/ncurses_test/build/ncurses_test.build/Release/ncurses_test.build/Objects-normal/ppc/ncurses_test" "-L/Adam's Stuff/C++_stuff/ncurses_test/build/Release" "-F/Adam's Stuff/C++_stuff/ncurses_test/build/Release" -filelist "/Adam's Stuff/C++_stuff/ncurses_test/build/ncurses_test.build/Release/ncurses_test.build/Objects-normal/ppc/ncurses_test.LinkFileList" -arch ppc -mmacosx-version-min=10.5 -isysroot /Developer/SDKs/MacOSX10.5.sdk
    Undefined symbols:
      "_newwin", referenced from:
          _main in main.o
      "_wbkgd", referenced from:
          _main in main.o
      "_init_pair", referenced from:
          _main in main.o
          _main in main.o
          _main in main.o
      "_endwin", referenced from:
          _main in main.o
      "_wmove", referenced from:
          _main in main.o
      "_initscr", referenced from:
          _main in main.o
      "_wrefresh", referenced from:
          _main in main.o
      "_stdscr", referenced from:
          _stdscr$non_lazy_ptr in main.o
      "_start_color", referenced from:
          _main in main.o
      "_wprintw", referenced from:
          _main in main.o
      "_delwin", referenced from:
          _main in main.o
      "_wgetch", referenced from:
          _main in main.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    		  "_newwin", referenced from:
    		      _main in main.o
    		  "_wbkgd", referenced from:
    		      _main in main.o
    		  "_init_pair", referenced from:
    		      _main in main.o
    		      _main in main.o
    		      _main in main.o
    		  "_endwin", referenced from:
    		      _main in main.o
    		  "_wmove", referenced from:
    		      _main in main.o
    		  "_initscr", referenced from:
    		      _main in main.o
    		  "_wrefresh", referenced from:
    		      _main in main.o
    		  "_stdscr", referenced from:
    		      _stdscr$non_lazy_ptr in main.o
    		  "_start_color", referenced from:
    		      _main in main.o
    		  "_wprintw", referenced from:
    		      _main in main.o
    		  "_delwin", referenced from:
    		      _main in main.o
    		  "_wgetch", referenced from:
    		      _main in main.o
    		ld: symbol(s) not found
    		collect2: ld returned 1 exit status
    	    cd "/Adam's Stuff/C++_stuff/ncurses_test"
        /Developer/usr/bin/g++-4.0 -o "/Adam's Stuff/C++_stuff/ncurses_test/build/ncurses_test.build/Release/ncurses_test.build/Objects-normal/i386/ncurses_test" "-L/Adam's Stuff/C++_stuff/ncurses_test/build/Release" "-F/Adam's Stuff/C++_stuff/ncurses_test/build/Release" -filelist "/Adam's Stuff/C++_stuff/ncurses_test/build/ncurses_test.build/Release/ncurses_test.build/Objects-normal/i386/ncurses_test.LinkFileList" -arch i386 -mmacosx-version-min=10.5 -isysroot /Developer/SDKs/MacOSX10.5.sdk
    Undefined symbols:
      "_newwin", referenced from:
          _main in main.o
      "_wbkgd", referenced from:
          _main in main.o
      "_init_pair", referenced from:
          _main in main.o
          _main in main.o
          _main in main.o
      "_endwin", referenced from:
          _main in main.o
      "_wmove", referenced from:
          _main in main.o
      "_initscr", referenced from:
          _main in main.o
      "_wrefresh", referenced from:
          _main in main.o
      "_stdscr", referenced from:
          _stdscr$non_lazy_ptr in main.o
      "_start_color", referenced from:
          _main in main.o
      "_wprintw", referenced from:
          _main in main.o
      "_delwin", referenced from:
          _main in main.o
      "_wgetch", referenced from:
          _main in main.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    		  "_newwin", referenced from:
    		      _main in main.o
    		  "_wbkgd", referenced from:
    		      _main in main.o
    		  "_init_pair", referenced from:
    		      _main in main.o
    		      _main in main.o
    		      _main in main.o
    		  "_endwin", referenced from:
    		      _main in main.o
    		  "_wmove", referenced from:
    		      _main in main.o
    		  "_initscr", referenced from:
    		      _main in main.o
    		  "_wrefresh", referenced from:
    		      _main in main.o
    		  "_stdscr", referenced from:
    		      _stdscr$non_lazy_ptr in main.o
    		  "_start_color", referenced from:
    		      _main in main.o
    		  "_wprintw", referenced from:
    		      _main in main.o
    		  "_delwin", referenced from:
    		      _main in main.o
    		  "_wgetch", referenced from:
    		      _main in main.o
    		ld: symbol(s) not found
    		collect2: ld returned 1 exit status
    Build failed (24 errors)
    From screenshots that I have seen, ncurses seems like it would be a good way to go. However, after hours of reading up on ncurses, I have no clue how to even get a bare-bones "hello world" file to work. =(

    Thanks for the tip about Qt. I found it myself before posting here by googling C++ and GUI (and cross-platform?). I watched a 9-minute YouTube video about Qt & MacOS-X and found it confusing, so I didn't download it. Perhaps I will download it tonight since you recommend it. However, I anticipate not being able to get it to work -- I just know too little about programming it would seem. =(
     
  5. rossipoo macrumors regular

    Joined:
    Jun 7, 2009
    #5
  6. boast macrumors 65816

    boast

    Joined:
    Nov 12, 2007
    Location:
    Phoenix
    #6
    I barely read the first couple pages of the book "C++ GUI Programming with Qt 4" and I already got a basic idea of how it worked by playing around using QtCreator.

    I still have to go back and read the book, but for basic setups its relatively simple- just drag the buttons and fields into place, and create signals and slots for them.

    I haven't tried GTK+ or wxWidgets, but I think qt4.5 is amazing. (and i was glad to dump xcode for qtcreator. I hate xcode)
     
  7. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #7
    You might want to give it another shot. It's really not that bad and, IMO, no more complicated to use than other toolkit or set of widgets. It's a really good framework.

    Here is a link to a couple of tutorials. They will get you started with how to create windows, buttons, etc.

    Here's a simple hello world app:

    Code:
    #include <QApplication>
    #include <QPushButton>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QPushButton hello("Hello world!");
        hello.resize(100, 30);
    
        hello.show();
        return app.exec();
    }
    
    Compile it by typing the following, in terminal, in the same directory as your source once you have Qt installed:

    Code:
    qmake -project
    qmake
    make
    
    That will create a Mac .app bundle. Qt does come with a nice IDE you can use. You can also use XCode if you want.

    I've always installed Qt by compiling the source code. It's usually a trivial process, however, you can probably download the binaries and just install that.
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    I haven't used ncurses, but i made a minor change so "Hello, World" stays up for a little longer.

    Code:
    #include <stdio.h>
    #include <ncurses.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[])
    {
            WINDOW *vin;
            initscr();
            start_color();
            init_pair(1,COLOR_YELLOW,COLOR_BLUE);
            init_pair(2,COLOR_BLUE,COLOR_YELLOW);
            init_pair(3,COLOR_BLUE,COLOR_WHITE);
            vin=newwin(12,40,13,0);
            wmove(vin,0,5);
            wprintw(vin,"Hello, World.");
            wbkgd(vin,COLOR_PAIR(1));
            wrefresh(vin);
            sleep(5);
            getch();
            delwin(vin);
            endwin();
            return 0;
    }
    
    I saved this as testn.cpp, and compiled with:
    Code:
    g++ -o testn -lncurses testn.cpp
    I am not as familiar with linking libraries like this in XCode, so maybe someone else can jump in on that.

    -Lee
     
  9. adammcd thread starter macrumors member

    Joined:
    Nov 15, 2007
    #9
    Everyone: Thanks for all your helpful comments.

    In the coming week I will play around with Qt to see if I can figure that out & I will also try to figure out how to 'link' ncurses and get that to work.
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    Just adding -lncurses to the "Other linker flags" option under your targets info (under the Build tab) should get things going in XCode. However, i couldn't get Xcode to run the ncurses code properly in it's tiny terminal that it uses. The executable that is produced works fine in the real terminal, though.

    -Lee
     
  11. adammcd thread starter macrumors member

    Joined:
    Nov 15, 2007
    #11
    Lee: I tried what you said concerning ncurses & Xcode. It sort-of helped... I got different errors instead.

    I also decided to download the (fairly large) Qt and give it a try.

    ... Wow. Qt is *so* nice. I don't think I'll use Xcode ever again. Learning Objective-C/Cocoa through Xcode to make a Mac-only GUI had been an intriguing option for me, but I like Qt's way of making GUIs just as much (meaning I can actually do it), plus Qt uses C++ (which I already know) and Qt's Apps are cross-platform.

    On this last point, how do I make my Qt App so that it works on Windows too (I am pretty sure that I read this is possible)? Do I need to download Qt on a Windows machine & build by App there to get a Windows copy, or can I do it on my Mac? If there is already a page or place explaining this, feel free to point me to it.

    Thanks -- Adam

    PS. In light of how great Qt is working for me, I don't know that I will pursue the ncurses more. Thanks for the ncurses help anyway -- it did force me to learn some tidbits that I didn't know before. :)
     
  12. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #12
    Glad Qt's working out for you. It provides a lot of useful stuff beyond just GUIs (networking, access to sql databases, file access, reading/writing xml files, opengl, etc). Considering that Qt can now be used under the LGPL license for free, and thus be included in commercial/proprietary apps without having to give away your source code if you don't modify the qt libraries, I see little reason for me to use anything else when developing applications.

    The way you would typically create a Windows version of your Qt app would be to install/build Qt on a Windows machine and then build your app on that Windows machine. You can either use a dedicated Windows machine or boot into windows on your mac via bootcamp/parallels/vmware.

    Building Qt apps on Windows can be slightly more involved than on a Mac if you aren't familiar with compiling stuff on windows in general. If you have access to Microsoft Visual Studio, than you can use that (I don't). If that's not an option, then you want to use minGW and possibly MSYS. If you aren't aware, minGW is Windows port of the GNU compiler tools. That will give you access to GCC/G++ on windows for free. From there, you can install the Qt Windows binaries (or build from source if you want). You can then build your app using the Qt IDE or on the command line. Many people who use mingw and prefer a unix command line environment also install MSYS. That gives you a basic unix environment on windows.

    One tip about this: If you install minGW, you'll want to install the latest version from the minGW website. When you go to install Qt for windows, you will download a mingw version. During the install, it will ask if you want to install mingw. Select no. It will then complain that your version is newer than what it supports. Ignore that warning.

    Most of the time, you shouldn't have to modify your app to work on windows, although, no cross-platform framework perfect. There are some cases where you need to have some platform specific code. You'll figure that out as you go.

    If you wanted a linux version of your app, then you do the same thing. Setup a linux box, install qt for linux and build your app.

    It should be possible to compile a windows version of your app directly on your mac but I don't recommend doing it. That's a more advanced topic that involves building cross compilers and what not.

    edit: somewhere on the Qt website are instructions for doing all of this on windows. Using that and the instructions on how to setup minGW (on mingw's website), you should get pretty far.
     
  13. adammcd thread starter macrumors member

    Joined:
    Nov 15, 2007
    #13
    mslide: The info you gave was exactly what I was looking for. Thanks a bunch! I won't dare try to cross-compile. I'll find an actual windows machine (and a linux too) or just use bootcamp on my Mac if I can't.
     

Share This Page