Martin Sieburg
Martin Sieburg

Reputation: 97

Qt, C++, QTcpServer, My QTcpServer keeps crashing whever I send it a QString from a QTcpSocket program

I have built a QTcpServer program that gets an XML-string sent to it by a QTcpSocket program.
I can confirm that the connection between these two programs can be established as before I wrote the script to convert the XML-string back into normal data, I could (with a few changes to the layout) display the same XML-string on the server program.

This leads me to believe that my error lies somewhere in the QDom parser, or the updateRow function.

Below is the code for the server:

//mainwindow.h

#include <QMainWindow>
#include <QTableView>
#include <QPushButton>
#include <QTextEdit>
#include <QStandardItemModel>
#include <QTcpServer>
#include <QTcpSocket>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void startServer();
    void handleConnection();
    void readMessage();
    void ReadXML(QString XMLString);
    void Add_to_CV(QString C, QString Con, int Pn, int Bh, int Bb, int Bl, int Bw);
    void Add_to_CV(QString C, QString Con, int Pn, int Ch, int Cd, int Cw);
public slots:
    void updateRow(QStandardItem *item);
private:
    QPushButton *SServer;
    QTableView *ContainerView;
    int Pallet_Number;
    QStandardItemModel *HMod;
    QTcpServer *tcpServer;
    QTcpSocket *tcpSocket;
};

#include "mainwindow.h"
#include <QLabel>
#include <QLayout>
#include <QDebug>
#include <QDomDocument>
#include <QRegularExpression>

static QRegularExpression Box_Code("B");
static QRegularExpression Cylinder_Code("C");

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
      SServer(new QPushButton("Start Listening")),
      ContainerView(new QTableView),
      tcpServer(new QTcpServer),
      tcpSocket(new QTcpSocket)
{
    QStringList HeaderRow;
    HeaderRow.append("Pallet");
    HeaderRow.append("Container");
    HeaderRow.append("Code");
    HeaderRow.append("Height");
    HeaderRow.append("Breadth/Diameter");
    HeaderRow.append("Length");
    HeaderRow.append("Weight");
    HMod = new QStandardItemModel;
    HMod->setHorizontalHeaderLabels(HeaderRow);
    resize(800,300);
    connect(SServer, &QPushButton::clicked, this, &MainWindow::startServer);
    connect(tcpServer, &QTcpServer::newConnection, this, &MainWindow::handleConnection);
    connect(tcpSocket, &QTcpSocket::readyRead, this, &MainWindow::handleConnection);
    connect(HMod, SIGNAL(itemchanged(QStandardItem*)), this, SLOT(updateRow(QStandardItem*)));
    QLabel *Lab1(new QLabel("Listening on Port 6164"));
    QHBoxLayout *HB(new QHBoxLayout);
    QVBoxLayout *VB(new QVBoxLayout);
    HB->addWidget(SServer);
    HB->addWidget(Lab1);
    VB->addItem(HB);
    VB->addWidget(ContainerView);
    QWidget *window(new QWidget);
    window->setLayout(VB);
    setCentralWidget(window);
}

MainWindow::~MainWindow()
{
}

void MainWindow::startServer()
{
    if(!tcpServer->listen(QHostAddress::LocalHost, 6164)){
        qDebug() << "Error connecting to Server";

        tcpServer->close();
        return;
    } else {
        qDebug() << "Started Successfully";
    }
}

void MainWindow::handleConnection()
{
    tcpSocket = tcpServer->nextPendingConnection();
    qDebug() << "New Connection!";
    connect(tcpSocket, &QTcpSocket::readyRead, this, &MainWindow::readMessage);
}

void MainWindow::readMessage()
{
    ContainerView->clearSpans(); //clear table to prepare for new XMLString
    QByteArray buffer = tcpSocket->readAll();
    QString FromContainer = QString::fromUtf8(buffer);
    ReadXML(FromContainer);
}

