PDA

View Full Version : How to access the UI from another class ?



fitzy
25th October 2009, 11:27
Hi,

I'd like to access the user interface from another class.

If I call this in my main class "emulation", it works :

ui->LOG->append("text");

But i want to do the same thing from another class "robot" :


emulation::ui.LOG->append("text");

Unfortunately, it doesn't work !

I really need your help ! :(

squidge
26th October 2009, 09:22
Typically, ui is private, so you can't access it from another class. You could make it public, but a better approach would be to add a public method to your gui class to log events and let that communicate with the ui.

fitzy
26th October 2009, 15:16
ty
But i am a beginner and I don't really know how to call a function of my Main class from another class.

Thanks in advance. :cool:

nightghost
26th October 2009, 15:31
void MyClass::method() {

AnotherClass* another_object = new AnotherClass();
another_object->anotherMethod();
}

fitzy
26th October 2009, 17:00
Actually it doesn't work :(

My program runs but never shows up.

high_flyer
26th October 2009, 17:03
Show the relevant code please.

fitzy
26th October 2009, 17:17
robotShooter.cpp :


class koala
{

public:
void log1(){
robotShooter* another_object = new robotShooter();
another_object->LOG("text");
}

};

void robotShooter::LOG(QString text){
ui->LOG->append(text);
}

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

koala *KO = new koala();
KO->log1();

}


robotShooter.h


#ifndef ROBOTSHOOTER_H
#define ROBOTSHOOTER_H

#include <QtGui/QMainWindow>

namespace Ui
{
class robotShooter;
}

class robotShooter : public QMainWindow
{
Q_OBJECT

public:
robotShooter(QWidget *parent = 0);
~robotShooter();

void LOG(QString);

//private:
Ui::robotShooter *ui;
};

#endif // ROBOTSHOOTER_H

high_flyer
26th October 2009, 17:34
Amm... what it is you are trying to do?

Your code is recursive:
your robotShooter is creating a koala object, which in turn the only thing it does is create a new robotShooter (and sends a string to LOG()), and the cycle goes on...

fitzy
26th October 2009, 17:47
Amm... what it is you are trying to do?

Your code is recursive:
your robotShooter is creating a koala object, which in turn the only thing it does is create a new robotShooter (and sends a string to LOG()), and the cycle goes on...

Well, i'm trying to add some text in a "text browser".

fatjuicymole told me :

add a public method to your gui class to log events and let that communicate with the ui.

that's what I'm trying to do. ;)

squidge
26th October 2009, 18:42
class CA {
public:
void log (QString *str);
};

class CB {
public:
void log (QString *str);
};

CA firstclass;
CB secondclass;

void CA::log(QString *str) {
secondclass.log("foo");
}

void CB::log(QString *str) {
ui->LOG->append(str);
}


You can also have firstclass and secondclass as pointers by using new to initialise them and use -> instead of '.'

fitzy
26th October 2009, 19:06
I don't get it.

I have pasted your example (by the way, I think you meant CA::log(QString *str) not A::log(QString *str) ) but it doesn't work.

There are many errors :


