How to share signals and slots between different classes?



  • Hi,
    first let me introduce the programm situation:

    The programm connects to an TCPIP Server,
    and it launches its own TCPIP Server, where a client connects to.

    From the connected Server, the programm receives Info-Lines, which are written in a txt-file, this is done in an extra class "tcpclient", and now the missing part is, that always when a new line is there (readyread), the textbrowser of the ui should refresh this data.

    how do i make the connection?

    the ui is initialisiated by

    @Robbie20 w;
    w.show();@

    and i have a loadtextfile method

    @void Robbie20::loadTextFile()
    {
    QDate date = QDate::currentDate();
    QString datum = date.toString("yyyyMMdd");
    QFile inputFile("c://LOGS/" + datum + ".txt");
    inputFile.open(QIODevice::ReadOnly);

    QTextStream in(&inputFile);
    QString line = in.readAll();
    inputFile.close();
    
    ui->textBrowser->setPlainText(line);
    QTextCursor cursor = ui->textBrowser->textCursor();
    cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
    

    }
    @

    so how can i run this method from the class tcpclient, always, when the signal "readyread" occurs, in a way, the browserwindow in the "w" ui will be refreshed?

    thanks for a hint.

    here the tcpclient class:
    @#include "tcpconnect.h"
    #include "robbie20.h"

    #include <QtDebug>
    #include <QTcpSocket>
    #include <QtGui/QApplication>

    tcpconnect::tcpconnect(QObject *parent) :
    QObject(parent)
    {
    }

    void tcpconnect::Test()
    {

    socket = new QTcpSocket(this);
    connect(socket,SIGNAL(connected()), this, SLOT(connected()));
    connect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
    connect(socket,SIGNAL(readyRead()), this, SLOT(readyRead()));
    connect(socket,SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
    
    
    socket->connectToHost("192.168.2.10",2000);
    
            qDebug() << "Verbinde mit Robotersteuerung ....";
    
    if(!socket->waitForConnected(3000))
    {
        qDebug() << "not connected";
    
    }
    

    }

    void tcpconnect::connected()
    {
    qDebug() << "Verbunden";
    }

    void tcpconnect::disconnected()
    {
    qDebug() << "disconnected";
    }

    void tcpconnect::bytesWritten(qint64 bytes)
    {
    qDebug() << "we wrote " << bytes ;
    }

    void tcpconnect::readyRead()
    {
    QDate date = QDate::currentDate();
    QString datum = date.toString("yyyyMMdd");
    QFile inputFile("c://LOGS/" + datum + ".txt");
    inputFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
    QTextStream out(&inputFile);
    out << socket->readAll();
    inputFile.close();

    inputFile.open(QIODevice::ReadOnly);
    QTextStream in(&inputFile);
    QString line = in.readAll();
    inputFile.close();
    }
    @

    thanks.



  • ps:

    i tried to add this line in main.cpp, but its not working

    @
    QObject::connect(st.Test,SIGNAL(readyread()), w, SLOT(w.loadTextFile()));
    @

    this is whole main.cpp

    @#include <QtGui/QApplication>
    #include "robbie20.h"
    #include "tcpconnect.h"
    #include <QObject>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    Robbie20 w;
    w.show();
    
    w.loadTextFile&#40;&#41;;
    
    tcpconnect st;
    st.Test(&#41;;
    
    QObject::connect(st.Test,SIGNAL(readyread()), w, SLOT(w.loadTextFile&#40;&#41;&#41;);
    
    
    return a.exec(&#41;;
    

    }@



  • What's rc180?

    And you need to pass a pointer not an object, so take the address of w
    @
    QObject::connect(validPointerHere,SIGNAL(readyread()), &w, SLOT(w.loadTextFile())); //take the address with operator &
    @



  • hi,

    thanks a lot for the fast reply!

    okay now i can compile like this

    @ QObject::connect(&st,SIGNAL(readyRead()), &w, SLOT(w.loadTextFile()));@

    but i got an error during programm runtime, and its not updating the gui

    @Object::connect: No such signal tcpconnect::readyRead() in ..\Robbie20\main.cpp:19
    Object::connect: (receiver name: 'Robbie20')@

    or more s.th. like
    @ QObject::connect(st.socket->readyRead(),SIGNAL(readyRead()), &w, SLOT(w.loadTextFile()));

    @
    but then i get an error:

    void qiodevice::readyread is protected
    whitin this content



  • Well readyRead seems it's a member function you defined:
    @
    void tcpconnect::readyRead()
    {
    QDate date = QDate::currentDate();
    QString datum = date.toString("yyyyMMdd");
    //...
    @
    And for a signal you don't provide an implementation, you just declare it and the moc will do the rest, read more in the "documentation":http://qt-project.org/doc/qt-4.8/signalsandslots.html

    So maybe that from that function you want to emit a iReadSomeStuff signal (or do some refactoring and rename the method... whatever seems logic in your class design)



  • ah i solved everything.
    thanks

    Zlatomir


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.