2

我正在使用 Qt 为我使用 unix 中可用的套接字接口在 C 中编写的测试 UDP 服务器应用程序开发一个 GUI。我的 GUI 目标是在“文本编辑框”或“文本浏览器框”中显示所有消息(perror 和 fprintf)。这是我的代码,

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

该应用程序正在编译,但我在执行它时遇到了问题。它只是冻结了,即使我设法将其从冻结状态中解救出来,我也无法在“文本编辑框”或“文本浏览器框”中显示所需的字符串。请帮忙。

PS:我是GUI编程和Cpp的新手。

4

1 回答 1

1

这个问题似乎很清楚:

ui->testedit->setText(mystr);

这仅在构造函数中调用,在其他任何地方都没有。这意味着,当您mystr更新时,小部件将不会意识到它。它不像您期望的那样工作;它不是一次性的属性绑定,因此当字符串更改时,小部件将自动且透明地收到通知。您必须自己完成这项工作。

此外,您确实应该针对QtNetwork在基于 Qt 的客户端中使用,而不是在带有 BSD 套接字的平台特定 POSIX API 中使用。

于 2014-07-21T06:51:13.480 回答