Question

This is part of a basic QtCreator code in C++ for TCP client-server communication. This is supposed to be the last function I need to get working. But to understand how this works and where the errors are, I am trying to show a whole bunch of QMessageBoxes.

When I run it, I get the first one saying "success". But I do not get any of the last three (since if-else, I would expect at least one to show up). So i put a couple more QMSGBoxes in the 'if' statements ending with return, to see if that is where the issue is, but even they do not show up.

Any idea as to why this is hapening?? Thanks in advance.

void DialogLogIn::success()
{

QMessageBox myBox;
myBox.setInformativeText("Sucess.");
myBox.setStandardButtons(QMessageBox::Ok);
myBox.exec();
//return;

qint16 blockSize = 0;
QDataStream in(mysocket);
in.setVersion(13);

    if (blockSize == 0) {
        if (mysocket->bytesAvailable() < (int)sizeof(quint16))
        {
            QMessageBox box;
            box.setInformativeText("return 1.");
            box.setStandardButtons(QMessageBox::Ok);
            box.exec();
            return;
        }

        in >> blockSize;
    }

    if (mysocket->bytesAvailable() < blockSize)
    {
        QMessageBox box;
        box.setInformativeText("return 2");
        box.setStandardButtons(QMessageBox::Ok);
        box.exec();
        return;
    }

    QString result;
    in >> result;

    if ( result == "G" )
    {
        QMessageBox box;
        box.setInformativeText("Password Verified.");
        box.setStandardButtons(QMessageBox::Ok);
        box.exec();
    }
    else if (result == "N")
    {
        QMessageBox box;
        box.setInformativeText("Password Incorrect.");
        box.setStandardButtons(QMessageBox::Ok);
        box.exec();
    }
    else
    {
        QMessageBox box;
        box.setInformativeText("Error.");
        box.setStandardButtons(QMessageBox::Ok);
        box.exec();
    }
}
Was it helpful?

Solution

The below works for me in Qt 4 and 5, Windows and OS X. I've only turned it into a self-contained example.

#include <QApplication>
#include <QMessageBox>
#include <QBuffer>
#include <QScopedPointer>

void success()
{
    QScopedPointer<QBuffer> mysocket(new QBuffer);
    QMessageBox myBox;
    myBox.setInformativeText("Sucess.");
    myBox.setStandardButtons(QMessageBox::Ok);
    myBox.exec();

    mysocket->open(QBuffer::ReadWrite);
    mysocket->write(QByteArray(20, 0));
    mysocket->seek(0);

    qint16 blockSize = 0;
    QDataStream in(mysocket.data());
    in.setVersion(13);

    if (blockSize == 0) {
        if (mysocket->bytesAvailable() < (int)sizeof(quint16))
        {
            QMessageBox box;
            box.setInformativeText("return 1.");
            box.setStandardButtons(QMessageBox::Ok);
            box.exec();
            return;
        }
        in >> blockSize;
    }

    if (mysocket->bytesAvailable() < blockSize)
    {
        QMessageBox box;
        box.setInformativeText("return 2");
        box.setStandardButtons(QMessageBox::Ok);
        box.exec();
        return;
    }

    QString result;
    in >> result;

    if ( result == "G" )
    {
        QMessageBox box;
        box.setInformativeText("Password Verified.");
        box.setStandardButtons(QMessageBox::Ok);
        box.exec();
    }
    else if (result == "N")
    {
        QMessageBox box;
        box.setInformativeText("Password Incorrect.");
        box.setStandardButtons(QMessageBox::Ok);
        box.exec();
    }
    else
    {
        QMessageBox box;
        box.setInformativeText("Error.");
        box.setStandardButtons(QMessageBox::Ok);
        box.exec();
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    success();
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top