Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] passing Userinput from one cpp as variable to other cpp

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    babuschkainyouface
    wrote on last edited by
    #1

    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;
    }
    @

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ;)

      1 Reply Last reply
      0
      • B Offline
        B Offline
        babuschkainyouface
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • B Offline
          B Offline
          babuschkainyouface
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • Chris KawaC Online
            Chris KawaC Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              babuschkainyouface
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved