[SOLVED] HELP: GUI VS SOFT connection



  • Hi all,

    I am new on this forum and new to QT programming. I have a good background in programming microcontrolers in C++ and a bunch of other languages. However, I am struggling with a few basic (I think) points in QT C++ programming.

    Here is the code I am working on

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    //allocate de l,espace pour plugNPlay comme étant HID_PnP
    plugNPlay = new HID_PnP();
    
    //connecter le bouton ToggleLEDS à la fonction toggle_leds() de plugNPlay (hid_pnp.cPP). Cette dernière fonction met à 1 ou 2 un bit
    connect(this, SIGNAL(toggle_leds_button_pressed()), plugNPlay, SLOT(toggle_leds()));
    
    connect(this, SIGNAL(delay_changed()),plugNPlay,SLOT(pulses_changed()));
    connect(this, SIGNAL(numpulse_changed()),plugNPlay,SLOT(pulses_changed()));
    connect(this, SIGNAL(ontimepulse_changed()),plugNPlay,SLOT(pulses_changed()));
    connect(this, SIGNAL(onduration_changed()),plugNPlay,SLOT(pulses_changed()));
    connect(this, SIGNAL(start_pulses()),plugNPlay,SLOT(pulses_go()));
    
    
    //connect(this, SIGNAL(pulse_gui(int, int, int, int)), plugNPlay, SLOT(update_pulse(int, int, int, int)));
    
    //connecter la fonction hid_comm_update à l'interface
    connect(plugNPlay, SIGNAL(hid_comm_update(bool, bool, int)), this, SLOT(update_gui(bool, bool, int)));
    

    }

    MainWindow::~MainWindow()
    {
    disconnect(this, SIGNAL(toggle_leds_button_pressed()), plugNPlay, SLOT(toggle_leds()));
    disconnect(plugNPlay, SIGNAL(hid_comm_update(bool, bool, int)), this, SLOT(update_gui(bool, bool, int)));
    delete ui;
    delete plugNPlay;
    }

    void MainWindow::on_pushButton_clicked()
    {
    emit toggle_leds_button_pressed();
    }

    void MainWindow::update_gui(bool isConnected, bool isPressed, int potentiometerValue)
    {
    if(isConnected)
    {
    ui->label_2->setEnabled(true);
    ui->pushButton->setEnabled(true);
    ui->pushbuttonStatus->setEnabled(true);
    ui->progressBar->setEnabled(true);

        ui->deviceConnectedStatus->setText("Device Found: AttachedState = TRUE");
    
        if(isPressed)
            ui->pushbuttonStatus->setText("Pushbutton State: Pressed");
        else
            ui->pushbuttonStatus->setText("Pushbutton State: Not Pressed");
    
        ui->progressBar->setValue(potentiometerValue);
    }
    else
    {
        ui->label_2->setEnabled(false);
        ui->pushButton->setEnabled(false);
        ui->pushbuttonStatus->setEnabled(false);
        ui->progressBar->setEnabled(false);
    
        ui->deviceConnectedStatus->setText("Device Not Detected: Verify Connection/Correct Firmware");
        ui->pushbuttonStatus->setText("Pushbutton State: Unknown");
        ui->progressBar->setValue(0);
    }
    

    }

    void MainWindow::on_edit_delay_textChanged(const QString &delay)
    {
    emit delay_changed();
    int delayI = delay.toInt();

    }

    void MainWindow::on_number_pulse_textChanged(const QString &npulse)
    {
    emit numpulse_changed();
    int npulseI = npulse.toInt();
    }

    void MainWindow::on_pulse_time_textChanged(const QString &timepulse)
    {
    emit ontimepulse_changed();
    int timepulseI = timepulse.toInt();
    }

    void MainWindow::on_edit_duration_textChanged(const QString &duration)
    {
    emit onduration_changed();
    int ondurationI = duration.toInt();
    }

    void MainWindow::on_pb_start_single_pressed()
    {
    emit start_pulses();
    }
    @

    On line 76, I want to take the value from the delay Qline in the GUI interface and put it into a variable that I will then connect with the plugNPlay part of my code.

    THe problem is that I get a "Undeclared identifier" error

    here is the header of the code

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "hid_pnp.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    HID_PnP *plugNPlay;

    public slots:
    void update_gui(bool isConnected, bool isPressed, int potentiometerValue);
    // void pulse_gui(int delay, int npulse, int timepulse, int duration);

    signals:
    void toggle_leds_button_pressed();
    void delay_changed();
    void numpulse_changed();
    void ontimepulse_changed();
    void onduration_changed();
    void start_pulses();

    private slots:
    void on_pushButton_clicked();
    void on_edit_delay_textChanged(const QString &arg1);
    void on_number_pulse_textChanged(const QString &arg1);
    void on_pulse_time_textChanged(const QString &arg1);
    void on_edit_duration_textChanged(const QString &arg1);
    void on_pb_start_single_pressed();
    };

    #endif // MAINWINDOW_H
    @

    Thanks for your help



  • try to get value before you emit signal:
    @
    int delayI = delay.toInt();
    emit delay_changed();
    @

    if it doesn't help, the problem is elsewhere... And you know what delayI will be local variable in this function and not accessible from elsewhere...



  • So, maybe you want to do something like that:
    @
    this->plugNPlay->delay = delay.toint();
    emit delay_changed();
    @



  • ok.. thanks, second solution is what I need... It gives another error... "delay is not member of plugNPlay".. I will work on that.

    Thanks a lot



  • How do I make delay a member of plugNPlay if I use the following???

    @this->plugNPlay->delay = delay.toint();
    emit delay_changed();--@

    Thanks a lot



  • It was just an example. You should look in HID_PnP class for variable name reference. Take a look which variable is used in the pulses_changed() function of HID_PnP.

    Or post HID_PnP class here.



  • Thanks for your help.. here is the HID_PnP class. I want to send the variable delay to usb via PollUSB function.

    @#include "hid_pnp.h"

    HID_PnP::HID_PnP(QObject *parent) : QObject(parent) {
    isConnected = false;
    pushbuttonStatus = false;
    potentiometerValue = 0;
    toggleLeds = 0;

    device = NULL;
    buf[0] = 0x00;
    buf[1] = 0x37;
    memset((void*)&buf[2], 0x00, sizeof(buf) - 2);
    
    timer = new QTimer();
    connect(timer, SIGNAL(timeout()), this, SLOT(PollUSB()));
    
    timer->start(250);
    

    }

    HID_PnP::~HID_PnP() {
    disconnect(timer, SIGNAL(timeout()), this, SLOT(PollUSB()));
    }
    /*void HID_PnP::update_pulse(int delayI, int npulseI, int timepulseI, int durationI){

    disconnect(timer, SIGNAL(timeout()), this, SLOT(PollUSB()));

            buf[0] = 0x40;//envoie les infor pour toggling
            buf[1]= delayI;
            buf[2]= npulseI;
            buf[3]= timepulseI;
            buf[4]= durationI;
    
            if (hid_write(device, buf, sizeof(buf)) == -1)
            {
                CloseDevice();
                return;
            }
    

    connect(timer, SIGNAL(timeout()), this, SLOT(PollUSB()));

    }
    

    */

    void HID_PnP::PollUSB()
    {
    buf[0] = 0x00;
    memset((void*)&buf[2], 0x00, sizeof(buf) - 2);

    if (isConnected == false) {
        device = hid_open(0x04d8, 0x003f, NULL);
    
        if (device) {
            isConnected = true;
            hid_set_nonblocking(device, true);
            timer->start(15);
        }
    }
    else {
        if(toggleLeds == true) {
            toggleLeds = false;
        //envoie la commande toggle au PIC
            buf[1] = 0x40;
            buf[2] = 0x90;
            buf[3] = 0x90;
    
            //écrire et si on a recu une erreur lors du write on ferme le device pour le réouvrir plus tard.. retourne -1 si erreur
            if (hid_write(device, buf, sizeof(buf)) == -1)
            {
                CloseDevice();
                return;
            }
        //charge buffer for potentiomètre et bouton-poussoir
            buf[0] = 0x00;
            buf[1] = 0x37;
            memset((void*)&buf[2], 0x00, sizeof(buf) - 2);
        }
    
    
    
        //écrire 37 et si on a recu une erreur lors du write on ferme le device pour le réouvrir plus tard.. retourne -1 si erreur
        //ceci va permettre d'aller chercher la valeur du potentiomètre (37)
        if (hid_write(device, buf, sizeof(buf)) == -1)
        {
            CloseDevice();
            return;
        }
        //lire ce que le PIC a à nous dire
        if(hid_read(device, buf, sizeof(buf)) == -1)
        {
            CloseDevice();
            return;
        }
        //si le PIC retourne 37, il veut parler du potentiomètre
        if(buf[0] == 0x37) {
            potentiometerValue = (buf[2]<<8) + buf[1];
            buf[1] = 0x81;
        }
        //si le PIC retourne 81, il veut parler du pushbutton Status
        else if(buf[0] == 0x81) {
            pushbuttonStatus = (buf[1] == 0x00);
            buf[1] = 0x37;
        }
    
    
    
    
    }
    
    hid_comm_update(isConnected, pushbuttonStatus, potentiometerValue);
    

    }

    //toggle led??
    void HID_PnP::toggle_leds() {
    toggleLeds = true;
    }

    void HID_PnP::pulses_changed() {
    pulseschanged = true;
    }
    void HID_PnP::pulses_go() {
    pulsesGO = true;
    }

    void HID_PnP::CloseDevice() {
    hid_close(device);
    device = NULL;
    isConnected = false;
    pushbuttonStatus = false;
    potentiometerValue = 0;
    toggleLeds = 0;
    hid_comm_update(isConnected, pushbuttonStatus, potentiometerValue);
    timer->start(250);
    }
    @



  • Show the class declaration from the header file too...



  • ok.. here it is

    Thanks for your help!

    @#ifndef HID_PNP_H
    #define HID_PNP_H

    #include <QObject>
    #include <QTimer>
    #include "../HIDAPI/hidapi.h"

    #include <wchar.h>
    #include <string.h>
    #include <stdlib.h>

    #define MAX_STR 65

    class HID_PnP : public QObject
    {
    Q_OBJECT
    public:
    explicit HID_PnP(QObject *parent = 0);
    ~HID_PnP();

    signals:
    void hid_comm_update(bool isConnected, bool isPressed, int potentiometerValue);

    public slots:
    void toggle_leds();
    void pulses_changed();
    void PollUSB();
    void pulses_go();
    //void update_pulse(int delayI, int npulseI, int timepulseI, int durationI);

    private:
    bool isConnected;
    bool pushbuttonStatus;
    bool toggleLeds;
    bool pulseschanged;
    bool pulsesGO;
    int potentiometerValue;

    hid_device *device;
    QTimer *timer;
    unsigned char buf[MAX_STR];
    
    void CloseDevice();
    

    };

    #endif // HID_PNP_H
    @



  • @
    public:
    explicit HID_PnP(QObject *parent = 0);
    ~HID_PnP();
    //Your delay variable here
    int MyDelay;
    @

    @
    HID_PnP::HID_PnP(QObject *parent) : QObject(parent) {
    isConnected = false;
    pushbuttonStatus = false;
    potentiometerValue = 0;
    toggleLeds = 0;
    MyDelay = -1;
    @

    @
    void MainWindow::on_edit_delay_textChanged(const QString &delay)
    {
    emit delay_changed();
    plugNPlay->MyDelay = delay.toInt();
    }
    @

    @
    void HID_PnP::PollUSB()
    {
    ....
    //check if your delay is set
    if(this->MyDelay != -1)
    {
    //do whatever you need
    }
    ...
    }
    @



  • THANK YOU... it works. I think I can make it from here.



  • I'm glad I could help you. Please add "[SOLVED]" prefix left to the topic subject. Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.