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. Accessing MainWindow object from another class
QtWS25 Last Chance

Accessing MainWindow object from another class

Scheduled Pinned Locked Moved Unsolved General and Desktop
signals & slotswidget
15 Posts 5 Posters 8.8k Views
  • 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.
  • T Offline
    T Offline
    TUStudi
    wrote on 9 Mar 2020, 17:53 last edited by
    #1

    Hey,

    If I want to create a Qt widget application, I have my MainWindow.ui, MainWindow.cpp and MainWindow.cpp. Assuming I have another class called TestClass in which I want to call a slot that is implemented the MainWindow class, how can I access the MainWindow Object, which is normally created in main.cpp? If

    For Example:

    void TestClass::startDeviceDiscovery(){
        discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
        discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
        connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, &MainWindowObject, &MainWindow::deviceDiscovered); //deviceDiscovered is my Slot implemented in MainWindow.cpp
        discoveryAgent->start();
    }
    
    

    So what would be my &MainWindowObject? My main:

    #include "MainWindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    
    }
    

    So if I create the MainWindow Object outside the main function I cannot use it in my TestClass because I cannot include main there. What is the best way to solve my problem?

    J J 2 Replies Last reply 10 Mar 2020, 06:11
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 9 Mar 2020, 18:08 last edited by
      #2

      Pass the TestClass a pointer to your mainwindow.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      T 1 Reply Last reply 9 Mar 2020, 18:18
      1
      • C Christian Ehrlicher
        9 Mar 2020, 18:08

        Pass the TestClass a pointer to your mainwindow.

        T Offline
        T Offline
        TUStudi
        wrote on 9 Mar 2020, 18:18 last edited by
        #3

        @Christian-Ehrlicher Thank you for the fast reply. I am a beginner to C++ and OOP, so the question is how? About the constructor? But I thought when I pass a TestClass Pointer to my MainWindow then I would still not be able to pass the MainWindow Object as a receiver in my connect() function because my TestClass still does not know anything about MainWindow. therefore I assumed that I have to make the MainWindow Object from my main.cpp globally..

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 9 Mar 2020, 18:34 last edited by
          #4

          It's up to you how you pass it to your class - either per ctor or as separate function call.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • T TUStudi
            9 Mar 2020, 17:53

            Hey,

            If I want to create a Qt widget application, I have my MainWindow.ui, MainWindow.cpp and MainWindow.cpp. Assuming I have another class called TestClass in which I want to call a slot that is implemented the MainWindow class, how can I access the MainWindow Object, which is normally created in main.cpp? If

            For Example:

            void TestClass::startDeviceDiscovery(){
                discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
                discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
                connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, &MainWindowObject, &MainWindow::deviceDiscovered); //deviceDiscovered is my Slot implemented in MainWindow.cpp
                discoveryAgent->start();
            }
            
            

            So what would be my &MainWindowObject? My main:

            #include "MainWindow.h"
            #include <QApplication>
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                MainWindow w;
                w.show();
                return a.exec();
            
            }
            

            So if I create the MainWindow Object outside the main function I cannot use it in my TestClass because I cannot include main there. What is the best way to solve my problem?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 10 Mar 2020, 06:11 last edited by
            #5

            @TUStudi For testing I suggest you take a look at: https://doc.qt.io/qt-5/qtest-overview.html
            For testing you do not have to create your main window inside main.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            T 1 Reply Last reply 10 Mar 2020, 08:28
            0
            • Q Offline
              Q Offline
              qt.1234
              wrote on 10 Mar 2020, 06:44 last edited by qt.1234 3 Oct 2020, 06:44
              #6

              Hi, not sure if i got your question right. But you can follow the example below. In TestClass.h you have to first declare the signal testClassSignal(). so whenever the signal is invoked in testClass.cpp, the slot in MainWindow.cpp called testClassSlot will be invoked in MainWindow.cpp. I hope it helps.

              MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui( new Ui:MainWindow)
              {
               TestClass * testClass = new TestClass();
              initConnections();
              }
              
              void MainWindow::initConnections()
              {
              QObject::connect(testClass, SIGNAL(testClassSignal()), this, SLOT(testClassSlot()),Qt::AutoConnection);
              }
              
              void MainWindow::testClassSlot()
              {
              
              }
              
              void TestClass::TestClass()
              {
              
              }
              
              
              1 Reply Last reply
              1
              • T TUStudi
                9 Mar 2020, 17:53

                Hey,

                If I want to create a Qt widget application, I have my MainWindow.ui, MainWindow.cpp and MainWindow.cpp. Assuming I have another class called TestClass in which I want to call a slot that is implemented the MainWindow class, how can I access the MainWindow Object, which is normally created in main.cpp? If

                For Example:

                void TestClass::startDeviceDiscovery(){
                    discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
                    discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
                    connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, &MainWindowObject, &MainWindow::deviceDiscovered); //deviceDiscovered is my Slot implemented in MainWindow.cpp
                    discoveryAgent->start();
                }
                
                

                So what would be my &MainWindowObject? My main:

                #include "MainWindow.h"
                #include <QApplication>
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    MainWindow w;
                    w.show();
                    return a.exec();
                
                }
                

                So if I create the MainWindow Object outside the main function I cannot use it in my TestClass because I cannot include main there. What is the best way to solve my problem?

                J Offline
                J Offline
                JonB
                wrote on 10 Mar 2020, 07:43 last edited by
                #7

                @TUStudi
                Is TestClass just for testing? Otherwise the more usual pattern is not to access MainWindow from other classes, but rather the other way round. Have MainWindow do the connect of the TestClass signal to its own slot.

                1 Reply Last reply
                0
                • J jsulm
                  10 Mar 2020, 06:11

                  @TUStudi For testing I suggest you take a look at: https://doc.qt.io/qt-5/qtest-overview.html
                  For testing you do not have to create your main window inside main.

                  T Offline
                  T Offline
                  TUStudi
                  wrote on 10 Mar 2020, 08:28 last edited by TUStudi 3 Oct 2020, 08:29
                  #8

                  @jsulm said in Accessing MainWindow object from another class:

                  @TUStudi For testing I suggest you take a look at: https://doc.qt.io/qt-5/qtest-overview.html
                  For testing you do not have to create your main window inside main.

                  Thank you, but it is not a class for testing, it is just a andom class name ;)

                  @qt-1234 said in Accessing MainWindow object from another class:

                  Hi, not sure if i got your question right. But you can follow the example below. In TestClass.h you have to first declare the signal testClassSignal(). so whenever the signal is invoked in testClass.cpp, the slot in MainWindow.cpp called testClassSlot will be invoked in MainWindow.cpp. I hope it helps.

                  MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui( new Ui:MainWindow)
                  {
                   TestClass * testClass = new TestClass();
                  initConnections();
                  }
                  
                  void MainWindow::initConnections()
                  {
                  QObject::connect(testClass, SIGNAL(testClassSignal()), this, SLOT(testClassSlot()),Qt::AutoConnection);
                  }
                  
                  void MainWindow::testClassSlot()
                  {
                  
                  }
                  
                  void TestClass::TestClass()
                  {
                  
                  }
                  
                  

                  Thank you tu @qt-1234. In my case, the Signal is not emitted by the Testclass but by an object of &QBluetoothDeviceDiscoveryAgent and the Signal is the &QBluetoothDeviceDiscoveryAgent::deviceDiscovered it is emitted every time a new Bluetooth device is discovered. So that won't work, will it?

                  connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, MainWindowObject, MainWindowSlot);
                  
                  J J Q 3 Replies Last reply 10 Mar 2020, 08:57
                  0
                  • T TUStudi
                    10 Mar 2020, 08:28

                    @jsulm said in Accessing MainWindow object from another class:

                    @TUStudi For testing I suggest you take a look at: https://doc.qt.io/qt-5/qtest-overview.html
                    For testing you do not have to create your main window inside main.

                    Thank you, but it is not a class for testing, it is just a andom class name ;)

                    @qt-1234 said in Accessing MainWindow object from another class:

                    Hi, not sure if i got your question right. But you can follow the example below. In TestClass.h you have to first declare the signal testClassSignal(). so whenever the signal is invoked in testClass.cpp, the slot in MainWindow.cpp called testClassSlot will be invoked in MainWindow.cpp. I hope it helps.

                    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui( new Ui:MainWindow)
                    {
                     TestClass * testClass = new TestClass();
                    initConnections();
                    }
                    
                    void MainWindow::initConnections()
                    {
                    QObject::connect(testClass, SIGNAL(testClassSignal()), this, SLOT(testClassSlot()),Qt::AutoConnection);
                    }
                    
                    void MainWindow::testClassSlot()
                    {
                    
                    }
                    
                    void TestClass::TestClass()
                    {
                    
                    }
                    
                    

                    Thank you tu @qt-1234. In my case, the Signal is not emitted by the Testclass but by an object of &QBluetoothDeviceDiscoveryAgent and the Signal is the &QBluetoothDeviceDiscoveryAgent::deviceDiscovered it is emitted every time a new Bluetooth device is discovered. So that won't work, will it?

                    connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, MainWindowObject, MainWindowSlot);
                    
                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 10 Mar 2020, 08:57 last edited by
                    #9

                    @TUStudi said in Accessing MainWindow object from another class:

                    So that won't work, will it?

                    It should. Did you try?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • T TUStudi
                      10 Mar 2020, 08:28

                      @jsulm said in Accessing MainWindow object from another class:

                      @TUStudi For testing I suggest you take a look at: https://doc.qt.io/qt-5/qtest-overview.html
                      For testing you do not have to create your main window inside main.

                      Thank you, but it is not a class for testing, it is just a andom class name ;)

                      @qt-1234 said in Accessing MainWindow object from another class:

                      Hi, not sure if i got your question right. But you can follow the example below. In TestClass.h you have to first declare the signal testClassSignal(). so whenever the signal is invoked in testClass.cpp, the slot in MainWindow.cpp called testClassSlot will be invoked in MainWindow.cpp. I hope it helps.

                      MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui( new Ui:MainWindow)
                      {
                       TestClass * testClass = new TestClass();
                      initConnections();
                      }
                      
                      void MainWindow::initConnections()
                      {
                      QObject::connect(testClass, SIGNAL(testClassSignal()), this, SLOT(testClassSlot()),Qt::AutoConnection);
                      }
                      
                      void MainWindow::testClassSlot()
                      {
                      
                      }
                      
                      void TestClass::TestClass()
                      {
                      
                      }
                      
                      

                      Thank you tu @qt-1234. In my case, the Signal is not emitted by the Testclass but by an object of &QBluetoothDeviceDiscoveryAgent and the Signal is the &QBluetoothDeviceDiscoveryAgent::deviceDiscovered it is emitted every time a new Bluetooth device is discovered. So that won't work, will it?

                      connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, MainWindowObject, MainWindowSlot);
                      
                      J Offline
                      J Offline
                      JonB
                      wrote on 10 Mar 2020, 09:01 last edited by JonB 3 Oct 2020, 09:10
                      #10

                      @TUStudi
                      Maybe it's just me, but I still wouldn't connect to a MainWindow slot from any other class. EDIT To be clear, I would never #include mainwindow.h into TestClass, so there is no way the latter could access &MainWindow::deviceDiscovered.

                      If you only ever create one instance of discoveryAgent once, make that exportable from TestClass to MainWindow so that the latter can do the connect on that one instance. If you create instances dynamically, invent a signal from TestClass passing the instance of QBluetoothDeviceDiscoveryAgent as an argument upon creation, and have MainWindow pick that up and put its slot on the instance's deviceDiscovered.

                      That's how I would do it. If a Qt expert says I'm wrong, please let me know!

                      @jsulm I'm not sure, but I think the OP is saying he can't do that connect from MainWindow because the instance is newed inside TestClass::startDeviceDiscovery()?

                      T 1 Reply Last reply 10 Mar 2020, 09:30
                      2
                      • J JonB
                        10 Mar 2020, 09:01

                        @TUStudi
                        Maybe it's just me, but I still wouldn't connect to a MainWindow slot from any other class. EDIT To be clear, I would never #include mainwindow.h into TestClass, so there is no way the latter could access &MainWindow::deviceDiscovered.

                        If you only ever create one instance of discoveryAgent once, make that exportable from TestClass to MainWindow so that the latter can do the connect on that one instance. If you create instances dynamically, invent a signal from TestClass passing the instance of QBluetoothDeviceDiscoveryAgent as an argument upon creation, and have MainWindow pick that up and put its slot on the instance's deviceDiscovered.

                        That's how I would do it. If a Qt expert says I'm wrong, please let me know!

                        @jsulm I'm not sure, but I think the OP is saying he can't do that connect from MainWindow because the instance is newed inside TestClass::startDeviceDiscovery()?

                        T Offline
                        T Offline
                        TUStudi
                        wrote on 10 Mar 2020, 09:30 last edited by
                        #11

                        @JonB said in Accessing MainWindow object from another class:

                        @TUStudi
                        Maybe it's just me, but I still wouldn't connect to a MainWindow slot from any other class. EDIT To be clear, I would never #include mainwindow.h into TestClass, so there is no way the latter could access &MainWindow::deviceDiscovered.

                        If you only ever create one instance of discoveryAgent once, make that exportable from TestClass to MainWindow so that the latter can do the connect on that one instance. If you create instances dynamically, invent a signal from TestClass passing the instance of QBluetoothDeviceDiscoveryAgent as an argument upon creation, and have MainWindow pick that up and put its slot on the instance's deviceDiscovered.

                        That's how I would do it. If a Qt expert says I'm wrong, please let me know!

                        @jsulm I'm not sure, but I think the OP is saying he can't do that connect from MainWindow because the instance is newed inside TestClass::startDeviceDiscovery()?

                        Hello JonB,
                        as I wrote in an other Thread, the idea of ​​mine is that I separate the actual logic from the UI. The communication should take place via my controller (my MainWindow.cpp). I.e. the user clicks on the "Scan for Bluetooth devices" button in the UI, this triggers a "button clicked" slot in my MainWindow.cpp. The startDeviceDiscovery method of the "TestClass" (which should contain the logic) is then called in this slot. If devices are then found, my MainWindow is notified by a signal (deviceDiscovered) and this MainWIndow interacts with the UI by entering the devices in the combo box.

                        553828a0-c21f-406e-92d2-7b07d312184e-image.png

                        But I just notice that a user in my other thread replied, which I unfortunately didn't see.

                        Your MainWindow should have a member variable of the TestClass class.
                        Your TestClass class should provide an API that your MainWindow can connect to and also that provides whatever data is needed.

                        So, do you know how to do that? I thought my problem can be solved If I just make my MainWIndow Object, which is created in my main class, globally.
                        But it seems I haven't understood the principle of OOP.

                        J 1 Reply Last reply 10 Mar 2020, 09:34
                        0
                        • T TUStudi
                          10 Mar 2020, 09:30

                          @JonB said in Accessing MainWindow object from another class:

                          @TUStudi
                          Maybe it's just me, but I still wouldn't connect to a MainWindow slot from any other class. EDIT To be clear, I would never #include mainwindow.h into TestClass, so there is no way the latter could access &MainWindow::deviceDiscovered.

                          If you only ever create one instance of discoveryAgent once, make that exportable from TestClass to MainWindow so that the latter can do the connect on that one instance. If you create instances dynamically, invent a signal from TestClass passing the instance of QBluetoothDeviceDiscoveryAgent as an argument upon creation, and have MainWindow pick that up and put its slot on the instance's deviceDiscovered.

                          That's how I would do it. If a Qt expert says I'm wrong, please let me know!

                          @jsulm I'm not sure, but I think the OP is saying he can't do that connect from MainWindow because the instance is newed inside TestClass::startDeviceDiscovery()?

                          Hello JonB,
                          as I wrote in an other Thread, the idea of ​​mine is that I separate the actual logic from the UI. The communication should take place via my controller (my MainWindow.cpp). I.e. the user clicks on the "Scan for Bluetooth devices" button in the UI, this triggers a "button clicked" slot in my MainWindow.cpp. The startDeviceDiscovery method of the "TestClass" (which should contain the logic) is then called in this slot. If devices are then found, my MainWindow is notified by a signal (deviceDiscovered) and this MainWIndow interacts with the UI by entering the devices in the combo box.

                          553828a0-c21f-406e-92d2-7b07d312184e-image.png

                          But I just notice that a user in my other thread replied, which I unfortunately didn't see.

                          Your MainWindow should have a member variable of the TestClass class.
                          Your TestClass class should provide an API that your MainWindow can connect to and also that provides whatever data is needed.

                          So, do you know how to do that? I thought my problem can be solved If I just make my MainWIndow Object, which is created in my main class, globally.
                          But it seems I haven't understood the principle of OOP.

                          J Offline
                          J Offline
                          JonB
                          wrote on 10 Mar 2020, 09:34 last edited by JonB 3 Oct 2020, 10:40
                          #12

                          @TUStudi said in Accessing MainWindow object from another class:

                          I thought my problem can be solved If I just make my MainWIndow Object, which is created in my main class, globally.

                          I just would not do this. Make MainWindow know about the other classes, and have it do the connect()s, rather than having the other classes know about MainWindow and they do the connect(). Or, just possibly, put your "controller" class elsewhere than in MainWindow.

                          T 1 Reply Last reply 11 Mar 2020, 16:26
                          4
                          • T TUStudi
                            10 Mar 2020, 08:28

                            @jsulm said in Accessing MainWindow object from another class:

                            @TUStudi For testing I suggest you take a look at: https://doc.qt.io/qt-5/qtest-overview.html
                            For testing you do not have to create your main window inside main.

                            Thank you, but it is not a class for testing, it is just a andom class name ;)

                            @qt-1234 said in Accessing MainWindow object from another class:

                            Hi, not sure if i got your question right. But you can follow the example below. In TestClass.h you have to first declare the signal testClassSignal(). so whenever the signal is invoked in testClass.cpp, the slot in MainWindow.cpp called testClassSlot will be invoked in MainWindow.cpp. I hope it helps.

                            MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui( new Ui:MainWindow)
                            {
                             TestClass * testClass = new TestClass();
                            initConnections();
                            }
                            
                            void MainWindow::initConnections()
                            {
                            QObject::connect(testClass, SIGNAL(testClassSignal()), this, SLOT(testClassSlot()),Qt::AutoConnection);
                            }
                            
                            void MainWindow::testClassSlot()
                            {
                            
                            }
                            
                            void TestClass::TestClass()
                            {
                            
                            }
                            
                            

                            Thank you tu @qt-1234. In my case, the Signal is not emitted by the Testclass but by an object of &QBluetoothDeviceDiscoveryAgent and the Signal is the &QBluetoothDeviceDiscoveryAgent::deviceDiscovered it is emitted every time a new Bluetooth device is discovered. So that won't work, will it?

                            connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, MainWindowObject, MainWindowSlot);
                            
                            Q Offline
                            Q Offline
                            qt.1234
                            wrote on 11 Mar 2020, 06:32 last edited by
                            #13

                            @TUStudi Hi. Then you will have to first initialize the discoveryAgent class first just like how i did for the testClass before doing the connection of signal and slot.

                            DiscoveryAgent * discoveryAgent = new DiscoveryAgent();
                            
                            1 Reply Last reply
                            0
                            • J JonB
                              10 Mar 2020, 09:34

                              @TUStudi said in Accessing MainWindow object from another class:

                              I thought my problem can be solved If I just make my MainWIndow Object, which is created in my main class, globally.

                              I just would not do this. Make MainWindow know about the other classes, and have it do the connect()s, rather than having the other classes know about MainWindow and they do the connect(). Or, just possibly, put your "controller" class elsewhere than in MainWindow.

                              T Offline
                              T Offline
                              TUStudi
                              wrote on 11 Mar 2020, 16:26 last edited by
                              #14

                              @JonB said in Accessing MainWindow object from another class:

                              I just would not do this. Make MainWindow know about the other classes, and have it do the connect()s, rather than having the other classes know about MainWindow and they do the connect(). Or, just possibly, put your "controller" class elsewhere than in MainWindow.

                              Thank you, here is my approach and it seems to work. I created a new Slot in my TestClass that is called, when a device is discovered. Then, in this Slot I emitted a Signal with the devicename that is then received by my mainwindow and mainwindow then does the work. Thus, my TestClass does not know anything about my MainWindow.

                              void TestClass::startDeviceDiscovery(){
                                  discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
                                  discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
                                  connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &TestClass::deviceDiscoveredSlot);
                                  discoveryAgent->start();
                              }
                              
                              void TestClass::deviceDiscoveredSlot(const QBluetoothDeviceInfo &device)
                              {
                                  emit deviceDiscoveredSignal(device);
                              }
                              
                              

                              And in my MainWindow Class:

                              MainWindow::MainWindow(QWidget *parent)
                                  : QMainWindow(parent)
                                  , ui(new Ui::MainWindow)
                              {
                                  ui->setupUi(this);
                                  connect(bluetoothModel, &BluetoothModel::deviceDiscoveredSignal, this, &BluetoothController::deviceDiscovered);
                              }
                              
                              
                              void MainWindow::deviceDiscovered(const QBluetoothDeviceInfo &device)
                              {
                                  ui->devicesList->addItem(device.name());
                              }
                              
                              

                              The only annoying thing is that I have more code with it because I can't call a slot from MainWindow directly. But it works.

                              J 1 Reply Last reply 11 Mar 2020, 17:41
                              1
                              • T TUStudi
                                11 Mar 2020, 16:26

                                @JonB said in Accessing MainWindow object from another class:

                                I just would not do this. Make MainWindow know about the other classes, and have it do the connect()s, rather than having the other classes know about MainWindow and they do the connect(). Or, just possibly, put your "controller" class elsewhere than in MainWindow.

                                Thank you, here is my approach and it seems to work. I created a new Slot in my TestClass that is called, when a device is discovered. Then, in this Slot I emitted a Signal with the devicename that is then received by my mainwindow and mainwindow then does the work. Thus, my TestClass does not know anything about my MainWindow.

                                void TestClass::startDeviceDiscovery(){
                                    discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
                                    discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
                                    connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &TestClass::deviceDiscoveredSlot);
                                    discoveryAgent->start();
                                }
                                
                                void TestClass::deviceDiscoveredSlot(const QBluetoothDeviceInfo &device)
                                {
                                    emit deviceDiscoveredSignal(device);
                                }
                                
                                

                                And in my MainWindow Class:

                                MainWindow::MainWindow(QWidget *parent)
                                    : QMainWindow(parent)
                                    , ui(new Ui::MainWindow)
                                {
                                    ui->setupUi(this);
                                    connect(bluetoothModel, &BluetoothModel::deviceDiscoveredSignal, this, &BluetoothController::deviceDiscovered);
                                }
                                
                                
                                void MainWindow::deviceDiscovered(const QBluetoothDeviceInfo &device)
                                {
                                    ui->devicesList->addItem(device.name());
                                }
                                
                                

                                The only annoying thing is that I have more code with it because I can't call a slot from MainWindow directly. But it works.

                                J Offline
                                J Offline
                                JonB
                                wrote on 11 Mar 2020, 17:41 last edited by JonB 3 Nov 2020, 17:44
                                #15

                                @TUStudi
                                I do believe this is the neater way to go. Your MainWindow and your TestClass are now quite independent of each other. If you had done "call a slot from MainWindow directly" TestClass would not be (re-)usable without MainWindow.

                                1 Reply Last reply
                                1

                                10/15

                                10 Mar 2020, 09:01

                                • Login

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