PDA

View Full Version : Qt signals slots again



bmpix
5th December 2011, 19:12
Hi,
A really newbie question.


QPushButton* buttonRun = new QPushButton(QString("Run"));

QGridLayout* layout_ = new QGridLayout;
layout_->addWidget(buttonRun,1,0);
widget->setLayout(layout_);

connect(buttonRun,SIGNAL(clicked()),this,SLOT(runA lgorithm()));


connect() returns true, but the slot is never called. I'm pretty sure the signal is emitted, because



connect(buttonRun,SIGNAL(clicked()),qApp,SLOT(abou tQt()));

works. I also think that the slot is defined properly, because if I do something like:


QObject::connect(this,SIGNAL(runThisBastard()),thi s,SLOT(runAlgorithm()));
emit runThisBastard();

the slot is called properly.

Just in case here's my definition of the class:


class A: public QObject, public IHasPropertiesPage
{
Q_OBJECT
public:
....
public slots:
void runAlgorithm ();
...
};


Any help would be greatly appreciated.

Lykurg
5th December 2011, 21:15
Based on what you wrote I can't see an error. So could you please make a minimal, compilable example reproducing your problem.

bmpix
5th December 2011, 21:50
Here you go. The full solution (VS 2010) is attached, I paste the code here for convenience. Thanks for help!



//A.h
#ifndef _A_H_
#define _A_H_

#include <qobject>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qlineedit.h>
#include <QtGlobal>
#include <qapplication.h>

#include "IHasPropertiesPage.h"

class A: public QObject, public IHasPropertiesPage
{
Q_OBJECT
public:
A() {};
void showProperties(QWidget* widget);
signals:
void runThisBastard();
public slots:
void run();
private:
QLineEdit* edit_;
};

#endif




//A.cpp
#include "A.h"

void A::showProperties(QWidget* widget)
{
edit_ = new QLineEdit(QString::number(0.1));
QPushButton* buttonRun = new QPushButton(QString("Run"));


//connect(this,SIGNAL(runThisBastard()),this,SLOT(ru n()));
//emit runThisBastard();
QGridLayout* layout_ = new QGridLayout;
layout_->addWidget(edit_,0,0);
layout_->addWidget(buttonRun,1,0);
widget->setLayout(layout_);
//connect(buttonRun,SIGNAL(clicked()),qApp,SLOT(abou tQt()));
connect(buttonRun,SIGNAL(clicked()),this,SLOT(run( )));
}

void A::run()
{
int i = 0; //just do something, I set a breakpoint here
}




//tempqt.cpp
#include "tempqt.h"
#include "A.h"
tempQt::tempQt(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

A a;
a.showProperties(ui.centralWidget);
}

tempQt::~tempQt()
{}




//tempqt.h
#ifndef TEMPQT_H
#define TEMPQT_H

#include <QtGui/QMainWindow>
#include "ui_tempqt.h"

class tempQt : public QMainWindow
{
Q_OBJECT
public:
tempQt(QWidget *parent = 0, Qt::WFlags flags = 0);
~tempQt();
private:
Ui::tempQtClass ui;
};

#endif // TEMPQT_H




//IHasPropertiesPage.h
#ifndef _IHASPROPERTIESPAGE_H_
#define _IHASPROPERTIESPAGE_H_

#include <qwidget.h>

class IHasPropertiesPage
{
public:
virtual void showProperties(QWidget* widget) = 0;
};

#endif


7150

Lykurg
6th December 2011, 06:45
Well A::run() could not be reached, since it is long go... It is deleted after the c-tor (Basic C++ :eek:)! So make A a private member or create it on the heap.

bmpix
6th December 2011, 19:54
Wow, this is stupid of me. Thanks!