I've got to check out the meaning of (make the instance of your mainwindow class visible to "class2").
In the main() method of your app, you should have something like this, right?
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
mainWindow mainWin;
mainWin.show();
return app.exec();
}
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
mainWindow mainWin;
mainWin.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
"mainWin" is an instance of the mainwindow class. In this case, there is only one instance of it in your application. You need to make a pointer to this instance visible to "class2" if you want to be able to call any of the mainwindow methods.
You don't explain what "class2" is or how you create an instance of it. If it is being created within some method of mainwindow (like the constructor, maybe), then you can simply give the instance of class2 a pointer to the mainwindow instalce when you create it:
// class2.h
#include "mainwindow.h"
class class2 : public QSomeWidget
{
Q_OBJECT
public:
class2
( QWidget * parent
) : QSomeWidget
( parent
), mpMainWind
( 0 ) {}
void setMainWindowPointer( mainwindow * pMW ) { mpMainWind = pMW; }
private:
mainwindow * mpMainWind;
}
// mainwindow.cpp
class2 * mainwindow::createClass2()
{
class2 * pClass2 = new class2( this );
pClass2->setMainWindowPointer( this );
return pClass2;
}
// class2.h
#include "mainwindow.h"
class class2 : public QSomeWidget
{
Q_OBJECT
public:
class2( QWidget * parent ) : QSomeWidget( parent ), mpMainWind( 0 ) {}
void setMainWindowPointer( mainwindow * pMW ) { mpMainWind = pMW; }
private:
mainwindow * mpMainWind;
}
// mainwindow.cpp
class2 * mainwindow::createClass2()
{
class2 * pClass2 = new class2( this );
pClass2->setMainWindowPointer( this );
return pClass2;
}
To copy to clipboard, switch view to plain text mode
This is redundant, though - the parent QWidget of the class2 instance is already the mainwindow instance, so you don't need to store an extra pointer to it. However, if class2 is being created as a child of some other widget, then you will need to store the pointer as shown.
But this stinks as far as a design goes. Widgets in a program shouldn't really have to know that much about each other or keep pointers to each other as part of their data. Qt's signals and slots mechanism was designed to allow different parts of a program to communicate information between them without the parts having to know very much about each other.
So instead of class2 "pulling" the text from mainwindow, you could connect a slot in class2 (like, setCurrentText( const QString & )) to the QComboBox::currentTextChanged() signal from the combobox in the mainwindow UI. You do this when you create your instance of class2:
// class2.h
#include <QString>
class class2 : public QSomeWidget
{
Q_OBJECT
public:
class2
( QWidget * parent
) : QSomeWidget
( parent
) {}
public slots:
void setCurrentText
( const QString & text
) { mCurrentText
= text;
}
private:
}
// mainwindow.cpp
class2 * mainwindow::createClass2()
{
class2 * pClass2 = new class2( this );
connect ( myCombo,
SIGNAL( currentTextChanged
( const QString & ) ), pClass2,
SLOT( setCurrentText
( const QString & ) ) );
// If you want the class2 text to be updated any time the combobox is clicked, not just when the selection changes, then:
connect ( myCombo,
SIGNAL( activated
( const QString & ) ), pClass2,
SLOT( setCurrentText
( const QString & ) ) );
return pClass2;
}
// class2.h
#include <QString>
class class2 : public QSomeWidget
{
Q_OBJECT
public:
class2( QWidget * parent ) : QSomeWidget( parent ) {}
public slots:
void setCurrentText( const QString & text ) { mCurrentText = text; }
private:
QString mCurrentText;
}
// mainwindow.cpp
class2 * mainwindow::createClass2()
{
class2 * pClass2 = new class2( this );
connect ( myCombo, SIGNAL( currentTextChanged( const QString & ) ), pClass2, SLOT( setCurrentText( const QString & ) ) );
// If you want the class2 text to be updated any time the combobox is clicked, not just when the selection changes, then:
connect ( myCombo, SIGNAL( activated( const QString & ) ), pClass2, SLOT( setCurrentText( const QString & ) ) );
return pClass2;
}
To copy to clipboard, switch view to plain text mode
Bookmarks