Thanks very much for the advice! The segmentation faults were because I was accidentally reinitialising my objects in the constructor after defining them in the header - that's now fixed - thanks faldżip.
I've got it working now so that the code passes messages from C to E. However the current implementation is pretty inefficient, because I have to set up new connections for each new instance of C (the Mirror class). I've done it this way because I'm not sure how objects talk to each other - this way, B creates lots of C, and can communicate back to A which knows all about D and E, so it seemed the easiest place to set up the connections.
The relevant parts of the code are shown below. I like the idea of what wagmare suggested, i.e. putting the connection code into D/E to listen to any signals from C. I don't know how to implement this though, as I'm not sure how to tell D/E that C exists, and how to listen to any instance of C.
Here's the relevant code:
A (MainView - instantiated once)
- B (DesignView - instantiated once)
--- C (Mirror - lots of these, created by user - the main part of the app)
- D (PropertyView - instantiated once)
--- E (line edit boxes on form)
The idea of the app, a tool for designing optical mirror/lens arrangements, is that the user sets up as many instances of C as they want, links them together in a user-defined order, and can click on any instance of C to reveal their properties in E.
[A] MainView.h
{
Q_OBJECT
public:
public slots:
private:
PropertyView *propertypane;
};
class MainView : public QWidget
{
Q_OBJECT
public:
MainView(QWidget *parent = 0);
public slots:
void changeID(const QString &);
private:
PropertyView *propertypane;
};
To copy to clipboard, switch view to plain text mode
[A] Mainview.cpp
{
DesignView *designpane = new DesignView(this);
propertypane = new PropertyView(this);
mainLayout->addWidget(designpane);
mainLayout->addWidget(propertypane);
setLayout(mainLayout);
}
void MainView
::changeID(const QString &message
) {
propertypane->setID(message);
}
MainView::MainView(QWidget *parent) : QWidget(parent)
{
QHBoxLayout *mainLayout = new QHBoxLayout;
DesignView *designpane = new DesignView(this);
propertypane = new PropertyView(this);
mainLayout->addWidget(designpane);
mainLayout->addWidget(propertypane);
setLayout(mainLayout);
}
void MainView::changeID(const QString &message)
{
propertypane->setID(message);
}
To copy to clipboard, switch view to plain text mode
[B] DesignView.h
{
Q_OBJECT
public:
};
class DesignView : public QWidget
{
Q_OBJECT
public:
DesignView(QWidget *parent = 0);
};
To copy to clipboard, switch view to plain text mode
[B] DesignView.cpp
{
Mirror *firstMirror = new Mirror(this,0,1);
grid->addWidget(firstMirror,1,0);
setLayout(grid);
// have to do this for each mirror if defined here - not ideal...
connect(firstMirror,
SIGNAL(setID
(const QString &)), parentWidget
(),
SLOT(changeID
(const QString &)) );
}
DesignView::DesignView(QWidget *parent) : QWidget(parent)
{
QGridLayout *grid = new QGridLayout;
Mirror *firstMirror = new Mirror(this,0,1);
grid->addWidget(firstMirror,1,0);
setLayout(grid);
// have to do this for each mirror if defined here - not ideal...
connect(firstMirror, SIGNAL(setID(const QString &)), parentWidget(), SLOT(changeID(const QString &)) );
}
To copy to clipboard, switch view to plain text mode
[C] Mirror.h
{
Q_OBJECT
public:
signals:
private:
int intID;
};
class Mirror : public QLabel
{
Q_OBJECT
public:
Mirror(QWidget *parent = 0);
signals:
void setID(const QString &);
private:
void mousePressEvent(QMouseEvent* event);
int intID;
};
To copy to clipboard, switch view to plain text mode
[C] Mirror.cpp
{
// code to set up unique ID number
intID = ...;
}
{
if ( event->buttons() == Qt::LeftButton )
{
// code to convert int to QString
strID = v.toString();
emit setID(strID);
}
}
Mirror::Mirror(QWidget *parent) : QLabel(parent)
{
// code to set up unique ID number
intID = ...;
}
void Mirror::mousePressEvent(QMouseEvent *event)
{
if ( event->buttons() == Qt::LeftButton )
{
// code to convert int to QString
QVariant v = intID;
strID = v.toString();
emit setID(strID);
}
}
To copy to clipboard, switch view to plain text mode
[D] PropertyView.h
class PropertyView
: public QWidget{
Q_OBJECT
public:
private:
QFormLayout *formLayout;
};
class PropertyView : public QWidget
{
Q_OBJECT
public:
PropertyView(QWidget *parent = 0);
void setID(QString strTest);
private:
QFormLayout *formLayout;
QLineEdit *idLineEdit;
};
To copy to clipboard, switch view to plain text mode
[D] PropertyView.cpp
{
formLayout = new QFormLayout;
formLayout->addRow("ID:", idLineEdit);
setLayout(formLayout);
}
void PropertyView
::setID(QString strID
) {
idLineEdit->setText(strID);
}
PropertyView::PropertyView(QWidget *parent) : QWidget(parent)
{
formLayout = new QFormLayout;
idLineEdit = new QLineEdit;
formLayout->addRow("ID:", idLineEdit);
setLayout(formLayout);
}
void PropertyView::setID(QString strID)
{
idLineEdit->setText(strID);
}
To copy to clipboard, switch view to plain text mode
Finally [E] is the QLineEdit instance (idLineEdit).
Any thoughts? Thanks again for the advice so far. : )
Bookmarks