Search code examples
c++qtmysql-connector

Send data from one UI to another UI in QT


**I posted the solution I came up with as answer

I have a login UI for students, I enter the login ID and the password and validate them from a MySQL DB I created. After the login, I need to save the entered student ID and send it to the student information page(another UI I created for students). I am using QT + C++ + MySQL. I tried to use signals and slots but no results

My main objective is to send the entered student ID to the other UI.

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow){
ui->setupUi(this);
connect(ui->userIDLogin, SIGNAL(textChanged(QString)), ui->stdName ,SLOT(setText(QString))); }

it doesn't recognize the ui->stdName which the object in the student page which I am trying to send the data to

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSql>
#include <QDialog>
#include <QSqlDatabase>
#include <QMessageBox>
#include <mysql/mysql.h>
#include "student.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

public slots:
void on_pushButton_clicked();

private:
Ui::MainWindow *ui;
Student *std;
};

struct connection_details;
MYSQL *mysql_connection_setup(struct connection_details mysql_details);

#endif // MAINWINDOW_H

Here is the error message I receive:

This is the file error message
/home/ahmad/Desktop/DBMS project/DBMS-project-                        
  master/mainwindow.cpp:38: error: ‘class Ui::MainWindow’ has no 
member named ‘stdName’
In file included from ../DBMS project/DBMS-project-    
master/student.cpp:2:
../DBMS project/DBMS-project-master/mainwindow.cpp: In constructor 
‘MainWindow::MainWindow(QWidget*)’:
../DBMS project/DBMS-project-master/mainwindow.cpp:38:73: error: 
‘class Ui::MainWindow’ has no member named ‘stdName’
38 |     QObject::connect(ui->userIDLogin, 
SIGNAL(textChanged(QString)), ui->stdName,SLOT(setText(QString)));
   |                        
                                             ^~~~~~~

Solution

  • The solution I came up with and works perfectly.

    I had two interfaces, a login and a student page (which displays info about a student).

    After the login, I wrote the default constructor for the student class (the student page is basically a class defined in the student.h and student.cpp files )

    The solution is to write a parameterized constructor and pass the login ID (the data I want to send to the student page) as a parameter from the login class to the student class