GUI won't show after I initialize a Class (QObject)



  • Hi again guys,
    Sorry if the title is missleading, i'll try to explain my problem! I have a SerialPort Class where i want to receive data (and signals) from my MainWindow class.

    class SerialPort : public QObject
    {
        Q_OBJECT
    
    public:
        explicit SerialPort(QObject *parent = 0);
        ~SerialPort();
    
    SerialPort::SerialPort(QObject *parent)
        : QObject(parent)
    {...
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    namespace Ui {
    class MainWindow;
    }
    class SerialPort;
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void about();
    private:
        Ui::MainWindow* ui;
        SerialPort *serial;
    };
    
    #endif // MAINWINDOW_H
    

    But as soon as I initialize the SerialPort object in my MainWindow, the gui won't show up. I can find the application in my task manager where i can see that it uses a lot of ram and cpu power.

    #include "SerialPort.h"
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow),
        serial(new SerialPort)  //THIS DOES NOT WORK WHY?
    

    I currently solved it by using all the signals in my SerialPort class so i don't have to use the serialport object in my MainWindow. Anyone got an idea? Thanks!
    (I don't know if the words i used are correct so pls tell me if i'm wrong)



  • serial(new SerialPort) //THIS DOES NOT WORK WHY?

    Could it be as simple as:

    serial(new SerialPort())
    

    ..by adding the parentheses?



  • @Carmoneer
    I just tried it but no, it doesn't work :( thank you for the answer though


  • Qt Champions 2016

    @Carmoneer
    Nope, this couldn't be it, because both are syntactically valid ways to create an object (when the constructor doesn't have parameters).

    @Basti46
    You should give us more code. From the looks of things you are initializing the variables fine, but we have no idea how you connect the signals, or how you construct your SerialPort objects.

    PS.
    Because you asked about the terms:
    A class is a compound type - a definition of what an object can do, while an object is a variable of a class.
    You connect signals from one object to slots (or signals) of another, but you define what signals or slots (or functions) the object possesses in the class.

    Kind regards.


  • Lifetime Qt Champion

    Hi,

    What does the constructor of your SerialPort class look like ?



  • Heres my SerialPort.
    Header:

    #ifndef SERIALPORT_H
    #define SERIALPORT_H
    #include <QtSerialPort/QtSerialPort>
    #include "MainWindow.h"
    
    QT_USE_NAMESPACE
    
    QT_BEGIN_NAMESPACE
    
    QT_END_NAMESPACE
    
    class SerialPort : public QObject
    {
        Q_OBJECT
    public:
        explicit SerialPort(QObject *parent = 0);
        ~SerialPort();
    
    signals:
        void serialPortOpened();
        void serialPortClosed();
    public slots:
        void test();
    
    private:
        QSerialPort *serial;
        MainWindow *mainWindow;
    };
    #endif // SERIALPORT_H
    
    

    Source:

    #include "SerialPort.h"
    #include "MainWindow.h"
    #include <QCoreApplication>
    #include <QSerialPort>
    
    QT_USE_NAMESPACE
    
    SerialPort::SerialPort(QObject *parent)
        : QObject(parent)
    {
        serial       = new QSerialPort(this);  //this seems to work
        mainWindow   = new MainWindow;  //this seems to work
    }
    SerialPort::~SerialPort()
    {
        delete serial;
        delete mainWindow;
    }
    void SerialPort::openSerialPort()
    {
              //just a test 
    }
    

    The problem ist not using the signals/slots. As soon as i use the SerialPort (serial(new SerialPort)) in my MainWindow Source the gui won't show up anymore and i don't know why:(


  • Lifetime Qt Champion

    You have a never ending class creation loop.

    Basically: MainWindow creates a new SerialPort which creates a new MainWindow which….. Creates a new SerialPort etc…

    What does MainWindow do inside SerialPort ? That's really not its place.


  • Qt Champions 2016

    @SGaist
    Nice catch, I missed it. :)



  • @SGaist Wow yeah i didn't think of that. I use a signal from the mainwindow to start the SerialPort and then use the SerialPort Class to update UI Elements. E.g. MainWindow Button Press-> signal(openSerialPort) ...if serialPort is Open Disable Button etc...

    Do you know how i can solve this issue?
    Thanks btw.


  • Lifetime Qt Champion

    @kshegunov Thanks :)

    @Basti46 Remove MainWindow from SerialPort, it doesn't belong there. In MainWindow connect all the signals you'll be emitting from SerialPort in order to trigger a GUI update.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.