Passing data from Qdialog to main window in Qt

New_ToQT

I am trying to pass data from a Qdialog (Login dialog) to my mainWindow after a successful login and was wondering if it is possible to use Signals and slots to achieve this. Here is my Main.cpp file so far in which I connect my Login Dialog to main window:

int main(int argc, char *argv[]){
    QApplication a(argc, argv);
    Login l;
    l.createConnection();
    MainWindow w;
    l.show();
    QObject::connect(&l, SIGNAL(accept()), &w, SLOT(show()));
    QObject::connect(&w, SIGNAL(Logout()), &l, SLOT(show()));
    return a.exec();
}

The signal accept emits from the Dialog after the user inserts correct username/password and I would like afterwards to pass the information relative to this user to my main window.

The user class I'm trying to pass:

class User
    {
    QString ID;
    QString username;
    QString password;
    QString name;
    QString Status;

    public:
        User();
        User(QString, QString, QString, QString, QString);
        ~User();
};

What is the best approach for this?

eyllanesc

For a new type to be used in the signals you must register it using the macro: Q_DECLARE_METATYPE

user.h

#ifndef USER_H
#define USER_H

#include <QString>
#include <QMetaType>

class User
{
    [...]
};

Q_DECLARE_METATYPE(User)

#endif // USER_H

Then it is used as parameter of the accept signal in your case:

login.h

signals:
    void accept(const User & user);

Then you issue it when necessary:

User user("1", "2", "3", "4", "5");
emit accept(user);

To make it simple you can connect using a lambda function, but for this we create a method that you get to use in MainWindow:

mainwindow.h

public:
    void setUser(const User &user);

private:
    User mUser;

mainwindow.cpp

void MainWindow::setUser(const User& user)
{
    mUser = user;
    qDebug()<<mUser.toString();
}

main.cpp

int main(int argc, char *argv[]){
    QApplication a(argc, argv);
    Login l;
    l.createConnection();
    MainWindow w;
    l.show();
    QObject::connect(&l, &Login::accept, [&w](const User user){
        w.setUser(user);
        w.show();
    });
    QObject::connect(&w, &MainWindow::Logout, &l, &Login::show);
    return a.exec();
}

You can find a complete example in the following link.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

QT Creator C++ : Passing information from QDialog to MainWindow

PyQt: How to pass chosen Combobox value from QDialog to main window?

Passing data from opened window

Qt open dialog from main window

Qt Prevent QDialog from hiding

Passing Data from Custom UIView to Main VC

Passing data from Main layout to subpages in Nextjs

Passing data from a window back to ShellViewModel

passing key events from main window to iframe using jquery

How to get the data from the window to the main screen

qt creator how to get layout from main window?

Open second window from main with pyqt5 and qt designer

how to open a QDialog widget on the center position of the main window

How to send signals/variables between a QDialog and Main Window

Qt: SIGNAL and SLOT don't work in a custom QDialog window

Qt: passing objects to every window

Passing data to controller from Kendo UI window and data is empty

Qt button not appearing in main window

Add MarbleWidget into main window on QT

Qt slots and passing data

Passing of data from main page to php query inside a modal

Android - passing data from Service to main at unknown point in time

How to get data from a page back to the main window?

Passed data on selected row from dialog window to main page

Qt - Closing the program from a QDialog (Before MainWindow fully loaded)

C++ application: Passing a value from a form to the main window private variable

Python PyQt4 open from QDialog new QWidget window

How to Pass the data from Main Window to Child Window to display large data in table using AngulaJS

How to center a QDialog in QT?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive