Why cant I pass the "ui" variable as a pointer?
-
[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? -
-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.
-
[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_OBJECTpublic:
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");
}
@ -
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?
-
[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.
-