negatic
negatic

Reputation: 35

QT good global Variable / Object handling

I'm still new to QT and don't really know how to deal with global variables here.

I want to load a file in main() and store the data in Objects / Variables which I then need to access via functions like ::on_Button_clicked().

What are the best ways to have access to the Objects / Variables I set in the main function from the slots?

Upvotes: 1

Views: 723

Answers (1)

deW1
deW1

Reputation: 5660

In your header file you can declare them like this:

class frmMain : public QMainWindow
{
        Q_OBJECT

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

    private slots:
        void on_lineEdit_returnPressed();

    private:
        Ui::frmMain *ui; // <--
        QComboBox *comboBox; // <--
        QDialog *dialog; // <--
        QString test; // <--
};

And then define them in your .cpp and use them within the class:

void frmMain::on_lineEdit_returnPressed()
{
    comboBox = new QComboBox( );
    test     = "Hello";

    comboBox->addItem( test );
}

On top of that declaring variables outside any scope still makes them global like you know it from basic c++.

Upvotes: 3

Related Questions