Results 1 to 15 of 15

Thread: New Twist to "Hello World"

  1. #1
    Join Date
    Apr 2009
    Posts
    20
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Red face New Twist to "Hello World"

    Hello All,

    I am an absolute newbie to the worlds of C++ and Qt and have spent the past 2 weeks trying to get to grips on both languages/syntax.

    Simply put, I would like to merge the basic “Hello World” examples of C++ and Qt. In this context, I would like to start the simplest Qt GUI application and when I push the button it prints the "Hello World" message on the Linux/Unix terminal/xterm/console that started it, instead of on the button itself !

    - Is this possible?

    - What’s the simplest way to go about it ?

    So Many Thanks for your valuable support in this request.

    Fassage

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: New Twist to "Hello World"

    when u have a gui application, u can print on console with debug statements..otherwise u can add a label in ur gui application and set text of that label on click of button..go through signal-slot mechanism..as you are a newbie, ur best friend is qt assistant and qt demos..run qtdemo.exe from bin folder of ur Qt directory and go through those examples..u can find their source in demo and examples folder of Qt directory..get started with them and happy Qting

  3. #3
    Join Date
    Apr 2009
    Posts
    20
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: New Twist to "Hello World"

    I am using Qt 3.1.2 and I dont seem to have <Qdebug> or <qdebug.h> !!!

    Was this not available in this version ?

    When i compile it returns :
    qmake -project ; qmake ; make
    g++ -c -pipe -Wall -W -O2 -g -pipe -fno-use-cxa-atexit -fno-exceptions -DQT_NO_DEBUG -I/usr/lib64/qt-3.1/mkspecs/default -I. -I. -I/usr/lib64/qt-3.1/include -o myp.o myp.cpp
    myp.cpp:5:18: Qdebug: No such file or directory
    /usr/lib64/qt-3.1/include/qglobal.h: In function `int main(int, char**)':
    /usr/lib64/qt-3.1/include/qglobal.h:909: too few arguments to function `void
    qDebug(const char*, ...)'
    myp.cpp:29: at this point in file
    myp.cpp:29: void value not ignored as it ought to be
    make: *** [myp.o] Error 1

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: New Twist to "Hello World"

    well, u must be forgetting printf() and why on earth are you still using Qt 3?

  5. #5
    Join Date
    Apr 2009
    Posts
    20
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: New Twist to "Hello World"

    Im using Qt3 at work as part of a Redhat 4 distribution ! that i have no permissions to upgrade

    You will be happy to know Im using Qt4.1 At home.

    )))) RESULT !!!
    I dont know why i thot i needed to include the <Qdebug> ? actually qdebug() and qWarning() are part of the <Qapplication> header.

    It works fine now except i have it in the main rather than in a signal/slot for the button ?

    Talk2amulya can you help me figure this out please ?

    Am i right in thinking i need to build a class object with a slot to handle the button clicked() and that it does the qdebug() inside that class ? If so, Can you show me some code of how this is done please ?

    Many thanks again for all you SO VALUABLE time and support ))

  6. #6
    Join Date
    Mar 2009
    Posts
    25
    Thanked 2 Times in 2 Posts

    Default Re: New Twist to "Hello World"

    Remember the first thing about Qt -- it is written in C++

    So everything works with plain Standard C++ works when you link Qt to your program.

    std::cout
    printf
    puts
    ...

    Also, Qt provides,

    qDebug(), which works like printf(), see your online document for qDenug().
    I advice you to use qDebug() for Qt based applications.


    On the other hand,
    I don't think it's good in the long term you start using fancy high level frameworks like Qt or Microsoft .NET before you have a solid understanding and practice with plain Standard C++ programming.

    Edit:
    Don't get discouraged. I mean don't get lost in the ocean before one can swim well. But sometimes it's just life
    Just don't ignore the basis when you use Qt.

  7. #7
    Join Date
    Apr 2009
    Posts
    20
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: New Twist to "Hello World"

    I am attacking my application on 2 fronts by trying to learn C++ and Qt in parrallel !
    Some good advice which i will work on ! But Like in many ways, i throw myself in at the deep-end and try my best not to drown.

    I do appreciate your help and comments and Im still not sure how to build the button signal/slot to do this when its clicked ???

    All i want is the simplest GUI, such that when i click the button instead of quiting it uses qDebug() or Cout<< rediret to print to the console/terminal !

    Here is my code:


    #include <qapplication.h>
    #include <qpushbutton.h>
    int main( int argc, char **argv )
    {
    QApplication mya( argc, argv );

    QPushButton myb1( "PushMe", 0 );
    QObject::connect( &myb1, SIGNAL(clicked()), &mya, SLOT( quit() ));

    mya.setMainWidget( &myb1 );
    myb1.show();
    return mya.exec();
    }

  8. #8
    Join Date
    Mar 2009
    Posts
    25
    Thanked 2 Times in 2 Posts

    Default Re: New Twist to "Hello World"

    Actually, everything is within a few clicks from the main page of online help (QtAssistant)

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. MainWindow(QWidget *parent = 0)
    9. {
    10. QPushButton *btn = new QPushButton(tr("click me"), this);
    11. connect(btn, SIGNAL(clicked()), this, SLOT(clickMe()));
    12. }
    13. ~MainWindow() {}
    14.  
    15. private slots:
    16. void clickMe() { qDebug("How dare you?"); }
    17. };
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: New Twist to "Hello World"

    here is how u can do without having to derive from QMainWindow:

    Qt Code:
    1. #include <QObject>
    2. #include <QtGui>
    3. class Check : public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. public slots:
    8. void onClicked(){qDebug("Who's your daddy!");};
    9. };
    10.  
    11. #include "main.moc"
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QApplication a(argc, argv);
    16. Check *check = new Check();
    17. QMainWindow *mainWindow = new QMainWindow();
    18. QPushButton *btn = new QPushButton(mainWindow);
    19.  
    20. QObject::connect(btn, SIGNAL(clicked()),check, SLOT(onClicked()));
    21. mainWindow->show();
    22. return a.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Apr 2009
    Posts
    20
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: New Twist to "Hello World"

    Goodmorning !

    Im sorry, but Im still trying to get this to compile !

    I am using Qt3.1.2 and gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-59) and modified
    'talk2amulya' code above to this:

    #include <qapplication.h>
    #include <qpushbutton.h>
    #include <qmainwindow.h>
    #include <qobject.h>

    class Check : public QObject
    {
    Q_OBJECT

    public slots:
    void onClicked(){qDebug("Who's your daddy!");};
    };

    // #include "main.moc"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Check *check = new Check();
    QMainWindow *mainWindow = new QMainWindow();
    QPushButton *btn = new QPushButton(mainWindow);

    QObject::connect(btn, SIGNAL(clicked()),check, SLOT(onClicked()));
    mainWindow->show();
    return a.exec();
    }

    and i keep getting this vtable error !!!!

    qmake -project ; qmake ; make
    g++ -o my3 my3.o -L/usr/lib64/qt-3.1/lib -L/usr/X11R6/lib64 -lqt-mt -lXext -lX11 -lm
    my3.o(.text+0x44): In function `main':
    /home/rowan/users/awqati/MYFILES/myQT/my3/my3.cpp:19: undefined reference to `vtable for Check'
    collect2: ld returned 1 exit status
    make: *** [my3] Error 1

    PLEASE HELP !!

  11. #11
    Join Date
    Apr 2009
    Posts
    20
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: New Twist to "Hello World"

    Is it because i commented out the #include "main.moc" ???

  12. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: New Twist to "Hello World"

    yes, uncommect that line.
    PS. please, use tags [ CODE ] & [ /CODE ] (spaces must be removed in this tags.)
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  13. #13
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: New Twist to "Hello World"

    and please make sure you are rebuilding the whole application

  14. #14
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: New Twist to "Hello World"

    and to your first problem, you just wanted to include <Qdebug> and there's no such file.
    In Qt when you have for example class QTextEdit, the header is <qtextedit.h> (no capital letters) or <QTextEdit> (this one I prefer because it's spelled exactly like the class name).
    QDebug is a exception here, cause working heders are <QDebug> <QtDebug> <qdebug.h>.
    I dont know how it's in Qt3 but if you want to use
    Qt Code:
    1. qDebug() << "asd";
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. qDebug("asd");
    To copy to clipboard, switch view to plain text mode 
    you have to include one of the QDebug headers.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  15. #15
    Join Date
    Apr 2009
    Posts
    20
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: New Twist to "Hello World"

    after a bit more debugging .......... IT WORKS !!!!!!!!!!

    FINALLY ..........

    Ok here is the solution for all those who are interested !

    You must split the class into a header file and include it from the .cpp main !

    1. hello.h

    #include <qapplication.h>
    #include <qobject.h>

    class Hello : public QObject
    {
    Q_OBJECT

    public slots:
    void printDebug() { qDebug("Button clicked!"); }

    };


    2. hello.cpp

    #include <qapplication.h>
    #include <qpushbutton.h>
    #include "hello.h"

    int main(int argc, char **argv)
    {
    QApplication mya(argc, argv);
    Hello hello;
    QPushButton myb1("PushMe", 0);

    QObject::connect(&myb1, SIGNAL(clicked()), &hello, SLOT( printDebug() ));
    mya.setMainWidget(&myb1);
    myb1.show();
    return mya.exec();
    }


    A big BIG MASSIVE THANK YOU TO talk2amulya & Hiker Hauk, spirit and faldżip for your patience, valuable time and support.


    I really appreciate it and hope to get upto speed and start contributing to this site.

Similar Threads

  1. Setting up qmake to run "Hello World"
    By Rolsch in forum Newbie
    Replies: 2
    Last Post: 27th May 2006, 02:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.