Re: pointer at QMainWindow
What is Modify? If it is your class, you can implement a constructor that fits your needs and pass it the pointer you need.
Re: pointer at QMainWindow
Quote:
Originally Posted by
wysota
What is Modify? If it is your class, you can implement a constructor that fits your needs and pass it the pointer you need.
Yes, it is my class. How would the constructor be?
Code:
Modify::Modify(...){} //<-- ??
Then i'd like to have access at all the Widget elements into the ui_class.h.
Thanks
Re: pointer at QMainWindow
Quote:
Originally Posted by
mattia
Is this legal C++? :)
Re: pointer at QMainWindow
No, it isn't legal in C++ but i was making just an example and i didn't pay attention at the class name, next time i'll take care about it cos i know i can generate misconception :o
Re: pointer at QMainWindow
Make the constructor accept a pointer to QMainWindow (just like QWidgets accept a pointer to QWidgets) and store the pointer somewhere in Modify.
Re: pointer at QMainWindow
I'made a little example to explain me in the best way, I created a simple GUI with a menu and a label, when i click on the menu voice i'd like to change the text label. To do this i created a class doSmt.h that perform this work.
I want to pass a QMainWindow pointer to the class doSmt to have the access at all the attributes defined in ui_guiwid.h.
In the class guiwid i cant acces to them simply with ui.nomeAttribute, i'd like to do the same thing into doSmt class. Thx
ui_guiwid.h
Code:
#ifndef UI_GUIWID_H
#define UI_GUIWID_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QLabel>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QWidget>
class Ui_MainWindow
{
public:
{
if (MainWindow->objectName().isEmpty())
MainWindow
->setObjectName
(QString::fromUtf8("MainWindow"));
MainWindow->resize(420, 510);
actionChangeLabel
= new QAction(MainWindow
);
actionChangeLabel
->setObjectName
(QString::fromUtf8("actionChangeLabel"));
centralwidget
= new QWidget(MainWindow
);
centralwidget
->setObjectName
(QString::fromUtf8("centralwidget"));
label
= new QLabel(centralwidget
);
label
->setObjectName
(QString::fromUtf8("label"));
label
->setGeometry
(QRect(20,
10,
321,
21));
MainWindow->setCentralWidget(centralwidget);
menubar
->setObjectName
(QString::fromUtf8("menubar"));
menubar
->setGeometry
(QRect(0,
0,
420,
28));
menuFIle
= new QMenu(menubar
);
menuFIle
->setObjectName
(QString::fromUtf8("menuFIle"));
MainWindow->setMenuBar(menubar);
statusbar
->setObjectName
(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);
menubar->addAction(menuFIle->menuAction());
menuFIle->addAction(actionChangeLabel);
retranslateUi(MainWindow);
} // setupUi
{
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
#endif // UI_GUIWID_H
guiwid.h
Code:
#include <QtGui/QWidget>
#include "ui_guiwid.h"
{
Q_OBJECT
public:
guiwid();
~guiwid();
private slots:
void callDoStm();
private:
Ui::MainWindow ui;
};
guiwid.cpp
Code:
#include "guiwid.h"
#include "doSmt.h"
#include <QtGui/QWidget>
guiwid::guiwid()
{
ui.setupUi(this);
connect(ui.actionChangeLabel, SIGNAL(triggered()), this, SLOT(callDoStm()));
}
guiwid::~guiwid()
{
}
void guiwid::callDoStm(){
doSmt *ds = new doSmt(this);
ds->changeLabel();
}
doSmt.h
Code:
#include <QtGui/QWidget>
#include <QMainWindow>
class doSmt
{
public:
~doSmt();
public:
void changeLabel();
};
doSmt.cpp
Code:
#include "doSmt.h"
#include <QtGui/QWidget>
{
this->mainWindow = ui;
}
doSmt::~doSmt()
{
}
void doSmt::changeLabel()
{
// i can't access to the MainWindow attributes with mainWindow->
}
Re: pointer at QMainWindow
1. Do you need doSmt::changeLabel() to be a separate class? You can have the same functionality in guiwnd.
2. Your problem is strictly C++ related. The ui component is private, thus other objects don't have access to it. You can either declare the doSmt class friend to the guiwnd class (not recommended), move the ui component to public section or use multiple inheritance approach (not recommended) or provide public methods (they are called getters and setters) in guiwnd to access components of the ui (that's what Qt does and preferrs). Of course you'll also need to cast that QMainWindow pointer to guiwnd pointer to be able to access its fields and methods (or better yet make your constructor accept a pointer to guiwnd instead of QMainWindow).
Re: pointer at QMainWindow
Thanks for the explanation, no i don't need a separate class to change a label, i thought that when i'll work with more complex application maybe i'll need to get access at some elements defined into ui_guiwid.h and rather than pass all the elements at the classes that will modify them it would be better get the direct access through a pointer to main window.
I've solved it passed Ui::MainWindow ui; as pointer to doStm and i'm storing it into the class as private member. Do you think it is a smart way?
Thx
Re: pointer at QMainWindow
No, it's not a smart way - it breaks the Object Oriented Programming paradigm. First you make the ui private so that noone can access it and then you allow some other object to modify it behind your back. With Qt you should just use signals and slots, like so:
Code:
public:
ui.setupUi(this);
obj = new childClass;
connect(obj,
SIGNAL(changeLabelText
(const QString &)), ui.
someLabel,
SLOT(setText
(const QString &)));
}
~Parent(){}
private:
Ui::someUi ui;
childClass *obj;
};
Re: pointer at QMainWindow
I followed your useful suggestions ;) Thanks so much!