Unsolved Interface class
-
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"; }
-
Hi
You can use signal & slots for that
http://doc.qt.io/qt-5/signalsandslots.htmlJust 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.
-
thanks i didn't want to use signal and slots. found an exmaple here
-
@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.