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. GUI won't show after I initialize a Class (QObject)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
qobject
10 Posts 4 Posters 3.2k Views 2 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.
  • Basti46B Offline
    Basti46B Offline
    Basti46
    wrote on last edited by
    #1

    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)

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Carmoneer
      wrote on last edited by
      #2

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

      Could it be as simple as:

      serial(new SerialPort())
      

      ..by adding the parentheses?

      Basti46B kshegunovK 2 Replies Last reply
      0
      • C Carmoneer

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

        Could it be as simple as:

        serial(new SerialPort())
        

        ..by adding the parentheses?

        Basti46B Offline
        Basti46B Offline
        Basti46
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • C Carmoneer

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

          Could it be as simple as:

          serial(new SerialPort())
          

          ..by adding the parentheses?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @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.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            What does the constructor of your SerialPort class look like ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • Basti46B Offline
              Basti46B Offline
              Basti46
              wrote on last edited by
              #6

              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:(

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                kshegunovK Basti46B 2 Replies Last reply
                2
                • SGaistS SGaist

                  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.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @SGaist
                  Nice catch, I missed it. :)

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    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.

                    Basti46B Offline
                    Basti46B Offline
                    Basti46
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

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