PDA

View Full Version : New Twist to "Hello World"



fassage
1st April 2009, 17:59
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

talk2amulya
1st April 2009, 18:05
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 ;)

fassage
2nd April 2009, 10:12
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

talk2amulya
2nd April 2009, 10:18
well, u must be forgetting printf() :) and why on earth are you still using Qt 3?

fassage
2nd April 2009, 10:58
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 :)))

Hiker Hauk
2nd April 2009, 11:13
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 :D
Just don't ignore the basis when you use Qt.

fassage
2nd April 2009, 12:08
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();
}

Hiker Hauk
2nd April 2009, 13:02
Actually, everything is within a few clicks from the main page of online help (QtAssistant) :D



#include <QtGui>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0)
{
QPushButton *btn = new QPushButton(tr("click me"), this);
connect(btn, SIGNAL(clicked()), this, SLOT(clickMe()));
}
~MainWindow() {}

private slots:
void clickMe() { qDebug("How dare you?"); }
};

talk2amulya
2nd April 2009, 14:19
here is how u can do without having to derive from QMainWindow:


#include <QObject>
#include <QtGui>
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();
}

fassage
3rd April 2009, 09:54
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 !!

fassage
3rd April 2009, 09:56
Is it because i commented out the #include "main.moc" ???

spirit
3rd April 2009, 10:02
yes, uncommect that line.
PS. please, use tags [ CODE ] & [ /CODE ] (spaces must be removed in this tags.)

talk2amulya
3rd April 2009, 10:26
and please make sure you are rebuilding the whole application

faldzip
3rd April 2009, 12:14
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
qDebug() << "asd"; instead of
qDebug("asd"); you have to include one of the QDebug headers.

fassage
3rd April 2009, 14:58
after a bit more debugging .......... IT WORKS !!!!!!!!!!

FINALLY .......... :D:D:D

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.