Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Construct a class inside another class exept main.cpp not working
Qt 6.11 is out! See what's new in the release blog

Construct a class inside another class exept main.cpp not working

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    safrano
    wrote on last edited by
    #1

    hi.

    at the moment i have 6 files in my project (the project file and a main.cpp, an experiment.cpp and a tcpserver.cpp and the 2 *.h files belonging to those)

    @#include <QtGui/QApplication>
    #include "klassenexperiment.h"
    #include "tcpserver.h"

    the main.cpp, where nothing else than standard and construction of one tcpserver object

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

    return a.exec&#40;&#41;;
    

    }
    @

    empty default klassenexperiment ui class
    @#ifndef KLASSENEXPERIMENT_H
    #define KLASSENEXPERIMENT_H

    #include <QMainWindow>
    #include <QtDebug>
    #include <QTcpSocket>
    #include <QTcpServer>

    namespace Ui {
    class klassenexperiment;
    }

    class klassenexperiment : public QMainWindow
    {
    Q_OBJECT

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

    public slots:

    private:
    Ui::klassenexperiment *ui;
    };

    #endif // KLASSENEXPERIMENT_H
    @

    and

    @
    #include "klassenexperiment.h"
    #include "ui_klassenexperiment.h"
    #include "tcpserver.h"

    klassenexperiment::klassenexperiment(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::klassenexperiment)
    {
    ui->setupUi(this);

    }

    klassenexperiment::~klassenexperiment()
    {
    delete ui;
    }
    @

    and finally the tcpserver:

    @
    #ifndef TCPSERVER_H
    #define TCPSERVER_H

    #include <QObject>
    #include <QtDebug>
    #include <QTcpSocket>
    #include <QTcpServer>

    class tcpserver : public QObject
    {
    Q_OBJECT
    public:
    explicit tcpserver(QObject *parent = 0);
    QTcpServer *server;
    QTcpSocket *socket;

    signals:

    public slots:
    void newconny();
    };

    #endif // TCPSERVER_H
    @

    and

    @
    #include "tcpserver.h"

    tcpserver::tcpserver(QObject *parent) :
    QObject(parent)
    {
    server = new QTcpServer (this);
    connect(server,SIGNAL(newConnection()),this,SLOT(newconny()));
    server->listen(QHostAddress::Any,123);
    qDebug() << "BORN!";
    }

    void tcpserver::newconny()
    {
    socket = server->nextPendingConnection();
    }
    @

    like this everything works. with my portscanner i see the port on 123 locally opened.

    but now when i want to construct a tcpserver object inside the klassenexperiment object, its not working.
    means, everything seems to work, but in fact, there is no port opened.
    thats the code how i did it (and of course i removed the constructon tcpserver server1; in main.cpp)

    @
    #include "klassenexperiment.h"
    #include "ui_klassenexperiment.h"
    #include "tcpserver.h"

    klassenexperiment::klassenexperiment(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::klassenexperiment)
    {
    tcpserver server2;
    ui->setupUi(this);

    }

    klassenexperiment::~klassenexperiment()
    {
    delete ui;
    }
    @

    port stays closed.

    any idea why?
    is it wrong to include an object like this within an other object?
    how would you do that? constuct the object in main.cpp or in the ui class?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rcari
      wrote on last edited by
      #2

      When you create your tcpserver without using new, it will be freed at the end of the scope. When your constructor returns, the tcpserver does not exist anymore, it has been destructed, poped from the stack. A simple fix: @tcpserver* server2 = new tcpserver(this);@ Passing this ensures it will be properly deleted when the parent class (this) is destructed.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        safrano
        wrote on last edited by
        #3

        thanks, that helped a lot. it works now.

        to the background:
        why has it been destroyed? its an object i created.
        a variable i create also doesn't get destroyed like this.

        to the style:
        if later i have several classes, is it nicer to let them work together in the main.cpp or the ui-class.cpp?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DerManu
          wrote on last edited by
          #4

          [quote author="safrano" date="1346338212"]why has it been destroyed? its an object i created.
          a variable i create also doesn't get destroyed like this.[/quote]

          Of course it does. Everything that is created on the stack is destroyed when leaving the scope. Read up on stack vs heap, e.g. here: http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved