[SOLVED] passing Userinput from one cpp as variable to other cpp



  • Hello again, :)
    i am stuck to a maybe small issue, ...

    i got 5 files

    • login.h
    • mainwindow.h
    • main.cpp
    • login.cpp
    • mainwindow.cpp

    in the login.cpp i got a lineEdit field where the user can input a username and declares it as a variable 'username'.

    in the mainwindow.cpp i want to use this variable 'username' but i don't get it to work.

    the errors are mostly like:
    symbol(s) not found for architecture x86_64
    linker command failed with exit code 1 (use -v to see invocation)
    or ...

    can someone maybe tell me where i am thinking wrong?

    thanks for any help or advice,
    Babu

    here the code:

    login.h

    @
    #ifndef LOGIN_H
    #define LOGIN_H

    #include <QMainWindow>
    #include <QDebug>
    #include <QMessageBox>
    namespace Ui {
    class login;
    }

    class login : public QMainWindow{
    Q_OBJECT

    public:
    explicit login(QWidget *parent = 0);
    ~login();
    signals:
    public slots:
    void on_pushButton_login_clicked();
    private:
    Ui::login *ui;
    };

    #endif // LOGIN_H
    @

    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QDialog>
    #include "login.h"

    namespace Ui {
    class mainwindow;
    }

    class mainwindow : public QDialog{
    Q_OBJECT

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

    private:
    Ui::mainwindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    main.cpp:
    @
    #include "login.h"
    #include <QApplication>

    int main(int argc, char *argv[]){
    QApplication a(argc, argv);
    login w;
    w.show();
    return a.exec();
    }
    @

    login.cpp:
    @
    #include "login.h"
    #include "ui_login.h"
    #include "mainwindow.h"

    login::login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::login){
    ui->setupUi(this);
    login::~login(){
    delete ui;
    }
    QString username; // also tried 'extern QString username' but then i get other errors ???
    void login::on_pushButton_login_clicked(){
    QString password;
    username=ui->lineEdit_username->text(); // here is the variable i want to pass
    password=ui->lineEdit_password->text();

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    ......
    ...
    .
    if (!db.open()){
    ...
    }
    else{
    ...

        mainwindow mainwin;
        mainwin.setModal(true);
        mainwin.exec&#40;&#41;;
    }
    

    }

    @

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

    mainwindow::mainwindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::mainwindow){
    ui->setupUi(this);
    ui->label_username->setText(username); // THIS IS THE PART WHERE I WANT TO CALL THE VARIABLE 'username'
    }
    mainwindow::~mainwindow(){
    delete ui;
    }
    @


  • Moderators

    You can't just grab this or that variable from whatever file and stick an extern when something doesn't compile. You need to design a proper interface.

    First of all why is username a global variable? Make it a member of the login class:
    @
    //login.h

    class login : public QMainWindow {
    //...
    QString username;
    };
    @
    Next you need a way to pass that to the mainwindow class, eg.
    @
    //mainwindow.h
    class mainwindow : public QDialog {
    //...
    public:
    void setUserName(const QString& name);
    };

    //mainwindow.cpp
    void mainwindow::setUserName(const QString& name) {
    ui->label_username->setText(name);
    }
    @
    With that in place you can pass the username from login to mainwindow:
    @
    //...
    else{
    //...
    mainwindow mainwin;
    mainwin.setModal(true);
    mianwin.setUserName(username);
    mainwin.exec();
    }
    @

    Also please reconsider your class names. You've got a login dialog that is QMainWindow and mainwindow that is a QDialog... something's not right here ;)



  • ok - thank you for your help -
    i am going through all of it -
    this might take some time - but i try to do it all correct - thanks
    babu



  • thanks i just have to figure out the problem about the QMainwindow, Qdialog ...

    is there anything i can read about why QMainwindow/ Qwidget/QDialog is the better choice?


  • Moderators

    Well I would say the official documentation is the best place to start.

    But to get you on track some common sense:
    QWidget is a base for any type of widget - be it window, button, frame or list. You rarely use it directly. Usually as a base when you design your own widget or when you just want to group some other set of widgets under a common parent. You can also use it as a very basic window that doesn't have any special functions.

    QDialog is a specialization of a QWidget used for, well... dialogs :) So any time you need to sorta break from normal run of a program to chat with a user you use it. It has special members to make that easier like accept() and reject() that let you know what user "said". They are usually shown modally ie. block the rest of the app untill you close them.

    QMainWindow is another specialization of QWidget and, as the name suggests, it's used for the main window of your app. It has special functionality for common things in such main windows, for example movable docking widgets and toolbars.

    The rule of thumb is - if you need just a simple place to put a few buttons - use a QWidget. When you need fancier stuff like docks or toolbars - use QMainWindow(there should usually be only one in an app). When you need to chat with a user (settings window, login screen, progress window etc.) use QDialog.



  • thank you very much - i guess i restart the project now with better knowledge
    babu


Log in to reply
 

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