10-16-2012 08:54 AM
Main:
h:
public slots:
void handleEmit();
cpp:
MyComponent *myComponent = new MyComponent();
bool res = QObject::connect(myComponent, SIGNAL(myFirstSignal()), this, SLOT(handleEmit()));
Q_ASSERT(res);
Q_UNUSED(res);
void Main::handleEmit()
{
//do something
}
MyComponent:
h:
class MyComponent
{
signals:
void myFirstSignal();
};
cpp:
MyComponent::MyComponent()
{
//EMIT THE SIGNAL
emit myFirstSignal();
}
I'm trying to emit a signal from a class (myComponent) to my Main app but am not having much luck, I've followed the tutorial on signals and slots but it still doesnt work. Am I missing something?
Thanks
Solved! Go to Solution.
10-16-2012 09:12 AM
i don't know if this is the way signals should be called but for me it works fine:
in the .h file
signals:
void myFirstSignal();and at the point you want to send the signal:
this->myFirstSignal();
10-16-2012 09:13 AM
The correct way is:
emit myFirstSignal();
This line for me is causing the problems:
bool res = QObject::connect(myComponent, SIGNAL(myFirstSignal()), this, SLOT(handleEmit()));
10-16-2012 09:28 AM
i changed it in my code and it still works fine.
and i changed my connect method to match yours but it still works fine.
do you get an error or anything?
10-16-2012 09:34 AM
no matching function for call to 'QObject::connect(MyComponent&, const char*, MyApp* const, const char*)'
Are you calling your signal from a seperate class like me?
10-16-2012 09:49 AM
yes i do.
the only difference I can see at the moment is that your classes don't extend another class and mine do
and connect expects a QObject for sender and receiver
and I don't have enough knollege about Qt to know for sure if a class automaticly extends QObject.
10-16-2012 09:53 AM
Could you do me a favour and post your class so I can take a look at it?
Thanks!
10-16-2012 09:59 AM
10-16-2012 10:06 AM - edited 10-16-2012 10:09 AM
I tried to filter my code for the usefull parts
MainWidget.h:
class MainWidget : public QWidget
{
Q_OBJECT
public:
explicit MainWidget(QWidget *parent = 0);
~MainWidget();
public slots:
void handleButton2();
void setValues(QString s);
private:
Ui::MainWidget *ui;
};
MainWidget.cpp:
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget)
{
//deze methoden creëerd een frame aan de hand van een xml file
ui->setupUi(this);
//een actie aan een knop hangen
connect(ui->pushButton_2, SIGNAL(pressed()), this, SLOT(handleButton2()));
}
void MainWidget::handleButton2(){
QVector<QString> *waarden = new QVector<QString>();
waarden->insert(waarden->size(), QString("man"));
waarden->insert(waarden->size(), QString("vrouw"));
radioButtonPopup *hoi = new radioButtonPopup();
hoi->addData(ui->label_3->text(), waarden);
hoi->exec();
bool res = QObject::connect(hoi, SIGNAL(done(QString)), this, SLOT(setValues(QString)));
}
void MainWidget::setValues(QString s){
ui->label_6->setText(s);
}
radioButtonPopup.h:
class radioButtonPopup : public QDialog
{
Q_OBJECT
public:
explicit radioButtonPopup(QWidget *parent = 0);
void addData(QString question, QVector<QString> *possible);
QString convertInt(int i);
signals:
void done(QString choise);
public slots:
void choisesMade();
};
radioButtonPopup.cpp:
radioButtonPopup::radioButtonPopup(QWidget *parent) :
QDialog(parent)
{
//layout voor de dialog aanmaken
layoutGrid = new QVBoxLayout;
setLayout(layoutGrid);
}
void radioButtonPopup::addData(QString question, QVector<QString> *possible){
//label om de vraag in te zetten
QLabel *vraag = new QLabel(question);
layoutGrid->addWidget(vraag);
//is nodig om te zorgen dat de lijst weer leeg. anders krijg je bij opnieuw invullen ook de vorige data erbij
buttons->clear();
//checkbox vector vullen met de radiobuttons door middel van een loop
for(int i = 0 ; i < possible->size() ; i++){
QRadioButton *c = new QRadioButton(possible->at(i));
buttons->insert(buttons->size(), c);
layoutGrid->addWidget(c);
}
//een knopje op een layout zetten zodat deze tevoorschijn komt
QPushButton *p = new QPushButton("ok");
layoutGrid->addWidget(p);
connect(p, SIGNAL(pressed()), this, SLOT(choisesMade()));
}
void radioButtonPopup::choisesMade(){
QString s = "";
for(int i = 0 ; i < buttons->size() ; i++){
if(buttons->value(i)->isChecked()){
s = buttons->value(i)->text();
break;
}
}
emit done(s);
this->setVisible(false);
this->close();
}
10-16-2012 10:29 AM
Thanks, could you let me knwo any includes you have?