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. Interface class
Forum Updated to NodeBB v4.3 + New Features

Interface class

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.2k 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.
  • R Offline
    R Offline
    rafael
    wrote on last edited by
    #1

    I am trying to create a serial library my original code has a lot of logging inside the serial class i am trying to seperate gui code from my serial class. in the example below i'm struggling to pass data from the serial class to the main gui.

    could you let me know what i'm missing.

    #ifndef INTERFACE_H
    #define INTERFACE_H
    
    #include <string>
    
    class Interface
    {
    public:
        Interface();
        virtual ~Interface(){}
        virtual void printerror(std::string val) = 0;
    };
    
    #endif // INTERFACE_H
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "interface.h"
    #include "serial.h"
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow, public Interface
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        void printerror(std::string);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
        serial *Serial;
    
    };
    
    #endif // MAINWINDOW_H
    
    #ifndef SERIAL_H
    #define SERIAL_H
    
    #include <string>
    #include "interface.h"
    #include <QWidget>
    #include <QDebug>
    
    class serial
    {
    
    
    public:
        serial();
        std::string value();
    };
    
    #endif // SERIAL_H
    
    #include "interface.h"
    
    Interface::Interface()
    {
    
    }
    
    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        Serial = new serial();
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::printerror(std::string val)
    {
        qDebug() << QString::fromStdString(val);
    }
    
    #include "serial.h"
    #include "interface.h"
    
    serial::serial()
    {
        // i need to pass com1 log to gui.
        this->printerror(value());
    
    }
    
    std::string serial::value()
    {
        return "com1";
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You can use signal & slots for that
      http://doc.qt.io/qt-5/signalsandslots.html

      Just like Qt serial class does.

      so you connect it to mainwindow (or any other class)

      and just do

      serial::serial()
      {
         // i need to pass com1 log to gui.
         emit printerror(value());
      
      }
      

      It can then work with any class having a compatible slot.

      1 Reply Last reply
      2
      • R Offline
        R Offline
        rafael
        wrote on last edited by
        #3

        thanks i didn't want to use signal and slots. found an exmaple here

        https://sourcemaking.com/design_patterns/observer/cpp/3

        mrjjM 1 Reply Last reply
        0
        • R rafael

          thanks i didn't want to use signal and slots. found an exmaple here

          https://sourcemaking.com/design_patterns/observer/cpp/3

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @rafael
          Ok, but in fact the signal & slots are a sort of observer pattern
          but the slots do not pull, they are called as the main difference.
          But nothing wrong with your sample and will allow to use in non Qt code also.

          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