QT socket question, maybe qt signal/slot

QT socket question, maybe qt signal/slot

Post by anonymou » Wed, 19 Dec 2001 23:43:23



I am trying to create a simple nntp class to try learning some different
things with the QT library.  I figure this would cover sockets, qt
slot/signals, and avoid UI stuff.  However, when I try to use test
program I created here it does not seem to resolve the host, or when I
use the ipaddress it doesn't seem to stay in the connecting state.  If
anyone could give me a pointer or two just to get an idea of what I
might be doing wrong here, that'd be great.

Makefile:
TARGET = nntptest
OBJS = qtrfc977.o moc_qtrfc977.o
MOCS = moc_qtrfc977.cc

CPP = g++
MOC = moc
CPPFLAGS = -Wall

LIBS = -L$(QTDIR)/lib -lqt
INCS = -I$(QTDIR)/include

all: $(MOCS) $(OBJS) $(TARGET)

.cc.o:
        $(CPP) $(CPPFLAGS) $(INCS) -c $<

moc_qtrfc977.cc: qtrfc977.h
        $(MOC) qtrfc977.h -o moc_qtrfc977.cc

qtrfc977.o: qtrfc977.cc qtrfc977.h
        $(CPP) $(CPPFLAGS) $(INCS) -c qtrfc977.cc

# executable    
nntptest: $(OBJS) nntptest.o
        $(CPP) $(CPPFLAGS) $(INCS) $(LIBS) $(OBJS) nntptest.o -o nntptest

.PHONY: clean
clean:
        rm -f $(TARGET) $(TARGET).o $(MOCS) $(OBJS) core

nntptest.cc:
#include <iostream.h>
#include <unistd.h>
#include "qtrfc977.h"

int main (int argc, char **argv)
{
        QString host;
        qt_rfc977 nntp;

        if (argc != 2) {
                cerr << "usage: nntptest <host>" << endl;
                exit (1);
        }
        host = argv[1];

        nntp.connectToHost (host, 119);

        for (;;) {
                switch (nntp.state ()) {
                        case QSocket::Idle:
                                cout << "nntp.state (): Idle\n";
                                exit (0);
                                break;
                        case QSocket::HostLookup:
                                cout << "nntp.state (): HostLookup\n";
                                sleep (1);
                                break;
                        case QSocket::Connecting:
                                cout << "nntp.state (): Connecting\n";
                                sleep (1);
                                break;
                        case QSocket::Connected:
                                cout << "nntp.state (): Connected\n";
                                nntp.close ();
                                break;
                        case QSocket::Closing:
                                cout << "nntp.state (): Closing\n";
                                break;
                        default:
                                cout << "nntp.state (): unknown\n";
                }
        }

}

qtrfc977.h:
#ifndef QT_RFC977_H
#define QT_RFC977_H

#define DEBUG_QT_RFC977 1

#include <qsocket.h>

class qt_rfc977 : public QSocket
{
private:
     Q_OBJECT
public:

     // constructor
     qt_rfc977 (QObject *parent = 0, const char *name = 0);

     // destructor
     ~qt_rfc977 (void);

     // connect to default nntp port number (119) on specified host
     virtual void connectToHost (const QString &host, Q_INT16 port = 119);

protected slots:
     // hostFound () signal handler
     virtual void rfc977hostFound (void);

     // connected () signal handler
     virtual void rfc977connected (void);

     // connectionClosed () signal handler
     virtual void rfc977connectionClosed (void);

     // delayedCloseFinished () signal handler
     virtual void rfc977delayedCloseFinished (void);

     // readyRead () signal handler
     virtual void rfc977readyRead (void);

     // bytesWritten (int) signal handler
     virtual void rfc977bytesWritten (int nbytes);

     // error (int) signal handler
     virtual void rfc977error (int err);

};

#endif // QT_RFC977_H

qtrfc977.cc:
#ifndef QT_RFC977_H
#include "qtrfc977.h"
#endif // QT_RFC977_H

