Okay here is how I made it. The code works but I do not really understand why.
The class:
#ifndef STATIONCARD_H
#define STATIONCARD_H
#include <QLabel>
#include <QGroupBox>
#include <QLayout>
#include <QDebug>
Q_OBJECT
public:
StationCard();
int index;
bool on;
public slots:
void turnOff(int actIndex);
void sentToggle(bool on);
signals:
void toggled(int index);
private:
};
#endif // STATIONCARD_H
#ifndef STATIONCARD_H
#define STATIONCARD_H
#include <QLabel>
#include <QGroupBox>
#include <QLayout>
#include <QDebug>
class StationCard : public QGroupBox{
Q_OBJECT
public:
StationCard();
QLabel *propLabel;
int index;
bool on;
public slots:
void turnOff(int actIndex);
void sentToggle(bool on);
signals:
void toggled(int index);
private:
};
#endif // STATIONCARD_H
To copy to clipboard, switch view to plain text mode
#include "stationcard.h"
StationCard::StationCard(){
layout->addWidget(propLabel, 10, Qt::AlignRight);
setLayout(layout);
}
void StationCard::turnOff(int actIndex){
if(actIndex != index){
setChecked(false);
}
}
void StationCard::sentToggle(bool on){
qDebug() << "Signal comes.";
if(on){
emit toggled(index);
emit turnOff(index);
}
}
#include "stationcard.h"
StationCard::StationCard(){
QHBoxLayout *layout = new QHBoxLayout;
propLabel = new QLabel();
layout->addWidget(propLabel, 10, Qt::AlignRight);
setLayout(layout);
}
void StationCard::turnOff(int actIndex){
if(actIndex != index){
setChecked(false);
}
}
void StationCard::sentToggle(bool on){
qDebug() << "Signal comes.";
if(on){
emit toggled(index);
emit turnOff(index);
}
}
To copy to clipboard, switch view to plain text mode
And the wirering (I also added a signal to my main window: void turnOff(int index)
QObject::connect(stationVector
[k
],
SIGNAL(toggled
(bool)), stationVector
[k
],
SLOT(sentToggle
(bool)));
QObject::connect(stationVector
[k
],
SIGNAL(toggled
(int)),
this,
SLOT(on_stationCard_toggled
(int)));
QObject::connect(this,
SIGNAL(turnOff
(int)), stationVector
[k
],
SLOT(turnOff
(int)));
[...]
void MainWindow::on_stationCard_toggled(int index){
activeIndex = index;
emit turnOff(index);
qDebug() << activeIndex;
}
QObject::connect(stationVector[k], SIGNAL(toggled(bool)), stationVector[k], SLOT(sentToggle(bool)));
QObject::connect(stationVector[k], SIGNAL(toggled(int)), this, SLOT(on_stationCard_toggled(int)));
QObject::connect(this, SIGNAL(turnOff(int)), stationVector[k], SLOT(turnOff(int)));
[...]
void MainWindow::on_stationCard_toggled(int index){
activeIndex = index;
emit turnOff(index);
qDebug() << activeIndex;
}
To copy to clipboard, switch view to plain text mode
The first connection connects the basic toggle signal with my custom signal. I thought if I do this all cards would listen if a toggle signal is sent but since the code works I think only signal from card k is connected to the slot from card k. Why is this so? All cards are identical, how does the compiler know that only signal and slot from k should be used? The slot from k+1 is exactly the same!
The second connection connects the card to my main window and sends active card index.
The third connection connects my main window to the cards and tells them to un-select if they are not the active card.
Bookmarks