PDA

View Full Version : pointer at QMainWindow



mattia
5th November 2007, 12:10
Hello, if i have created with QT Designer a class called ui_class.h with the main window layout and at the and i have declared this namespace:


namespace Ui {
class MainWindowClass: public Ui_MainWindowClass {};
}


then i've made class.h in this way


#include <QtGui/QWidget>
#include "modify.h"
class class : public QMainWindow
{
Q_OBJECT

public:
class();
~class();
private:
Ui::MainWindowClass ui;
Modify *modify;
};


and the class.cpp


#include <QtGui>
#include "class.h"


class::class()
{
ui.setupUi(this);
modify = new Modify(this);

}

server::~server(){}


how is the constructor in modify.h? cos i want to pass at the modify.h a pointer at the "MainWindowClass ui" to have access at all the widget that i have in ui_class.h.
Is it possibile?

wysota
5th November 2007, 13:59
What is Modify? If it is your class, you can implement a constructor that fits your needs and pass it the pointer you need.

mattia
5th November 2007, 14:10
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?


Modify::Modify(...){} //<-- ??

Then i'd like to have access at all the Widget elements into the ui_class.h.
Thanks

jpn
5th November 2007, 15:48
class class : public QMainWindow


Is this legal C++? :)

mattia
5th November 2007, 15:52
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

wysota
5th November 2007, 18:51
Make the constructor accept a pointer to QMainWindow (just like QWidgets accept a pointer to QWidgets) and store the pointer somewhere in Modify.

mattia
6th November 2007, 12:13
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


#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:
QAction *actionChangeLabel;
QWidget *centralwidget;
QLabel *label;
QMenuBar *menubar;
QMenu *menuFIle;
QStatusBar *statusbar;

void setupUi(QMainWindow *MainWindow)
{
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 = new QMenuBar(MainWindow);
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 = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);

menubar->addAction(menuFIle->menuAction());
menuFIle->addAction(actionChangeLabel);

retranslateUi(MainWindow);

QMetaObject::connectSlotsByName(MainWindow);
} // setupUi

void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
actionChangeLabel->setText(QApplication::translate("MainWindow", "Change Label", 0, QApplication::UnicodeUTF8));
label->setText(QApplication::translate("MainWindow", "TextLabel", 0, QApplication::UnicodeUTF8));
menuFIle->setTitle(QApplication::translate("MainWindow", "File", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui

#endif // UI_GUIWID_H


guiwid.h


#include <QtGui/QWidget>
#include "ui_guiwid.h"

class guiwid : public QMainWindow
{
Q_OBJECT

public:
guiwid();
~guiwid();
private slots:
void callDoStm();
private:
Ui::MainWindow ui;
};


guiwid.cpp


#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


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

class doSmt
{
public:
doSmt(QMainWindow *ui);
~doSmt();
public:
void changeLabel();
QMainWindow *mainWindow;
};


doSmt.cpp


#include "doSmt.h"
#include <QtGui/QWidget>

doSmt::doSmt(QMainWindow *ui)
{
this->mainWindow = ui;
}

doSmt::~doSmt()
{

}

void doSmt::changeLabel()
{
// i can't access to the MainWindow attributes with mainWindow->
}

wysota
6th November 2007, 13:04
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).

mattia
6th November 2007, 14:38
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

wysota
6th November 2007, 15:39
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:


class Parent : public QWidget{
public:
Parent(QWidget *parent=0) : QWidget(parent){
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;
};

mattia
7th November 2007, 11:34
I followed your useful suggestions ;) Thanks so much!