// constructor
qt_rfc977::qt_rfc977 (QObject *parent, const char *name)
         : QSocket (parent, name)
{
#if  DEBUG_QT_RFC977 == 1
        std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977

     // connect signals
     QObject::connect (this, SIGNAL (hostFound ()),
                      this, SLOT (rfc977hostFound ()));
     QObject::connect (this, SIGNAL (connected ()),
                      this, SLOT (rfc977connected ()));
     QObject::connect (this, SIGNAL (connectionClosed ()),
                      this, SLOT (rfc977connectionClosed ()));
     QObject::connect (this, SIGNAL (delayedCloseFinished ()),
                      this, SLOT (rfc977delayedCloseFinished ()));
     QObject::connect (this, SIGNAL (readyRead ()),
                      this, SLOT (rfc977readyRead ()));
     QObject::connect (this, SIGNAL (error (int)),
                      this, SLOT (rfc977error (int)));

}

// destructor
qt_rfc977::~qt_rfc977 (void)
{
#if  DEBUG_QT_RFC977 == 1
        std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977

}

void qt_rfc977::connectToHost (const QString &host, Q_INT16 port = 119)
{
#if  DEBUG_QT_RFC977 == 1
        std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977

     QSocket::connectToHost (host, port);

}

void qt_rfc977::rfc977hostFound (void)
{
#if  DEBUG_QT_RFC977 == 1
        std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977

        return;

}

void qt_rfc977::rfc977connected (void)
{
#if  DEBUG_QT_RFC977 == 1
        std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977

        return;

}

void qt_rfc977::rfc977connectionClosed (void)
{
#if  DEBUG_QT_RFC977 == 1
        std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977

        return;

}

void qt_rfc977::rfc977delayedCloseFinished (void)
{
#if  DEBUG_QT_RFC977 == 1
        std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977

        return;

}

void qt_rfc977::rfc977readyRead (void)
{
#if  DEBUG_QT_RFC977 == 1
        std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977

        return;

}

void qt_rfc977::rfc977bytesWritten (int nbytes)
{
#if  DEBUG_QT_RFC977 == 1
        std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977

        return;

}

void qt_rfc977::rfc977error (int err)
{
#if  DEBUG_QT_RFC977 == 1
        std::cerr << __FILE__ << " " << __LINE__ << " " << __FUNCTION__ << endl;
#endif // DEBUG_QT_RFC977

        return;

}

 
 
 

QT socket question, maybe qt signal/slot

Post by Kevin Kramme » Thu, 20 Dec 2001 18:34:09



> nntptest.cc:
> #include <iostream.h>
> #include <unistd.h>
> #include "qtrfc977.h"

> int main (int argc, char **argv)
> {
> QString host;
> qt_rfc977 nntp;

> if (argc != 2) {
> cerr << "usage: nntptest <host>" << endl;
> exit (1);
> }
> host = argv[1];

> nntp.connectToHost (host, 119);

I am pretty sure you have to construct a QApplication instance and hand
over execution to its exec() method so that the main event loop gets
started, which will handle distribution of signals to slots.

Quote:

> class qt_rfc977 : public QSocket
> {
> private:
>      Q_OBJECT

This is not an error, but it is private anyway :)

hth,
Kevin
--

Student at Graz University of Technology
http://www.sbox.tu-graz.ac.at/home/v/voyager

 
 
 

QT socket question, maybe qt signal/slot

Post by anonymou » Fri, 21 Dec 2001 04:38:08



> I am pretty sure you have to construct a QApplication instance and hand
> over execution to its exec() method so that the main event loop gets
> started, which will handle distribution of signals to slots.

int main (int argc, char **argv)
{
         QApplication app (argc, argv);

         QString host;
         qt_rfc977 nntp;

         if (argc != 2) {
                 cerr << "usage: nntptest <host>" << endl;
                 exit (1);
         }
         host = argv[1];

         nntp.connectToHost (host, 119);

         app.exec ();

Quote:}

This is what I have as the main program (nntptest.cc).  This compiles,
however when I run it via an ssh connection I get the "nntptest: cannot
connect to X server".  I am assuming this is because QApplication
believes it needs to connnect to the X, however I am not using any X
components of Qt.  Is there an option for a non-graphical Qt
QApplication, as I'd like to just use some of the base classes to learn
Qt, instead of using the graphics.

I will not be able to check to see if the adding QApplication will work
until I get home today, where I have X up and running, but I will let
you know if that is the problem.

Quote:>>class qt_rfc977 : public QSocket
>>{
>>private:
>>     Q_OBJECT

 >>
 > This is not an error, but it is private anyway :)

Now what do you mean by this?  In the examples I saw it had this as a
private ``member''.

 
 
 

QT socket question, maybe qt signal/slot

Post by anonymou » Fri, 21 Dec 2001 05:42:36



> int main (int argc, char **argv)
> {
>         QApplication app (argc, argv);

          QApplication app (argc, argv, QApplication::Tty);

Quote:

>         QString host;
>         qt_rfc977 nntp;

>         if (argc != 2) {
>                 cerr << "usage: nntptest <host>" << endl;
>                 exit (1);
>         }
>         host = argv[1];

>         nntp.connectToHost (host, 119);

>         app.exec ();
> }

doh, I figured it out.  The line changed above allows it to not need to
be connected to the X server and allows it to be connected to a
terminal.  Excellent.  Thanks for the help.
 
 
 

QT socket question, maybe qt signal/slot

Post by Kevin Kramme » Fri, 21 Dec 2001 19:35:19



> doh, I figured it out.  The line changed above allows it to not need
> to be connected to the X server and allows it to be connected to a
> terminal.  Excellent.  Thanks for the help.

Ah, didn't know that this was possible.

Thanks too :)

