Skip to content
Advertisement

How to emit signal when the label text change?

I have 3 forms (main form, password form and menu form). The user clicks menu to open menu form, but before the menu form shows, the password form display to authenticate the user. He could display RFID card or enter his Id and password.

When he puts his card the socket notifier in main form recieved the RFID card data and it send to active form (my active form is password form). So the main form send card data to lblrfid:

if(f->windowTitle()=="PassDialog")
{
    QLabel* lbl = f->findChild<QLabel*>("lblRFID");
    //mf->processData(output);
    lbl->setText(output);
    return;
}

in password form I have a slot to authenticate the RFID card data and open menu form if the card is valid.

void MyDialog::AcceptCard()
{
    QString output=ui->lblRFID->text();
    bool st=database->checkPassword(NULL,output,"1");
    //this user is valid to go to menu page
    //s/he is admin
    if(st)
    {
        this->close();
        menu *m=new menu();
        m->showFullScreen();
    }
}  

I want to call AcceptCard after the label text changed.
How can I do that?

Advertisement

Answer

Provided code is not the best variant of using forms…

I think password form is created in main from code? Am I correct?

Here is an example of better code (not the best of cource):

Code can contain errors! Have no ability to check it now!

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

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

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void showPasswordDialog();
    void gotRFID(QString output);

private:
    Ui::MainWindow *ui;
    MyDialog *passwordDialog;
};

#endif // MAINWINDOW_H

mainwindow.cpp

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

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

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showPasswordDialog()
{
    passwordDialog = new MyDialog(this);
    passwordDialog->show();
}

void MainWindow::gotRFID(QString output)
{
    if (!passwordDialog) {
        showPasswordDialog();
    }
    passwordDialog->setRFID(output);
}

mydialog.h

#ifndef MYDIALOG_H
#define MYDIALOG_H

#include <QDialog>

namespace Ui {
class MyDialog;
}

class MyDialog : public QDialog
{
    Q_OBJECT

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

public slots:
    void AcceptCard();
    void setRFID(QString data);

private:
    Ui::MyDialog *ui;
};

#endif // MYDIALOG_H

mydialog.cpp

#include "mydialog.h"
#include "ui_mydialog.h"

MyDialog::MyDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyDialog)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);
}

MyDialog::~MyDialog()
{
    delete ui;
}

void MyDialog::AcceptCard()
{
    QString output=ui->lblRFID->text();
    bool st=database->checkPassword(NULL,output,"1");
    //this user is valid to go to menu page
    //s/he is admin
    if (st)
    {
        this->close();
        menu *m=new menu();
        m->showFullScreen();
    }
}

void MyDialog::setRFID(QString data)
{
    ui->lblRFID->setText(data);
    AcceptCard();
}

EDIT

You can cast f to MyDialog

if(f->windowTitle()=="PassDialog")
{
    MyDialog *tempDialog = qobject_cast<MyDialog*>(f);
    tempDialog->setRFID(output);
    return;
}
Advertisement