PDA

View Full Version : Signal and Slot



juraj
18th January 2015, 21:26
Hi i am new in Qt and i try to join signal "clicked" from QPushbutton with my own slot "Slot" but it is not working, and i dont know what i doing wrong
could you help me?

window.h


#ifndef Window_H
#define Window_H

#include <QWidget>

class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent=0);
~Window();
};
#endif


value.h


#ifndef VALUE_H
#define VALUE_H
#include <QObject>

class Value : public QObject
{
Q_OBJECT
public:
explicit Value(QObject *parent=0);
~Value();
public:
void Fu_1(){cx=76;}
int Fu_2(){return cx;}
public slots:
void Slot();
private:
int cx;
};

#endif // VALUE_H


window.cpp


#include "window.h"

Window::Window(QWidget *parent):QWidget(parent)
{
}

Window::~Window()
{
}


value.cpp


#include "value.h"

Value::Value(QObject *parent):QObject(parent)
{
}

Value::~Value()
{
}

void Value::Slot()
{
Fu_1();
}


main.cpp


#include <QApplication>
#include "window.h"
#include "value.h"
#include <QtWidgets>
#include <QHBoxLayout>


int main(int argc, char*argv[])
{
QApplication app(argc,argv);
Window *Win=new Window;
QPushButton *Object_1=new QPushButton;
QLCDNumber *Object_2=new QLCDNumber;
Value *Object_3=new Value;
QHBoxLayout *Lay=new QHBoxLayout;

QObject::connect(Object_1,SIGNAL(clicked()),Object _3,SLOT(Slot())); // this is not working
Object_3->Fu_1(); // but when i try to call Fu_1 through Object its working normaly
int x;
x=Object_3->Fu_2();
Object_2->display(x); // it is working because value of cx was set on 76

Lay->addWidget(Object_1);
Lay->addWidget(Object_2);

Win->setLayout(Lay);
Win->show();
return app.exec();
}

hayzel
19th January 2015, 06:46
Win->show() is equivalent of setVisible(true). It shows up the QWindow.
To start its events, you have to add after showing-up the window:


Win->activateWindow();

wysota
19th January 2015, 07:37
Win->show() is equivalent of setVisible(true). It shows up the QWindow.
To start its events, you have to add after showing-up the window:


Win->activateWindow();


No, that's not true, you don't have to do anything like that.

ChrisW67
19th January 2015, 09:39
QObject::connect(Object_1,SIGNAL(clicked()),Object _3,SLOT(Slot())); // this is not working

How do you determine this? The slot is not called until the button is clicked, when the program is in app.exec(), and does not do anything that is outwardly visible.