For the private thing:
I meant, you don't have to specify private: at the beginning of a clss
declaration, as the default access modifier is already private.

You only need to declare a section private, if you used proteceted or
public before.

Kevin

--

Student at Graz University of Technology
http://www.sbox.tu-graz.ac.at/home/v/voyager

 
 
 

QT socket question, maybe qt signal/slot

Post by anonymou » Sat, 22 Dec 2001 02:50:39




>>doh, I figured it out.  The line changed above allows it to not need
>>to be connected to the X server and allows it to be connected to a
>>terminal.  Excellent.  Thanks for the help.
> Ah, didn't know that this was possible.
> Thanks too :)

No problem.  :)  I figured I'd search through the documentation on
QApplication, and it turned up pretty quick.  I don't know if I care for
the idea of QApplication being needed.  It reminds me of Glut in many
ways, but hey, I guess I'll get used to it.

Quote:> For the private thing:
> I meant, you don't have to specify private: at the beginning of a clss
> declaration, as the default access modifier is already private.

> You only need to declare a section private, if you used proteceted or
> public before.

Ah, yes, now I see what you mean.  I just prefer kinda to make it
explicit for myself.  It also keeps me from accidently cutting and
pasting it to elseware and hosing myself up.

Well, thanks for your help.

Aaron

 
 
 

1. Qt SIGNAL / SLOT question (different parameter lists)

I must disclose that I am new to GUI programming, but I have chosen Qt
as the toolkit that I will use.

What I want to be able to do is this:

QObject::connect(button1, SIGNAL(clicked()), window1,
SLOT(optionSelection(1)))

The difficulty is that the clicked signal does not have any parameters,
and the optionSeleciton slot expects an integer parameter.

I wish to "hardcode" the value that should be passed to the
optionSelection slot, but this obviously didn't work.

I can think of several work-arounds, but being new to GUI programming in
general (and Qt in particular), I figured I'd ask your
opinions regarding  a "reasonable" solution to my problem.

Many thanks in advance.

-Steve

2. Creating Thinkpad hibernate file (PM_HIBER.BIN) from Linux

3. Qt: Slot question -- updating widgets inside a slot

4. Bash - cat doesn't like for-then loops

5. signals, slots and Qt Designer

6. VPND per linux... aiuto :(

7. qt Signal/Slot-mechanism

8. Linux TPC-C performance aided by kernel features

9. qt signal and slot problem. Please Help!!!

10. QT Signals and Slots

11. QT signals/slots

12. KDEstudio -> checking for Qt... configure: error: Qt (>= 19991109) (libraries) not found.

13. KDE @ QT, complaints about QT not being freeware