2

I am developing a GUI using Qt for a test UDP server application i wrote in C using the sockets interface available in unix. I aim of the GUI is to display all the messages (perror and fprintf) in either a "text edit box" or "text browser box". Here is my code,

 //-----------------------------Main.Cpp------------------------------------------//

//-----------------------------QT specific headers-------------------------------//
#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
//------------------------------------------------------------------------------------//


int main(int argc, char *argv[])
{
int sock_fd=0;
funct_class funct_class_obj;
QApplication a(argc, argv);
MainWindow w;
sock_fd=funct_class_obj.udp_config();
funct_class_obj.recv_fucnt(sock_fd);

return a.exec();
}
//--------------------------------End of main.cpp------------------------------//

//-----------------------------mainwindow.cpp----------------------------------//

//-----------------------------QT specific headers-----------------------------//
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QLabel>
//-----------------------------------------------------------------------------//


//--------------------------------C program specific headers------------------//
#include <stdio.h>
#include <stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
 //---------------------------------------------------------------------------//


unsigned char message[50];

QString mystr;

int funct_class:: udp_config()
{
     int sock_fd=0;

     if ((sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
        perror("\nError creating socket\n");
         //mystr = "\nError creating socket\n";


     else
     {
        fprintf(stdout, "\nSuccessfully created socket with descriptor:%d\n", sock_fd);
        //mystr = "Successfully created socket";

        struct sockaddr_in sock_addr = {0};
        sock_addr.sin_family = AF_INET;
        sock_addr.sin_port = htons(PORT);
        if (inet_aton("127.0.0.1", &sock_addr.sin_addr) < 0)
        perror("\nError converting destination IP address from dotted decimal to     binary\n");
        //    mystr = "Error converting destination IP address from dotted decimal to binary";

     else
     {
        if (bind(sock_fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0)                         // Bind function call binds the properties assigned above to the socket created earlier
        perror("\nError binding the port and IP address with the socket\n");
        //mystr = "Error binding the port and IP address with the socket";

     else
        fprintf(stdout, "\nBinding port and IP address successful\n");
        //    mystr = "Binding port and IP address successful";

     }
     }

     return sock_fd;
}



void funct_class:: recv_fucnt(int sock_fd)
{
    struct sockaddr_in recv_sockaddr = {0};
    socklen_t recv_sockaddr_len = sizeof(recv_sockaddr);
    int recv_data=0;

    if ((recv_data = recvfrom(sock_fd, message, sizeof(message), 0x00, (struct     sockaddr*)&recv_sockaddr, &recv_sockaddr_len)) <= 0)

        perror("\nError receiving data/no data received\n");
        //mystr = "Error receiving data/no data received";


        else
        fprintf(stdout, "\nSuccessfully received %d bytes of data\n", recv_data);
    fprintf(stdout, "\nData is :%s", message);
           //mystr = "Successfully received data";
    mystr = QString::fromStdString((const char*)message);


}



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->testedit->setText(mystr);
//ui->mytextbrowser->setText((mystr));
}



MainWindow::~MainWindow()
{
delete ui;
}

//-----------------------------End of mainwindow.cpp---------------------------//

//------------------------------------mainwindow.h-----------------------------//

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#define PORT 5030


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();


private:
Ui::MainWindow *ui;
};


class funct_class{

public:
int udp_config();
void recv_fucnt(int sock_fd);

};




 #endif // MAINWINDOW_H


//-------------------------------End of mainwindow.h-----------------------------//

The application is compiling fine, but i am facing problems when i execute it. It just freezes and even if i manage to get it out from freeze, i am not able to display the required string either in the "text edit box" or "text browser box". Please help.

P.S.: I am a newbie to GUI programming and Cpp.

László Papp
  • 51,870
  • 39
  • 111
  • 135

1 Answers1

1

The issue seems to be pretty clear:

ui->testedit->setText(mystr);

This is only called in the constructor and nowhere else. That means, when your mystr is updated, the widget will not be aware of it. It does not work like you seem to expect; it is not a one-time property binding so that the widget will be be notified automatically and transparently when the string changes. You have to do that work on your own.

Beside, you really ought to aim for using QtNetwork in a Qt based client rather than platform specific POSIX API with BSD sockets.

László Papp
  • 51,870
  • 39
  • 111
  • 135