Results 1 to 6 of 6

Thread: QCoreApplication, QTcpSocket and Console input question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QCoreApplication, QTcpSocket and Console input question

    Quote Originally Posted by MichaelB View Post
    My question is: how to enable input from the console and also have socket signals processed correctly?
    Create an object, connect it to readyRead() signal of the device on which QTextStream operates and let it read the input.

  2. The following user says thank you to jacek for this useful post:

    QPlace (20th July 2007)

  3. #2
    Join Date
    Jul 2007
    Posts
    121
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    38
    Thanked 3 Times in 3 Posts

    Default Re: QCoreApplication, QTcpSocket and Console input question

    Quote Originally Posted by jacek View Post
    Create an object, connect it to readyRead() signal of the device on which QTextStream operates and let it read the input.
    I am new to QT and I am sure that I am missing something. Nevertheless, I tried to implement your advise and in doing so I stumbled upon another problem. I created ConsoleInput class:
    class ConsoleInput : public QObject
    {
    Q_OBJECT

    QTextStream ts;
    public:
    ConsoleInput(QObject *parent) : ts(stdin), QObject(parent)
    {
    connect (ts.device(), SIGNAL (readyRead()), this, SLOT(lineIsReady()));
    };
    ~ConsoleInput() {};

    private slots:
    void lineIsReady()
    {
    QString line = ts.readLine();
    };
    };

    my main.cpp is:
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    ConsoleInput cn(&a);
    return a.exec();
    };

    Now I cannot get an input from the console. What am I doing wrong?

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QCoreApplication, QTcpSocket and Console input question

    It seems that QFile doesn't emit any signals, although QIODevice docs suggest it should. I've tried to make it work with QSocketNotifier, but it didn't work well.

    You can make it work by implementing some loop that will check whether there is no data to be read and run QCoreApplication::processEvents() (remember to use non-blocking operations on the file) or using QTimer to check for new characters periodically.

  5. #4
    Join Date
    Jul 2007
    Posts
    121
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    38
    Thanked 3 Times in 3 Posts

    Default Re: QCoreApplication, QTcpSocket and Console input question

    Thank you for looking into this.
    I gave up on the attempts to do it with one event loop from CoreApplication it and did it with two threads - one for console input and another for socket comms.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QCoreApplication, QTcpSocket and Console input question

    Quote Originally Posted by MichaelB View Post
    I gave up on the attempts to do it with one event loop from CoreApplication it and did it with two threads
    Well, that's the easiest solution, but one thread should be enough. I really don't know why QSocketNotifier doesn't work, but unfortunately I don't have time to look into it now.

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
  •  
Qt is a trademark of The Qt Company.