Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to share signals and slots between different classes?

How to share signals and slots between different classes?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 6.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roboterprogrammer
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      roboterprogrammer
      wrote on last edited by
      #2

      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;;
      

      }@

      1 Reply Last reply
      0
      • ZlatomirZ Offline
        ZlatomirZ Offline
        Zlatomir
        wrote on last edited by
        #3

        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 &
        @

        https://forum.qt.io/category/41/romanian

        1 Reply Last reply
        0
        • R Offline
          R Offline
          roboterprogrammer
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • ZlatomirZ Offline
            ZlatomirZ Offline
            Zlatomir
            wrote on last edited by
            #5

            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)

            https://forum.qt.io/category/41/romanian

            1 Reply Last reply
            0
            • R Offline
              R Offline
              roboterprogrammer
              wrote on last edited by
              #6

              ah i solved everything.
              thanks

              Zlatomir

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved