Class2 is a set of functions (only) to do with I/O and disk/file manipulation.
OK, so they need to know something about which file to open or something like that based on a choice made in a combo box on the main window form? And presumably you are creating a class2 instance from within your mainwindow class somewhere?
Then I would derive class2 from QObject (making sure not to forget to add the Q_OBJECT macro at the top of the class definition). When you create it, you can choose to make the mainwindow instance the parent or not. In any case, if you need only one of them, be sure you implement the creation in such a way that you don't end up creating a new one on every button click or whatever. It's basically the same as the last code snippet I posted, changing the base class for class2:
// class2.h
#include <QObject>
#include <QString>
{
Q_OBJECT
public:
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 <QObject>
#include <QString>
class class2 : public QObject
{
Q_OBJECT
public:
class2( QObject * parent ) : QObject( 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
You still retain the encapsulation of your I/O methods in class2, plus you add some encapsulation and separation of the mainwindow UI objects. All mainwindow needs to know about class2 is that it has a setCurrentText slot, and class2 doesn't know anything at all about mainwindow. If class2 needs to notify mainwindow when it has finished some I/O operation, then give it a signal to pass information that mainwindow can also connect up to .
This one of the great things about Qt's signals and slots mechanism. You could completely replace the internals of class2 while retaining the signals and slots, and mainwindow wouldn't have a clue (or need to know). Likewise, *anything* that has a signal with a const QString & argument could be connected to class2's slot, and it wouldn't know or care.
Bookmarks