Why cant I pass the "ui" variable as a pointer?



  • The below program is a simplified example of what I'm trying to accomplish.
    The actual program in which the code is used is much more complex.

    The below example code consist of a single main window containing a button and a label.
    I Have a Object called "StatsObj" of the type "StatsClass" that contains statistical data.
    When I click on the button the statistics must be reset and the "ui" label must be updated from within the "StatsObj". For this I want to pass the "ui" variable to the "StatsObj".

    I Have tried to pass "ui" as a pointer to "StatsObj" but this does not seem to work for some reason.
    I Know the best way is to do so using Signals and Slots. But in the actual program where I want to use this code there are many Labels and Objects, and all of the Objects need to be able to be able to access the "ui" variable.

    My Question:
    Why cant I pass the "ui" variable as a pointer to the "StatsObj"?
    And is there a easier why to access the "ui" vairable directly from all objects without having to write Signals and Slots for each and every one of them.

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "statsclass.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    StatsClass StatsObj;
    

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "statsclass.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    //Need to pass the "ui" vairable to "StatsObj" so it can update information on the MainWindow
    StatsObj.ResetAllStats();
    }
    @



  • [quote]I Have tried to pass “ui” as a pointer to “StatsObj” but this does not seem to work for some reason.[/quote]
    How exactly it does not work?
    Are there compilation errors?
    Are there run-time exceptions?


  • Moderators

    -The variable holds an object in the Ui namespace. That namespace is private, and only visible to your MainWindow class. You cannot pass that variable to other classes.-

    EDIT: Sorry, that was the wrong diagnosis. Please show us the code you tried.

    Anyway, also remember that MainWindow and Ui::MainWindow are two different classes.



  • the UI pointer is private. You can make it public and then pass the this pointer to your object.


  • Moderators

    [quote author="creeveshft" date="1419411113"]the UI pointer is private. You can make it public and then pass the this pointer to your object.[/quote]No, that's not it. The MainWindow is allowed to pass its private member variables to any function that it calls.

    achmed needs to provide more details (preferably code) about what he has tried.



  • Why not just pass the pointer of the MainWindow instead of the ui pointer?



  • Hi. Sorry for replying so late. I was on holiday overseas and only got back to work today.

    I Did not save my previous attempts at trying to pass the ui variable.
    But here is some new code to show what I'm trying to accomplish.
    This example does not compile.

    Remember: My main goal is to be able to access the ui varaible from sub classes. Maybe there is an easier more code efficient way that I'm not thinking of right now.

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "myclass.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MyClass *MyObject;

    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

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

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    MyObject = new MyClass(this,ui);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    @#ifndef MYCLASS_H
    #define MYCLASS_H

    #include <QObject>
    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MyClass : public QObject
    {
    Q_OBJECT
    public:
    explicit MyClass(QObject *parent = 0, Ui::MainWindow *ui_pointer = 0);

    signals:

    public slots:

    };

    #endif // MYCLASS_H
    @

    @#include "myclass.h"

    MyClass::MyClass(QObject *parent, Ui::MainWindow *ui_pointer) :
    QObject(parent)
    {
    ui_pointer->QLabel->SetText("abc");
    }
    @


  • Moderators

    Hi,

    Please describe exactly what doesn't work, and what error messages you get.



  • Ok. Thanks for your patience. The code does not compile.
    Here are the errors I get:

    @X:\MTEK\Programming\Delete\PassUi2\PassUi\myclass.cpp:-1: In constructor 'MyClass::MyClass(QObject*, Ui::MainWindow*)':

    X:\MTEK\Programming\Delete\PassUi2\PassUi\myclass.cpp:6: error: invalid use of incomplete type 'class Ui::MainWindow'
    ui_pointer->QLabel->SetText("abc");
    ^

    X:\MTEK\Programming\Delete\PassUi2\PassUi\myclass.cpp:1: In file included from ..\PassUi\myclass.cpp:1:0:

    X:\MTEK\Programming\Delete\PassUi2\PassUi\myclass.h:8: error: forward declaration of 'class Ui::MainWindow'
    class MainWindow;
    ^@



  • Missing #include "ui_mainwindow.h" in your MyClass file perhaps?


  • Moderators

    [quote]invalid use of incomplete type 'class Ui::MainWindow'[/quote]That means you didn't include the header which defines the Ui::MainWindow class structure. You need #include "ui_mainwindow.h"

    This is a common error message in C++, so remember it well.



  • Ok. Thank you very much. Problem solved.


Log in to reply
 

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