void MainWindow::ReadXML(QString XMLString)
{
    QDomDocument xRead;
    xRead.setContent(XMLString);
    QDomElement root = xRead.documentElement();
    if(root.tagName() == "Pallets")
    {
        QDomElement Pallet = root.firstChildElement();
        int PNum;
        bool ok;
        PNum = Pallet.attribute("Number").toInt(&ok);
        while(!Pallet.isNull())
        {
            bool ok;
            QDomElement Box_Cyl = Pallet.firstChildElement();
            while(!Box_Cyl.isNull())
            {
                int CHei, CLen, CBre, CWei;
                QString Container, CCode;
                Container = Box_Cyl.tagName();
                if(Box_Cyl.tagName() == "Box")
                {
                    QDomElement Code = Box_Cyl.firstChildElement();
                    CCode = Code.text();
                    QDomElement Height = Code.nextSiblingElement();
                    CHei = Height.text().toInt(&ok);
                    QDomElement Length = Code.nextSiblingElement();
                    CLen = Length.text().toInt(&ok);
                    QDomElement Breadth = Code.nextSiblingElement();
                    CBre = Breadth.text().toInt(&ok);
                    QDomElement Weight = Code.nextSiblingElement();
                    CWei = Weight.text().toInt(&ok);
                    Add_to_CV(CCode, Container, PNum, CHei, CBre, CLen, CWei);
                }else if(Box_Cyl.tagName() == "Cylinder")
                {
                    QDomElement Code = Box_Cyl.firstChildElement();
                    CCode = Code.text();
                    QDomElement Height = Code.nextSiblingElement();
                    CHei = Height.text().toInt(&ok);
                    QDomElement Diameter = Code.nextSiblingElement();
                    CBre = Diameter.text().toInt(&ok);
                    QDomElement Weight = Code.nextSiblingElement();
                    CWei = Weight.text().toInt(&ok);
                    Add_to_CV(CCode, Container, PNum, CHei, CBre, CWei);
                }
                Box_Cyl = Box_Cyl.nextSiblingElement();
            }
        }
        Pallet = Pallet.nextSiblingElement();
    }
}

void MainWindow::Add_to_CV(QString C, QString Con, int Pn, int Bh, int Bb, int Bl, int Bw)
{
    QList<QStandardItem*> row;
    QStandardItem *Pallet_Num_Item = new QStandardItem(Pn);
    QStandardItem *Container_Type = new QStandardItem(Con);
    QStandardItem *Box_Code = new QStandardItem(C);
    QStandardItem *Box_Height = new QStandardItem(Bh);
    QStandardItem *Box_Breadth = new QStandardItem(Bb);
    QStandardItem *Box_Length = new QStandardItem(Bl);
    QStandardItem *Box_Weight = new QStandardItem(Bw);
    row << Pallet_Num_Item << Container_Type << Box_Code << Box_Height << Box_Breadth << Box_Length << Box_Weight;
    HMod->appendRow(row);
}

void MainWindow::Add_to_CV(QString C, QString Con, int Pn, int Ch, int Cd, int Cw)
{
    QList<QStandardItem*> row;
    QStandardItem *Pallet_Num_Item = new QStandardItem(Pn);
    QStandardItem *Container_Type = new QStandardItem(Con);
    QStandardItem *Cylinder_Code = new QStandardItem(C);
    QStandardItem *Cylinder_Height = new QStandardItem(Ch);
    QStandardItem *Cylinder_Diameter = new QStandardItem(Cd);
    QStandardItem *Cylinder_Blank = new QStandardItem(); //Cylinder does not have a Length
    QStandardItem *Cylinder_Weight = new QStandardItem(Cw);
    row <<Pallet_Num_Item << Container_Type << Cylinder_Code << Cylinder_Height << Cylinder_Diameter << Cylinder_Blank << Cylinder_Weight;
    HMod->appendRow(row);
}

void MainWindow::updateRow(QStandardItem *item)
{
    QColor colour = Qt::white;
    int row = item->row();
    if (HMod->item(row, 2)->data(Qt::DisplayRole).toFloat() >= 200)
        colour = Qt::red;
    for (int col=0; col<4; col++)
        HMod->item(row, col)->setBackground(colour);
}

The XML string I am currently trying to send into this program is identical to the string that is shown in the following image:

XML-String

I suspect that the problem lies with my QDom xml parsing. If so, could anyone kindly let me know what I need to do to fix it please.

Upvotes: 0

Views: 73

Answers (0)

Related Questions