ISO C++ forbids declaration of `log' with no type
CA::log(QString*)' does not match any in class `CA'
ISO C++ forbids declaration of `log' with no type
etc

wysota
26th October 2009, 19:11
fatjuicymole told me :

I will tell you this: get at least a glimpse of knowledge of C++ before doing more advaced things. Looking up "Thinking in C++" is a good place to start.

squidge
26th October 2009, 21:30
Apologies, thats what you get for rush typing, but you get the idea.

If you don't understand it you need to buy a book on C++ programming. You really need to understand C++ and OOP before working with Qt.

tokimay
31st January 2016, 17:49
function_in_class_A()
{
Class_B_Object.function_in_class_B(*ui->QLineEdit); // send qwidget to other class
}

function_in_class_B(QLineEdit &some_name)
{
some_name.setText("string_value"); // acces to qwidget in class B
}

d_stranz
31st January 2016, 21:23
Geez, not only do you bring back from the dead a 6+ years-old post, you post an absolutely terrible solution that is completely counter to what Qt signals and slots are all about.

You should never, ever implement code like this in a Qt app.

If you have two classes where one of them needs to send information that updates the UI on the other, then you do it this way:

1 - Implement a slot on the UI class:



void MyUIClass::updateText( const QString & newText )
{
ui->textEdit->setText( newText );
}


2 - Implement a signal in the class that needs to send the text to the UI class:



signals:
void sendText( const QString & newText );


3 - Connect the signal to the slot:



connect( myClass, SIGNAL( sendText( const QString & ) ), myUIClass, SLOT( updateText( const QString & ) ) );


4 - and every time "myClass" needs to display a new message on the UI class, it simply calls:



emit sendText( "Here is new text" );

jps3001
7th July 2016, 12:53
d_stranz

I really appreciated your explanations in this thread but have few questions related:

How could you use a Class (Myclass) to initiate the signal as follows?

connect( myClass, SIGNAL( sendText( const QString & ) ), myUIClass, SLOT( updateText( const QString & ) ) );

In which QT program did you insert this Qt code (connect)?

Thks in advance,

d_stranz
7th July 2016, 19:41
If you don't understand something as basic to Qt as how to implement signals and slots, then you should spend some time studying the Qt documentation on them (http://doc.qt.io/qt-5/signalsandslots.html), and look at some of the QWidget examples (http://doc.qt.io/qt-5/examples-widgets.html), like the Calculator example.

jps3001
9th July 2016, 12:10
I do not have problem with signals and slots which I used quite often in the Mainwindow in my apps.
My concern is how to generate signals and slots between different classes.

I figured out that 'myClass' in your connect example is actually an object but can't find what 'myUIClass' refers to in your example, I tried MainWindow but did not work.

I wrote the connect function in the secondary class as follows:

QObject::connect(objetA, SIGNAL(SendPlot()),MainWindow, SLOT(addPlot()) );

but MainWindow is not recognized despite the following includes:

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

Your help appreciated !

anda_skoa
9th July 2016, 12:40
In which QT program did you insert this Qt code (connect)?

The connect() call can be in any code where you have access to both the sender and the receiver object.
The other two arguments are basically just strings.



My concern is how to generate signals and slots between different classes.

No difference, really.



I figured out that 'myClass' in your connect example is actually an object but can't find what 'myUIClass' refers to in your example, I tried MainWindow but did not work.

It refers to a different object than the one "myClass" refers to.
Both these arguments are just pointers to QObject.



I wrote the connect function in the secondary class as follows:

QObject::connect(objetA, SIGNAL(SendPlot()),MainWindow, SLOT(addPlot()) );

but MainWindow is not recognized despite the following includes:

Do you have a QObject pointer variable named MainWindow?
Given its capitalization it looks awefully like the name of a class.

Cheers,
_

jps3001
15th July 2016, 12:43
You are right, Mainwindow is a Class and not an Object

However I can't figure out how to implement the following:

I need to add a Widget to ui->verticalLayout->addWidget(plot0)

as you know ui is private, I intend to use connect to access a SLOT which will add this Widget(plot0)

but I do not have an Object available in ui.

Should I have to create a dummy object just for this ?

I'm really puzzled !

anda_skoa
15th July 2016, 14:32
Aside from the fact that it is not very customary to send a QWidget pointer via a signal, the standard rules for signal/slot connects apply:

You need to have access to both the sender and the receiver pointer at the place where you put the connect.

So what good would a "dummy object" do?

Forget about Qt or signal/slots until you have a place where you have access to both pointers.
Basic C++.

Cheers,
_

jps3001
20th July 2016, 14:01
No help available ??

anda_skoa
20th July 2016, 15:21
There are plenty of C++ tutorials online, some of them should cover object creation, passing data through methods, storing data in variable and members.

Cheers,
_