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. How to use methods of an object from a different class?
Forum Update on Monday, May 27th 2025

How to use methods of an object from a different class?

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 4 Posters 6.9k 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.
  • B Offline
    B Offline
    bask185
    wrote on 21 Mar 2017, 09:21 last edited by
    #21

    The one with the comma remains ;)

    #include "mainwindow.h" // mainwindow.cpp
    #include "ui_mainwindow.h"
    #include <QPixmap>
    #include <QDebug>
    #include <QPalette>
    #include <QtSerialPort>
    #include "keyboard.h"
    #include "numpad.h"
    QSerialPort *serial;
    void readCommand();
    
    char lettres[5][40][16];     // initializes all to be used variables;
    int index1 = 0;
    int index2 = 0;
    int colom[5];
    int row[5];
    int colorIndex[5];
    char c;
    Keyboard *Kboard;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        serial = new QSerialPort();
        serial->setPortName("COM4");
        serial->open(QIODevice::ReadWrite);
        serial->setBaudRate(QSerialPort::Baud115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::OddParity);
        serial->setStopBits(QSerialPort::OneStop);
        //serial.setFlowControl(QSerialPort::NoFlowControl);
        //serial.open(QIODevice::ReadWrite);
    
        QPixmap logo(":/resources/img/logo.jpg");
        int w = ui->label->width();
        int h = ui->label->height();
        ui->label->setPixmap(logo.scaled(w,h,Qt::KeepAspectRatio));
    
        for(int i=0;i<5;i++) {
            for (int j=0;j<40;j++) {
                for (int k=0;k<16;k++){
                    lettres[i][j][k] =' ';   // initializes the letters array
                }
            }
        }
       connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
    
       Kboard = new Keyboard();
       connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
    }
    
    MainWindow::~MainWindow()
    {
       delete ui;
    }
    
    void MainWindow::serialReceived()
    {
        QByteArray b = serial->readAll();
        qDebug() << b;
    }
    
    void MainWindow::send(QString c)
    {
      serial->write("c");
      qDebug() << "hello world " << c;
    }
    
    #include "keyboard.h" //keyboard.cpp
    #include "ui_keyboard.h"
    #include <QtSerialPort>
    #include "mainwindow.h"
    
    
    bool caps = true, shift = true;
    
    Keyboard::Keyboard(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Keyboard)
    
    {
        ui->setupUi(this);
    }
    
    Keyboard::~Keyboard()
    {
        delete ui;
    }
    
    void Keyboard::on_a_clicked()
    {
       QString c;
       c = ui->a->text();
       emit KeyPressed(c);
       qDebug() << c;
    
       if(caps == true && shift == false){ // disables the effect of the shift key when any lettre has been pressed.
           shift = false;
           on_shift1_clicked();
       }
    }
    
    #ifndef MAINWINDOW_H //main window.h
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "keyboard.h"
    #include "numpad.h"
    
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void send(QString c);
    
    private:
        Ui::MainWindow *ui;
        Keyboard *keyboard;
        Numpad *numpad;
    };
    
    #endif // MAINWINDOW_H
    
    #ifndef KEYBOARD_H
    #define KEYBOARD_H
    
    #include <QDialog>
    
    namespace Ui {  //keyboard.h
    class Keyboard;
    }
    
    class Keyboard : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Keyboard(QWidget *parent = 0);
        ~Keyboard();
    signals:
        void KeyPressed(QString);
    
    private slots:
        void on_a_clicked();
        
    
    private:
        Ui::Keyboard *ui;
    };
    
    #endif // KEYBOARD_H
    
    J J 3 Replies Last reply 21 Mar 2017, 09:26
    0
    • B bask185
      21 Mar 2017, 09:21

      The one with the comma remains ;)

      #include "mainwindow.h" // mainwindow.cpp
      #include "ui_mainwindow.h"
      #include <QPixmap>
      #include <QDebug>
      #include <QPalette>
      #include <QtSerialPort>
      #include "keyboard.h"
      #include "numpad.h"
      QSerialPort *serial;
      void readCommand();
      
      char lettres[5][40][16];     // initializes all to be used variables;
      int index1 = 0;
      int index2 = 0;
      int colom[5];
      int row[5];
      int colorIndex[5];
      char c;
      Keyboard *Kboard;
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          serial = new QSerialPort();
          serial->setPortName("COM4");
          serial->open(QIODevice::ReadWrite);
          serial->setBaudRate(QSerialPort::Baud115200);
          serial->setDataBits(QSerialPort::Data8);
          serial->setParity(QSerialPort::OddParity);
          serial->setStopBits(QSerialPort::OneStop);
          //serial.setFlowControl(QSerialPort::NoFlowControl);
          //serial.open(QIODevice::ReadWrite);
      
          QPixmap logo(":/resources/img/logo.jpg");
          int w = ui->label->width();
          int h = ui->label->height();
          ui->label->setPixmap(logo.scaled(w,h,Qt::KeepAspectRatio));
      
          for(int i=0;i<5;i++) {
              for (int j=0;j<40;j++) {
                  for (int k=0;k<16;k++){
                      lettres[i][j][k] =' ';   // initializes the letters array
                  }
              }
          }
         connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
      
         Kboard = new Keyboard();
         connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
      }
      
      MainWindow::~MainWindow()
      {
         delete ui;
      }
      
      void MainWindow::serialReceived()
      {
          QByteArray b = serial->readAll();
          qDebug() << b;
      }
      
      void MainWindow::send(QString c)
      {
        serial->write("c");
        qDebug() << "hello world " << c;
      }
      
      #include "keyboard.h" //keyboard.cpp
      #include "ui_keyboard.h"
      #include <QtSerialPort>
      #include "mainwindow.h"
      
      
      bool caps = true, shift = true;
      
      Keyboard::Keyboard(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::Keyboard)
      
      {
          ui->setupUi(this);
      }
      
      Keyboard::~Keyboard()
      {
          delete ui;
      }
      
      void Keyboard::on_a_clicked()
      {
         QString c;
         c = ui->a->text();
         emit KeyPressed(c);
         qDebug() << c;
      
         if(caps == true && shift == false){ // disables the effect of the shift key when any lettre has been pressed.
             shift = false;
             on_shift1_clicked();
         }
      }
      
      #ifndef MAINWINDOW_H //main window.h
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include "keyboard.h"
      #include "numpad.h"
      
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      private slots:
          void send(QString c);
      
      private:
          Ui::MainWindow *ui;
          Keyboard *keyboard;
          Numpad *numpad;
      };
      
      #endif // MAINWINDOW_H
      
      #ifndef KEYBOARD_H
      #define KEYBOARD_H
      
      #include <QDialog>
      
      namespace Ui {  //keyboard.h
      class Keyboard;
      }
      
      class Keyboard : public QDialog
      {
          Q_OBJECT
      
      public:
          explicit Keyboard(QWidget *parent = 0);
          ~Keyboard();
      signals:
          void KeyPressed(QString);
      
      private slots:
          void on_a_clicked();
          
      
      private:
          Ui::Keyboard *ui;
      };
      
      #endif // KEYBOARD_H
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 21 Mar 2017, 09:26 last edited by
      #22

      @bask185 One note: why do you use global variables? This is bad habit!
      For example Kboard should be a member variable of your MainWindow class.

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

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bask185
        wrote on 21 Mar 2017, 09:31 last edited by
        #23

        idk to get it to work I suppose, but I should declare it in the constructor above the connect function??

        In processing I sometimes use global variables because I can acces and modify them in every class I write. In Qt I know very little of what the firetruck I am doing. I do know that in Qt it is a lot more work to get an exact same application than in processing..

        E 1 Reply Last reply 21 Mar 2017, 09:48
        0
        • B bask185
          21 Mar 2017, 09:21

          The one with the comma remains ;)

          #include "mainwindow.h" // mainwindow.cpp
          #include "ui_mainwindow.h"
          #include <QPixmap>
          #include <QDebug>
          #include <QPalette>
          #include <QtSerialPort>
          #include "keyboard.h"
          #include "numpad.h"
          QSerialPort *serial;
          void readCommand();
          
          char lettres[5][40][16];     // initializes all to be used variables;
          int index1 = 0;
          int index2 = 0;
          int colom[5];
          int row[5];
          int colorIndex[5];
          char c;
          Keyboard *Kboard;
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              serial = new QSerialPort();
              serial->setPortName("COM4");
              serial->open(QIODevice::ReadWrite);
              serial->setBaudRate(QSerialPort::Baud115200);
              serial->setDataBits(QSerialPort::Data8);
              serial->setParity(QSerialPort::OddParity);
              serial->setStopBits(QSerialPort::OneStop);
              //serial.setFlowControl(QSerialPort::NoFlowControl);
              //serial.open(QIODevice::ReadWrite);
          
              QPixmap logo(":/resources/img/logo.jpg");
              int w = ui->label->width();
              int h = ui->label->height();
              ui->label->setPixmap(logo.scaled(w,h,Qt::KeepAspectRatio));
          
              for(int i=0;i<5;i++) {
                  for (int j=0;j<40;j++) {
                      for (int k=0;k<16;k++){
                          lettres[i][j][k] =' ';   // initializes the letters array
                      }
                  }
              }
             connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
          
             Kboard = new Keyboard();
             connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
          }
          
          MainWindow::~MainWindow()
          {
             delete ui;
          }
          
          void MainWindow::serialReceived()
          {
              QByteArray b = serial->readAll();
              qDebug() << b;
          }
          
          void MainWindow::send(QString c)
          {
            serial->write("c");
            qDebug() << "hello world " << c;
          }
          
          #include "keyboard.h" //keyboard.cpp
          #include "ui_keyboard.h"
          #include <QtSerialPort>
          #include "mainwindow.h"
          
          
          bool caps = true, shift = true;
          
          Keyboard::Keyboard(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::Keyboard)
          
          {
              ui->setupUi(this);
          }
          
          Keyboard::~Keyboard()
          {
              delete ui;
          }
          
          void Keyboard::on_a_clicked()
          {
             QString c;
             c = ui->a->text();
             emit KeyPressed(c);
             qDebug() << c;
          
             if(caps == true && shift == false){ // disables the effect of the shift key when any lettre has been pressed.
                 shift = false;
                 on_shift1_clicked();
             }
          }
          
          #ifndef MAINWINDOW_H //main window.h
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          #include "keyboard.h"
          #include "numpad.h"
          
          
          namespace Ui {
          class MainWindow;
          }
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();
          
          private slots:
              void send(QString c);
          
          private:
              Ui::MainWindow *ui;
              Keyboard *keyboard;
              Numpad *numpad;
          };
          
          #endif // MAINWINDOW_H
          
          #ifndef KEYBOARD_H
          #define KEYBOARD_H
          
          #include <QDialog>
          
          namespace Ui {  //keyboard.h
          class Keyboard;
          }
          
          class Keyboard : public QDialog
          {
              Q_OBJECT
          
          public:
              explicit Keyboard(QWidget *parent = 0);
              ~Keyboard();
          signals:
              void KeyPressed(QString);
          
          private slots:
              void on_a_clicked();
              
          
          private:
              Ui::Keyboard *ui;
          };
          
          #endif // KEYBOARD_H
          
          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 21 Mar 2017, 09:32 last edited by
          #24

          @bask185 said in How to use methods of an object from a different class?:

          Keyboard *keyboard;

          I think I found the mixup,

          You have Keyboard as a priavte member of your mainwindow.h, as it should be, and as a global Variable too.

          I think the instance of your keyboard, that you show and have input of, is not the one you connected your Signal from.

          Change that.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          B 1 Reply Last reply 21 Mar 2017, 09:48
          1
          • B bask185
            21 Mar 2017, 09:21

            The one with the comma remains ;)

            #include "mainwindow.h" // mainwindow.cpp
            #include "ui_mainwindow.h"
            #include <QPixmap>
            #include <QDebug>
            #include <QPalette>
            #include <QtSerialPort>
            #include "keyboard.h"
            #include "numpad.h"
            QSerialPort *serial;
            void readCommand();
            
            char lettres[5][40][16];     // initializes all to be used variables;
            int index1 = 0;
            int index2 = 0;
            int colom[5];
            int row[5];
            int colorIndex[5];
            char c;
            Keyboard *Kboard;
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                serial = new QSerialPort();
                serial->setPortName("COM4");
                serial->open(QIODevice::ReadWrite);
                serial->setBaudRate(QSerialPort::Baud115200);
                serial->setDataBits(QSerialPort::Data8);
                serial->setParity(QSerialPort::OddParity);
                serial->setStopBits(QSerialPort::OneStop);
                //serial.setFlowControl(QSerialPort::NoFlowControl);
                //serial.open(QIODevice::ReadWrite);
            
                QPixmap logo(":/resources/img/logo.jpg");
                int w = ui->label->width();
                int h = ui->label->height();
                ui->label->setPixmap(logo.scaled(w,h,Qt::KeepAspectRatio));
            
                for(int i=0;i<5;i++) {
                    for (int j=0;j<40;j++) {
                        for (int k=0;k<16;k++){
                            lettres[i][j][k] =' ';   // initializes the letters array
                        }
                    }
                }
               connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
            
               Kboard = new Keyboard();
               connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
            }
            
            MainWindow::~MainWindow()
            {
               delete ui;
            }
            
            void MainWindow::serialReceived()
            {
                QByteArray b = serial->readAll();
                qDebug() << b;
            }
            
            void MainWindow::send(QString c)
            {
              serial->write("c");
              qDebug() << "hello world " << c;
            }
            
            #include "keyboard.h" //keyboard.cpp
            #include "ui_keyboard.h"
            #include <QtSerialPort>
            #include "mainwindow.h"
            
            
            bool caps = true, shift = true;
            
            Keyboard::Keyboard(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::Keyboard)
            
            {
                ui->setupUi(this);
            }
            
            Keyboard::~Keyboard()
            {
                delete ui;
            }
            
            void Keyboard::on_a_clicked()
            {
               QString c;
               c = ui->a->text();
               emit KeyPressed(c);
               qDebug() << c;
            
               if(caps == true && shift == false){ // disables the effect of the shift key when any lettre has been pressed.
                   shift = false;
                   on_shift1_clicked();
               }
            }
            
            #ifndef MAINWINDOW_H //main window.h
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            #include "keyboard.h"
            #include "numpad.h"
            
            
            namespace Ui {
            class MainWindow;
            }
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();
            
            private slots:
                void send(QString c);
            
            private:
                Ui::MainWindow *ui;
                Keyboard *keyboard;
                Numpad *numpad;
            };
            
            #endif // MAINWINDOW_H
            
            #ifndef KEYBOARD_H
            #define KEYBOARD_H
            
            #include <QDialog>
            
            namespace Ui {  //keyboard.h
            class Keyboard;
            }
            
            class Keyboard : public QDialog
            {
                Q_OBJECT
            
            public:
                explicit Keyboard(QWidget *parent = 0);
                ~Keyboard();
            signals:
                void KeyPressed(QString);
            
            private slots:
                void on_a_clicked();
                
            
            private:
                Ui::Keyboard *ui;
            };
            
            #endif // KEYBOARD_H
            
            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 21 Mar 2017, 09:33 last edited by
            #25

            @bask185 said in How to use methods of an object from a different class?:

            void MainWindow::send(QString c)
            {
            serial->write("c");
            qDebug() << "hello world " << c;
            }

            Can you try to comment out serial->write("c"); and try again?
            Also: I guess you want to send the string/character entered by user over serial bus, right?
            In that case you should change

            serial->write("c");
            

            to

            serial->write(c.toLatin1()); // If you only use ASCII characters
            

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

            1 Reply Last reply
            0
            • B bask185
              21 Mar 2017, 09:31

              idk to get it to work I suppose, but I should declare it in the constructor above the connect function??

              In processing I sometimes use global variables because I can acces and modify them in every class I write. In Qt I know very little of what the firetruck I am doing. I do know that in Qt it is a lot more work to get an exact same application than in processing..

              E Offline
              E Offline
              Eeli K
              wrote on 21 Mar 2017, 09:48 last edited by
              #26

              @bask185 I didn't know you know so little about C++ and Qt. It's true that C++ is in many ways a low-level language. Qt takes it a bit higher, but certainly it's still means more work for certain things than many other languages. But on the other hand C++ is on the most popular and used general purpose languages in the world, and unlike with processing, you can call yourself a real programmer if you master C++ and Qt. Every language has its place and has pros and cons. If you want to just get one program done in your life C++ isn't the right language for you, otherwise it pays back what you put into learning it.

              1 Reply Last reply
              0
              • J J.Hilk
                21 Mar 2017, 09:32

                @bask185 said in How to use methods of an object from a different class?:

                Keyboard *keyboard;

                I think I found the mixup,

                You have Keyboard as a priavte member of your mainwindow.h, as it should be, and as a global Variable too.

                I think the instance of your keyboard, that you show and have input of, is not the one you connected your Signal from.

                Change that.

                B Offline
                B Offline
                bask185
                wrote on 21 Mar 2017, 09:48 last edited by bask185
                #27

                @J.Hilk said in How to use methods of an object from a different class?:

                @bask185 said in How to use methods of an object from a different class?:

                Keyboard *keyboard;

                I think I found the mixup,

                You have Keyboard as a priavte member of your mainwindow.h, as it should be, and as a global Variable too.

                I think the instance of your keyboard, that you show and have input of, is not the one you connected your Signal from.

                Change that.

                Application Output:  hello world   "a"   // we have a winner!
                                     hello world  "A" // < caps works too :D
                

                My thank is great Y'all

                @jsulm said in How to use methods of an object from a different class?:

                I guess you want to send the string/character entered by user over serial bus, right?

                was it that obvious :P?

                In that case you should change
                serial->write("c");

                to
                serial->write(c.toLatin1()); // If you only use ASCII characters

                Application Output:  "\x06\x04\x01""a"  // < proper respons of the arduino for receiving the 'a'. Tnx ;)
                

                sigh though... in processing or arduino or whatever I usually send.. just integers or longs. Vague Qt syntax with & * :: private public and what if I want to send a long variable over serial port would I need to characterize all 10 numbers and send 10 bytes instead of 4????

                @Eeli-K said in How to use methods of an object from a different class?:

                @bask185 I didn't know you know so little about C++ and Qt. It's true that C++ is in many ways a low-level language. Qt takes it a bit higher, but certainly it's still means more work for certain things than many other languages. But on the other hand C++ is on the most popular and used general purpose languages in the world, and unlike with processing, you can call yourself a real programmer if you master C++ and Qt. Every language has its place and has pros and cons. If you want to just get one program done in your life C++ isn't the right language for you, otherwise it pays back what you put into learning it.

                we had a few lessons in C++ in my first year in visual studios but even there we would not use :: privates and publics, (the latter 2 we got with java). And as said before I only learned how to use pointers with arrays and not with functions.

                But regardless, I almost have my bachelor degree in electrical engineering and learning a new programming language from scrap is what we do.

                J J 2 Replies Last reply 21 Mar 2017, 09:56
                0
                • B bask185
                  21 Mar 2017, 09:48

                  @J.Hilk said in How to use methods of an object from a different class?:

                  @bask185 said in How to use methods of an object from a different class?:

                  Keyboard *keyboard;

                  I think I found the mixup,

                  You have Keyboard as a priavte member of your mainwindow.h, as it should be, and as a global Variable too.

                  I think the instance of your keyboard, that you show and have input of, is not the one you connected your Signal from.

                  Change that.

                  Application Output:  hello world   "a"   // we have a winner!
                                       hello world  "A" // < caps works too :D
                  

                  My thank is great Y'all

                  @jsulm said in How to use methods of an object from a different class?:

                  I guess you want to send the string/character entered by user over serial bus, right?

                  was it that obvious :P?

                  In that case you should change
                  serial->write("c");

                  to
                  serial->write(c.toLatin1()); // If you only use ASCII characters

                  Application Output:  "\x06\x04\x01""a"  // < proper respons of the arduino for receiving the 'a'. Tnx ;)
                  

                  sigh though... in processing or arduino or whatever I usually send.. just integers or longs. Vague Qt syntax with & * :: private public and what if I want to send a long variable over serial port would I need to characterize all 10 numbers and send 10 bytes instead of 4????

                  @Eeli-K said in How to use methods of an object from a different class?:

                  @bask185 I didn't know you know so little about C++ and Qt. It's true that C++ is in many ways a low-level language. Qt takes it a bit higher, but certainly it's still means more work for certain things than many other languages. But on the other hand C++ is on the most popular and used general purpose languages in the world, and unlike with processing, you can call yourself a real programmer if you master C++ and Qt. Every language has its place and has pros and cons. If you want to just get one program done in your life C++ isn't the right language for you, otherwise it pays back what you put into learning it.

                  we had a few lessons in C++ in my first year in visual studios but even there we would not use :: privates and publics, (the latter 2 we got with java). And as said before I only learned how to use pointers with arrays and not with functions.

                  But regardless, I almost have my bachelor degree in electrical engineering and learning a new programming language from scrap is what we do.

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 21 Mar 2017, 09:56 last edited by
                  #28

                  @bask185 said in How to use methods of an object from a different class?:

                  we had a few lessons in C++ in my first year in visual studios but even there we would not use :: privates and publics

                  Then those were not really C++ lessons. public/private (data encapsulation) belong to core concepts in C++ (and any other object oriented programming language).

                  "what if I want to send a long variable over serial port would I need to characterize all 10 numbers and send 10 bytes instead of 4" - no.
                  From the user you will get a string like "12345".
                  Then convert it to int:

                  long int number = c.toLong();
                  

                  And then send the number to Arduino.

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

                  1 Reply Last reply
                  0
                  • B bask185
                    21 Mar 2017, 09:48

                    @J.Hilk said in How to use methods of an object from a different class?:

                    @bask185 said in How to use methods of an object from a different class?:

                    Keyboard *keyboard;

                    I think I found the mixup,

                    You have Keyboard as a priavte member of your mainwindow.h, as it should be, and as a global Variable too.

                    I think the instance of your keyboard, that you show and have input of, is not the one you connected your Signal from.

                    Change that.

                    Application Output:  hello world   "a"   // we have a winner!
                                         hello world  "A" // < caps works too :D
                    

                    My thank is great Y'all

                    @jsulm said in How to use methods of an object from a different class?:

                    I guess you want to send the string/character entered by user over serial bus, right?

                    was it that obvious :P?

                    In that case you should change
                    serial->write("c");

                    to
                    serial->write(c.toLatin1()); // If you only use ASCII characters

                    Application Output:  "\x06\x04\x01""a"  // < proper respons of the arduino for receiving the 'a'. Tnx ;)
                    

                    sigh though... in processing or arduino or whatever I usually send.. just integers or longs. Vague Qt syntax with & * :: private public and what if I want to send a long variable over serial port would I need to characterize all 10 numbers and send 10 bytes instead of 4????

                    @Eeli-K said in How to use methods of an object from a different class?:

                    @bask185 I didn't know you know so little about C++ and Qt. It's true that C++ is in many ways a low-level language. Qt takes it a bit higher, but certainly it's still means more work for certain things than many other languages. But on the other hand C++ is on the most popular and used general purpose languages in the world, and unlike with processing, you can call yourself a real programmer if you master C++ and Qt. Every language has its place and has pros and cons. If you want to just get one program done in your life C++ isn't the right language for you, otherwise it pays back what you put into learning it.

                    we had a few lessons in C++ in my first year in visual studios but even there we would not use :: privates and publics, (the latter 2 we got with java). And as said before I only learned how to use pointers with arrays and not with functions.

                    But regardless, I almost have my bachelor degree in electrical engineering and learning a new programming language from scrap is what we do.

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 21 Mar 2017, 09:56 last edited by
                    #29

                    @bask185 said in How to use methods of an object from a different class?:

                    sigh though... in processing or arduino or whatever I usually send.. just integers or longs. Vague Qt syntax with & * :: private public and what if I want to send a long variable over serial port would I need to characterize all 10 numbers and send 10 bytes instead of 4????

                    QSerialPort::writeData has a max length of qint64 , long long int and __int64 respectively so you should be fine sending 'long' variables.


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    0

                    21/29

                    21 Mar 2017, 09:21

                    • Login

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