Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to link one project to another



  • Hello!
    Im the newbi
    Please help to solve some problem.
    I have got to working projects each of them with own ui and form.
    I need to build single project wich based on one of them and add another
    But in this case I dont undrstand how to add clases and events which already have link to another ui



  • If you want to link something it needs to be in a library.
    So if you say you have two projects set up in Qt i assume, that they just compile into two stand alone executables.
    The first step to do would be to separate each project into the main function (which will be the executable) and the other stuff you want to reuse which will be the library.
    Then you can setup a third project and link to those libraries.

    If you have set up the projects just for a first draft and don't want to keep them as stand alone projects, then you of course can also just copy all the .cpp, .h, .ui ... files together and make a new project out of it.



  • sorry I didnt understand how I can use another project as library



  • you cannot use another project as library. but you can change the other projects so the parts that you want to reuse are separated and encapsulated in a lib.
    So first question would be: are you using qmake or cmak for the projects?



  • @another_one

    I need to build single project wich based on one of them and add another
    

    refer to qt doc for subdir template for qmake
    https://doc.qt.io/qt-5/qmake-variable-reference.html#subdirs

    sorry I didnt understand how I can use another project as library
    

    https://doc.qt.io/qt-5/qmake-common-projects.html#building-a-library



  • @another_one First - do not introduce yourself as newbie .
    Secondly - take a look at SUB_DIRS concept.
    I like to refer to SUB_DIRS concept as another layer for managing MORE than ONE project.
    That is its primary task , it does it OK IF you have the latest Qt software.
    It is relatively poorly documented and unfortunately there is no real example to "cut and paste". Only bits and pieces of ancient code and pictures of "folder trees" you do not build because that is what SUB_DIRS does for you.

    Please note that "project" is not limited to executable code , but "library" is also "project" in SUB_DIRS concept.

    The SUB_DIRS "upper management " is by Qt application of "make" and done in x.pro files. You need to pay attention to .pro files options , no detailed knowledge of qmake is necessary. But some knowledge of "make" (syntax) is helpful.

    The hardest part in using SUB_DIRS concept is to keep tract how parts interacts - sort-of "who is on first" dilemma.

    But - if SUB_DIRS sounds like the ticket, give it a go.

    You find this forum very helpful resolving issues.

    .



  • Thanks to your support!
    Yet another question, - I trying to test custom Ethrnet device and see how it sends, but cant get data out, can somebody help, please

    #include "mainwindow.h"
    #include <QByteArray>
    #include <QDataStream>
    #include <QDebug>
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        server_status=0;
    }
    
    void MainWindow::on_starting_clicked()
    {
        tcpServer = new QTcpServer(this);
        connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newuser()));
        if (!tcpServer->listen(QHostAddress::Any, 56547) && server_status==0) {
            qDebug() <<  QObject::tr("Unable to start the server: %1.").arg(tcpServer->errorString());
            ui->textinfo->append(tcpServer->errorString());
        } else {
            server_status=1;
            qDebug() << tcpServer->isListening() << "TCPSocket listen on port";
            ui->textinfo->append(QString::fromUtf8("Сервер запущен!"));
            qDebug() << QString::fromUtf8("Сервер запущен!");
        }
        connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(sendMessage()));
    }
    
    void MainWindow::on_stoping_clicked()
    {
        if(server_status==1){
            foreach(int i,SClients.keys()){
                QTextStream os(SClients[i]);
                os.setAutoDetectUnicode(true);
                os << QDateTime::currentDateTime().toString() << "\n";
                SClients[i]->close();
                SClients.remove(i);
            }
            tcpServer->close();
            ui->textinfo->append(QString::fromUtf8("Сервер остановлен!"));
            qDebug() << QString::fromUtf8("Сервер остановлен!");
            server_status=0;
        }
    }
    
    
    void MainWindow::newuser()
    {
        if(server_status==1){
            qDebug() << QString::fromUtf8("У нас новое соединение!");
            ui->textinfo->append(QString::fromUtf8("У нас новое соединение!"));
            QTcpSocket* clientSocket=tcpServer->nextPendingConnection();
            int idusersocs=clientSocket->socketDescriptor();
            SClients[idusersocs]=clientSocket;
            connect(SClients[idusersocs],SIGNAL(readyRead()),this, SLOT(slotReadClient()));
        }
    }
    
    void MainWindow::slotReadClient()
    {
        QTcpSocket* clientSocket = (QTcpSocket*)sender();
        int idusersocs=clientSocket->socketDescriptor();
        QTextStream os(clientSocket);
        os.setAutoDetectUnicode(true);
        os << "HTTP/1.0 200 Ok\r\n"
              "Content-Type: text/html; charset=\"utf-8\"\r\n"
              "\r\n"
              "<h1>Nothing to see here</h1>\n"
              << QDateTime::currentDateTime().toString() << "\n";
        ui->textinfo->append("ReadClient:"+clientSocket->readAll()+"\n\r");
        // Если нужно закрыть сокет
        debug_var = 1;
    qDebug() << debug_var;
       // clientSocket->close();
        SClients.remove(idusersocs);
    }
    
    
    void MainWindow::sendMessage(QString message)
                      {
                        qDebug() << QString::fromUtf8("debug 17");
                        if(debug_var == 1)
                        {
                           QTcpSocket* clientSocket = (QTcpSocket*)sender();
                          QByteArray data;
                          QDataStream dataStream(&data, QTcpSocket::WriteOnly);
                          dataStream.setVersion(QDataStream::Qt_4_0);
    
                          dataStream << (quint16)0;  // placeholder for blocksize
                          dataStream << message;     // append the message
                          dataStream.device()->seek(0);  // go back to beginning
                                  // overwrite placeholder with actual blocksize
                          dataStream << (quint16)(data.size() - sizeof(quint16));
    
                          clientSocket->write(data);
                          qDebug() << QString::fromUtf8("debug 18");
                          //clientSocket->close();
                        }
                      }
    
    void MainWindow::on_pushButton_clicked()
    {
      sendMessage(ui->LE->text());
    }
    

    P.S. I took base from github and trying to mix it to get tcp server write and read
    Thanks to authors



  • Now I have got transfer and receive on TCP-IP but cant understans how transfer data to the client in that function:

    void MainWindow::slotReadClient()
    {
        QString str2 = "1234";
        QTcpSocket* clientSocket = (QTcpSocket*)sender();
        int idusersocs=clientSocket->socketDescriptor();
        QTextStream os(clientSocket);
        os.setAutoDetectUnicode(true);
        /*os << "HTTP/1.0 200 Ok\r\n"
              "Content-Type: text/html; charset=\"utf-8\"\r\n"
              "\r\n"
              "<h1>Nothing to see here</h1>\n"
              << QDateTime::currentDateTime().toString() << "\n";*/
        os << str2;
        ui->textinfo->append("ReadClient:"+clientSocket->readAll()+"\n\r");
        // Если нужно закрыть сокет
        debug_var = 1;
    qDebug() << debug_var;
       // clientSocket->close();
        SClients.remove(idusersocs);
    }
    
    

    Please explain who knows


  • Lifetime Qt Champion

    @another_one said in how to link one project to another:

    but cant understans how transfer data to the client

    clientSocket->write(data);
    

    https://doc.qt.io/qt-5/qiodevice.html#write-2


Log in to reply