Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to set QDate global variables



  • Hello everyone, this is my first time here to ask something.
    I have some editDate in ui, in GUI, and it is troublesome to set a QDate variable for every declaration.
    to Explain more clearly, I have the following:
    void MainWindow::on_pushButtonTestDate_clicked()
    {
    QDate A = ui->dateEditA->date();
    QDate B = ui->dateEditB->date();
    // ........some codes
    }

    void MainWindow::on_pushButtonTestDate2_clicked()
    {
    QDate A = ui->dateEditA->date();
    QDate B = ui->dateEditB->date();
    // ........some codes

    }

    My question is:
    How to make those QDate A, QDate B etc. global variable, define one time somewhere and can be used in every "void MainWindow::...."?

    Not quite sure if I expressed easily to be understood or not.
    But this problem really really stuck me for a lot of days, and made me not able to move on for the work....

    Thank You All!


  • Lifetime Qt Champion

    Hi and welcome
    To make a variable available to all function of mainwindow ,
    you just need to make it part of the class.
    so in mainwindow.h
    class Mainwindow:: public QMianwindow {
    private:
    QDate A;
    QDate b;
    };

    Its called a member variable. and is "global" to all functions of the class.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    To add to @mrjj, you will also have to keep them updated, which is easily done using signals and slots.


Log in to reply