PDA

View Full Version : Form , set object externally



giacomelli.fabio
27th December 2009, 13:33
Hi

A very simple question , but I don't understand how to do it.

I'm using QTCREATOR. I created a form with designer.

Now I need to do some operation to this form by a new external *.ccp file.

I put Ui::Main *ui; public

Main::Ui->object->setText("mmmm" is visible but I get an error " Invalid use of non-static member"

How to do it??

In the forum nothig helped me

Coises
27th December 2009, 15:00
You need ui (the variable — a specific instance of your Ui class) rather than Main::Ui as the first pointer. To get that, you must have access to the class in which you declare ui.

If you are not in some way communicating the address of the object, or of something that contains it, to your new code, then you cannot access it.

giacomelli.fabio
27th December 2009, 15:35
Mhhmmmh :confused:
I have


#ifndef PRINCIPALE_H
#define PRINCIPALE_H

#include <QMainWindow>
#include <QtGui/QWidget>
#include <QtNetwork/QAbstractSocket>

class QSslSocket;
class QListWidgetItem;

namespace Ui {
class Principale;
}

class Principale : public QMainWindow {
Q_OBJECT

public:
Principale(QWidget *parent = 0);
~Principale();
//........ omitted code
Ui::Principale *ui;

protected:
private:
QSslSocket *socket;
private slots:
void on_lineEdit_returnPressed();
//... omitted code
};

#endif // PRINCIPALE_H



In the first post I wrote Main .... but I tried Principale::ui::abc->setText("xyz") ....
it's recognized ( autocompletition works ) but I get the error "Invalid use of non-static member"

in new *.cpp I wrote



#include <ui_principale.h>
#include <principale.h>


Now to access for example to a label named "abc" ???? i feel a little bit stupid :o

Thank you in advance

Coises
27th December 2009, 16:48
Somewhere, probably in main, you instantiate and show Principale; that is, you have code like:


// one way:
//
Principale primaryWindow;
primaryWindow.show();
//
// or, another way:
//
Principale* primaryWindowPointer = new Principale();
primaryWindowPointer->show();


In your new code, you need:


// one way:
//
primaryWindow.ui->abc->setText("xyz");
//
// or, another way:
//
primaryWindowpointer->ui->abc->setText("xyz");


How you get “primaryWindow” or “primaryWindowPointer” to your new code depends on how you call the new code. If it’s called from main (or whatever routine creates and shows an instance of Principale), pass the same variable as an argument to your code. If it’s called from a member function of Principale, pass the this pointer.

You could also pass primaryWindow.ui->abc directly to your new code, if it only needs that one object.

There are any number of ways accomplish this, but somehow your new code must get a pointer to the relevant instance of abc to able to apply setText.

giacomelli.fabio
27th December 2009, 17:21
This is my main (created by QT creator)



#include <QtGui/QApplication>
#include "principale.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Principale w;
w.show();
return a.exec();
}


w AND a are not recognized in my new code ....

If I try to put in my new code



Principale primaryWindow;
primaryWindow.ui->setText("xyz");


The error does not shows up anymore but Label doesn't change :crying:

Coises
27th December 2009, 17:35
If I try to put in my new code



Principale primaryWindow;
primaryWindow.ui->setText("xyz");


The error does not shows up anymore but Label doesn't change

Do you understand why the label doesn’t change? Consider it for a little while, then tell me what you think is the reason. (Hint: your confusion has nothing to do with Qt; it’s something rather important about C++ that you’re missing.)

giacomelli.fabio
27th December 2009, 17:57
Ah, ok that was because of I wrote it by hand in the forum ... i always use auto-completion so it's very difficult i do so big errors . I'm tired now , I will not continue.

I think I need to study a little bit... I used gambas ( a VB clone for Linux) learning it directly .... with C++ and QT it's impossible ...

Thank you ( I had put all my code into primary *cpp :D )

ChrisW67
27th December 2009, 21:05
Here is a hint: your lineEdit widget is a member variable, probably one of many, in the instance pointed to by ui (built and set up by Designer code). See what you can do with:
ui->lineEdit->

giacomelli.fabio
30th December 2009, 12:14
Similar but different from what I needed before ... I think it's a c++ question .... but if you have 5 minutes please help me

I did an additional form ... I declared it :



#include <dialog.h>
#include <ui_dialog.h>
Dialog2 myDialog;
myDialog.show();


and it's ok. I can use its slots and its form objects

NOW

I need also to use FROM myDialog some objects from my first dialog

If I try to #include <firstdialog.h> into dialog2.h ( so I could declare FirstDialog myDialog2 ) i get some errors

it's very difficult find in google or into forum an answer ...

squidge
30th December 2009, 15:29
You probably don't have a guard in your include file to prevent multiple inclusion. Eg.



#ifndef FIRSTDIALOG_H
#define FIRSTDIALOG_H

blah blah
#endif

giacomelli.fabio
31st December 2009, 13:03
No it's OK ... it was included by qtcreator ...

but I think it's a similar problem