View Full Version : QT4 scope problem
the_bis
29th January 2007, 15:59
This is a very newbie question :confused:
It' a basic problem of scope between objects.
In attachment the full example code.
I have the main object (qt4testMainWindow : public QMainWindow) and I have a secondary object (qt4testData: public QObject) created by the first one.
The main object build a UI of QMainWindow type; the second handle data but have to comunicate with the main object: it must call a main object's method.
So I've created the new secondary object passing it the reference to the main UI (see "main_window.cpp" file at line 30).
It's all ok if I have to change a UI value (see "qt4test_data.cpp" at line 30) from inside the secondary object.
My problem is: how can I invoke "qt4testMainWindow::changeVar" method from the
secondary object?
I tried the method you can see in "qt4test_data.cpp" at line 32 without success:
main_ui->changeVar ( 20 );
but I have a compiler error:
qt4test_data.cpp: In member function ‘void qt4testData::init()’:
qt4test_data.cpp:32: error: base operand of ‘->’ has non-pointer type ‘Ui::MainWindow’
Any help?
Thanks,
the_bis
wysota
29th January 2007, 16:56
Either use a dot (".") instead of an arrow ("->") or redesign so that you an use signals and slots without having a reference to the main window object.
the_bis
29th January 2007, 17:30
Either use a dot (".") instead of an arrow ("->") or redesign so that you an use signals and slots without having a reference to the main window object.
No luck :(
qt4test_data.cpp: In member function ‘void qt4testData::init()’:
qt4test_data.cpp:32: error: ‘class Ui::MainWindow’ has no member named ‘changeVar’
I'm becoming crazy...
I tried first the solution you suggest... but was too difficult for me.
I post now some sample code:
"main_window.cpp":
#include "main_window.h"
#include "qt4test_data.h"
qt4testMainWindow::qt4testMainWindow (QWidget *parent) : QMainWindow (parent)
{
ui.setupUi (this);
QObject::connect(ui.horizontalSlider, SIGNAL(valueChanged(int)), ui.spinBox, SLOT(setValue(int)));
connect (ui.pushButton2, SIGNAL(pressed()),
this, SLOT(slotPush2Pressed()));
init ();
}
void qt4testMainWindow::init ()
{
QString stringToSend = "TestString";
int integerToSend = 10;
qt4testData *myqt4testData = new qt4testData ( stringToSend , integerToSend , ui );
statusBar()->showMessage (tr("Starting..."));
}
void qt4testMainWindow::slotPush2Pressed ()
{
qDebug ("\nqt4testMainWindow::slotPush2Pressed\n");
}
void qt4testMainWindow::changeVar ( int newValue )
{
qDebug ("\nqt4testMainWindow::changeVar - newValue: %d\n" , newValue );
ui.spinBox->setValue ( newValue );
}
"main_window.h":
#include "ui_main_window.h"
#include <QtDebug>
class qt4testData;
class qt4testMainWindow : public QMainWindow
{
Q_OBJECT
public:
qt4testMainWindow ( QWidget *parent = 0 );
void changeVar ( int newValue );
signals:
private:
Ui::MainWindow ui;
void init ();
public slots:
void slotPush2Pressed ();
private slots:
};
"qt4test_data.cpp":
#include "qt4test_data.h"
qt4testData::qt4testData (QString& testString, int testNumber, Ui::MainWindow& main_ui_temp)
{
qDebug ("\nqt4testData::qt4testData -> constructor");
mytestString = testString;
mytestNumber = testNumber;
qDebug () << "\nqt4testData::qt4testData -> 'mytestString' variable: " << mytestString << "\n";
qDebug () << "\nqt4testData::qt4testData -> 'mytestNumber' variable: " << mytestNumber << "\n";
main_ui = main_ui_temp;
init ();
}
void qt4testData::init ()
{
qDebug ("\nqt4testData::init -> init");
main_ui.spinBox->setValue (80);
main_ui.changeVar ( 20 );
}
"qt4test_data.h":
#include <QtDebug>
#include "ui_main_window.h"
class qt4testData: public QObject
{
Q_OBJECT
public:
qt4testData (QString& testString, int testNumber, Ui::MainWindow& main_ui_temp);
private:
void init ();
Ui::MainWindow main_ui;
QString mytestString;
int mytestNumber;
};
Or can you tell me where to find documentation or an example to study?
Thanks,
the_bis
wysota
29th January 2007, 17:55
What is the type of main_ui? Where is it declared? You shouldn't do anything like:
main_ui = main_ui_temp;
If you're passing a reference, then you have to treat it like one. The line above creates an unnecessary copy of the ui. Since ui is not a widget this is not forbidden, but still not advised.
You're suffering from simple C++ errors which have nothing to do with Qt. I suggest you use signals and slots to interconnect the main form and that other object.
the_bis
29th January 2007, 20:47
[...] I suggest you use signals and slots to interconnect the main form and that other object.
Ok, thank you far your suggestion.
I think this is the correct solution...
It works :)
Can you see if it is all ok?
Thanks,
the_bis
"main_window.h":
#include "ui_main_window.h"
#include "qt4test_data.h"
#include <QtDebug>
class qt4testData;
class qt4testMainWindow : public QMainWindow
{
Q_OBJECT
public:
qt4testMainWindow ( QWidget *parent = 0 );
void changeVar ( int newValue );
signals:
private:
void init ();
Ui::MainWindow ui;
qt4testData * myqt4testData;
public slots:
void slotPush2Pressed ();
void mainSlot ( int newValue );
private slots:
};
"main_window.cpp":
#include "qt4test_data.h"
qt4testData::qt4testData ( QString& testString, int testNumber, QObject * parent )
{
mytestString = testString;
mytestNumber = testNumber;
qDebug () << "\nqt4testData::qt4testData -> 'mytestString' variable: " << mytestString << "\n";
qDebug () << "\nqt4testData::qt4testData -> 'mytestNumber' variable: " << mytestNumber << "\n";
connect ( this, SIGNAL ( secondarySignal ( int ) ), parent, SLOT ( mainSlot ( int ) ) );
init ();
}
void qt4testData::init ()
{
qDebug ("\nqt4testData::init\n");
emit ( secondarySignal ( 75 ) );
}
void qt4testData::secondarySlot ()
{
qDebug ("\nqt4testData::secondarySlot\n");
}
"qt4test_data.h":
#include <QtDebug>
class qt4testData: public QObject
{
Q_OBJECT
public:
qt4testData ( QString& testString, int testNumber, QObject * parent );
private:
void init ();
QString mytestString;
int mytestNumber;
signals:
void secondarySignal ( int newValue );
public slots:
void secondarySlot ();
};
"qt4test_data.cpp":
#include "qt4test_data.h"
qt4testData::qt4testData ( QString& testString, int testNumber, QObject * parent )
{
mytestString = testString;
mytestNumber = testNumber;
qDebug () << "\nqt4testData::qt4testData -> 'mytestString' variable: " << mytestString << "\n";
qDebug () << "\nqt4testData::qt4testData -> 'mytestNumber' variable: " << mytestNumber << "\n";
connect ( this, SIGNAL ( secondarySignal ( int ) ), parent, SLOT ( mainSlot ( int ) ) );
init ();
}
void qt4testData::init ()
{
qDebug ("\nqt4testData::init\n");
emit ( secondarySignal ( 75 ) );
}
void qt4testData::secondarySlot ()
{
qDebug ("\nqt4testData::secondarySlot\n");
}
wysota
29th January 2007, 23:01
If it works then it's ok.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.