You are not logged in.

1

Thursday, June 23rd 2011, 2:36pm

[solved] Non descriptive error when using QTcpSocket

Hey guys. I am getting a frustrating error message when trying to write a QTcpSocket.

The error is non descriptive and I have seen it appear for many reasons which is a little frustrating:

Source code

1
Collect2: Id returned 1 exit status


Here is the simple program I am trying to compile. If you comment out the line...

Source code

1
tcpSocket = new QTcpSocket(this);

... it will compile and run just fine...

mainWindow.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef MAINWINDOW_H#define MAINWINDOW_H
#include "../QTNetwork/QTcpSocket"

class QTcpSocket;
class QNetworkSession;

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    QTcpSocket *tcpSocket;

private slots:
    void on_button_connect_clicked();
    void on_actionExit_triggered();
};

#endif // MAINWINDOW_H


mainWindow.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "../QTNetwork/QtNetwork"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tcpSocket = NULL;
// Initialise program to start state
ui->actionGreenConnection->setEnabled(false);
ui->actionAmberConnection->setEnabled(false);
}

MainWindow::~MainWindow()
{
if(tcpSocket != NULL){
delete tcpSocket;
tcpSocket = NULL;
}
delete ui;
}

void MainWindow::on_actionExit_triggered()
{
qApp->quit();
}

void MainWindow::on_button_connect_clicked()
{
char char_serverAddress[50];
memset(char_serverAddress, 0, 50);
sprintf(char_serverAddress, "localhost");
QString qstring_serverAddress = char_serverAddress;
tcpSocket = new QTcpSocket(this);
}



main.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
#include 
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}



Any ideas? it's driving me mad. I am using QT creator and was simply using the following as a guide... http://doc.qt.nokia.com/4.7-snapshot/net…client-cpp.html

This post has been edited 5 times, last edit by "aatwo" (Jun 23rd 2011, 3:00pm)


2

Thursday, June 23rd 2011, 2:49pm

I just noticed the compiler ou

I just noticed the compiler output tab if it helps, although the message there seems a bit cryptic also...

Source code

1
2
3
4
5
6
7
mingw32-make[1]: *** [debug\mit-windows-QT-client.exe] Error 1

mingw32-make: *** [debug] Error 2

The process "C:/Qt/2010.05/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project mit-windows-QT-client (target: Desktop)
When executing build step 'Make'

3

Thursday, June 23rd 2011, 2:58pm

Weird. I just edited my .pro file and changed...

Source code

1
QT       += core gui


... to...

Source code

1
QT       += core gui network


and this seemed to work. Out of curiosity I then changed it back again and it still compiled without errors so I think is most likely some kind of bug with QTCreator.