Qt: Why doesn't my button get the signal? - qt

Why didn't the button object get the sigKK() signal when the button was clicked?
When a signal is emitted, can all qt objects receive this signal?
The code is as follows:
class PushButton : public QPushButton
{
Q_OBJECT
signals:
void sigKK();
};
The PushButton class inherits from QPushButton, but doesn't connect signals and slots here. Is this right?
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(){
resize(400,200);
connect(this,SIGNAL(sigKK()),this,SLOT(showRecv1()));
button = new PushButton();
button->setText("Hello,All");
button->resize(40,15);
connect(button, SIGNAL(clicked()),this,SLOT(buttonCK()));
connect(button, SIGNAL(sigKK()),this,SLOT(showRecv2()));
//**I can connect sigKK signal with showRecv2 slot here ?****
button->show();
}
~MainWindow(){
}
signals:
void sigKK();
public slots:
void showRecv1(){
cout<<"recved 1"<<endl;
resize(100,100);
}
void showRecv2(){
cout<<"recved 2"<<endl;
button->setText(".....");
}
void buttonCK(){
emit sigKK();
cout<<"emited"<<endl;
}
private:
PushButton *button ;
};
#endif

When a signal is emitted, can all qt objects receive this signal ?
No. When a signal is emitted it is received only by QObjects with signals or slots connected to it.
Your MainWindow and your PushButton both have a signal with the same name... but they are still different signals. They are completely unrelated to each other. When MainWindow emits sigKK, that has no effect on PushButton's sigKK.
In your example, sigKK seems to be entirely unneccessary. Perhaps you could instead connect clicked() directly to the actions you want to perform?
connect(button, SIGNAL(clicked()),this,SLOT(showRecv1()));
connect(button, SIGNAL(clicked()),this,SLOT(showRecv2()));

Related

signal points to slot in struct

I'd like a signal to be connected to a slot inside of a struct. My struct looks like:
//Header file
struct someStruct {
public:
int index;
public slots:
void someSlot();
};
QList<someStruct*> mListOfStructs;
and then I create a button that should forward its clicked() signal to the someSlot function.
//Source file
QPushButton *cmd = new QPushButton();
grd->addWidget(cmd, 3, 2, Qt::AlignCenter);
//grd is a QGridLayout somewhere inside the gui. I can see it and also the button.
now connection the clicked() event with the slot inside a specific struct does not work.
connect(cmd, SIGNAL(clicked()), mListOfStructs[3], SLOT(someSlot()));
some sources tell me that I have to add a metaObject or sth. I tried but it didn't work out. Maybe you know better.
I might use How to connect in Qt signal and slot in dynamically added buttons to get in slot index of added button? as workaround though.
your structure need the Q_Object meta attributes in order to emit signals and recieve slot events...
struct testStruct : public QObject
{
Q_OBJECT
public:
int index;
testStruct():index(0){}
public slots:
void someSlot()
{
qDebug() << "slot called!";
}
};
after that you can conected as usual:
testStruct *ts= new testStruct;
connect(this, SIGNAL(someSignal()), ts, SLOT(someSlot()));

Can eventFilter react to signals on Qt

I am emiting a signal from mainwindow to my editor class where my event filter is placed is there any way for enent filter react to Signals
mainwindow:
connect(this, SIGNAL(MidiMessage(QString)),Editor,
SLOT(getMsgfromUi(QString)));
emit MidiMessage(QString::number(message->getStatus()));
my editor class:
bool Editor::eventFilter(QObject *o, QEvent *e)
{
if (e->type()==??signal
}

Unable to connect to signal from outside class in Qt

I have a EmitSignal class like this:
class EmitSignal : public QObject
{
Q_OBJECT
public:
EmitSignal() {
emit emittedSignal();
qDebug() << "Signal emitted";
}
signals:
void emittedSignal();
};
And in ConnectSlot class, it's like this:
class ConnectSlot : public QMainWindow
{
Q_OBJECT
public:
ConnectSlot() {
connect(&emitSignalObject, &EmitSignal::emittedSignal, this, &ConnectSlot::connectToSlot);
}
EmitSignal emitSignalObject;
public slots:
void connectToSlot() {
qDebug() << "Connected";
}
};
As you can see, I tried to connect the signal and slot, but it seems like the slot is not triggered. And the only output I got is: Signal emitted.
Why isn't the slot not connected and how do I do it properly?
Thanks
You are emitting a signal from EmitSignal's constructor. That constructor will run before the body of ConnectSlot's constructor begins execution.
So the signal will be emitted before the connection is made.
You need to change your code so that connections are made before signals get fired.

How to connect signal and slot in different classes in Qt?

I have two simple classes(class A and class B).
In a.h, I just declared a QPushButton:
QPushButton *testBtn = new QPushButton(this);
In b.h:
class B : public QMainWindow
{
Q_OBJECT
public:
explicit B(QWidget *parent = nullptr);
A testingA;
public slots:
void testing();
};
and b.cpp:
B::B(QWidget *parent) : QMainWindow(parent)
{
connect(testingA.testBtn, &QPushButton::clicked, this, &B::testing);
}
void B::testing()
{
qDebug() << "testing";
}
I am trying to connect the signal in class A to the slot in class B, but from the code I provide, it's not working.
So what is the right way to do it? Thanks
Edit:
According to PRIME's answer, I made a few changes.
In A's constructor, added:
connect(testBtn, &QPushButton::clicked, [this](){OnButtonClicked();});
to emit the own defined OnButtonClicked() signal;
and in B's construtor, changed to this:
connect(&testingA, &A::OnButtonClicked, this, &B::testing);
But when I clicked the button, the testing slot still not triggered.
Edit 2:
After doing some researches and trying a few times, I found that if I created B's object in A's constructor, and then connect A's signal to B's slot in A, it will work.
But I really can not figure out why I can not connect A's signal to B's slot in B.
This is what's in the main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
B b;
A w;
w.show();
return a.exec();
}
Is that because of some reasons that A's object is out of scope in B?
Can someone tell me where I did wrong? Thanks so much.
Don't do it like this, hide your button in the class A, emit your own defined signal from class A lets call it OnButtonClicked.
Cascading code(inside A's c'tor):
connect(testBtn , &QPushButton::clicked, [this](){OnButtonClicked();});
You will also have to declare this new signal in class A now:
So class A must have folowing besides whatever it has right now:
class A
{
Q_OBJECT
signals:
void OnButtonClicked();
};
No special slot is needed since you are using a Lambda as a slot for the signal OnButtonClicked.
Connection in class B( do it in the c'tor ):
connect(testingA, &A::OnButtonClicked, this, &B::testing);
You can connect signal-to-signal in your sender object, for example widget containing the button:
class MyWidget : public QWidget
{
Q_OBJECT
QPushButton *pushButton;
public:
explicit MyWidget(QWidget *parent = nullptr) : QWidget(parent), pushButton(new QPushButton(this)) {
connect(pushButton, &QPushButton::click, this, &MyWidget::buttonClicked);
}
signals:
void buttonClicked();
public slots:
};
By the way you would normally send signals by using emit keyword, e.g.:
emit buttonClicked();
Then the consumer:
class TestObject : public QObject
{
Q_OBJECT
public:
explicit TestObject(QObject *parent = nullptr) : QObject(parent) { }
public slots:
void onButtonClicked() {
qDebug() << "clicked";
}
};
And connect both instances:
MyWidget widget;
TestObject to;
QObject::connect(&widget, &MyWidget::buttonClicked, &to, &TestObject::onButtonClicked);
in your class A you should use the signal testing of the class B, if you clicked on your button the OnButtonClicked function will be activated
A:
public slots:
void OnButtonClicked();
void A::OnButtonClicked()
{
...
emit testing(1);
}
B:
signals:
void testing(int level);
then to connect both you can do this
connect(startButton, &QPushButton::clicked, board, &A::OnButtonClicked);

Qt connect mainwindow and dialog using signal and slot

I am trying to connect mainwindow and dialog using signal and slot. I am very new to qt. I have a lineEdit and a pushButton in mainwindow.ui, a lineEdit in dialog.ui. And I have those very basic code:
mainwindow.h:
signals:
void sendString(QString);
mainwindow.cpp:
void MainWindow::on_pushButton_clicked()
{
Dialog *mDialog = new Dialog(this);
emit sendString(ui->lineEdit->text());
connect(this, SIGNAL(sendString(QString)), mDialog, SLOT(showString(QString)));
mDialog->show();
}
dialog.h:
private slots:
void showString(QString);
dialog.cpp:
void Dialog::showString(QString str)
{
ui->lineEdit->setText(str);
}
But after I clicked the pushButton, the dialog showed, but nothing changed in the lineEdit.I hope I explain this clearly enough?
Can someone explain to me why and how to solve this? Thanks.
emit signal after connect
void MainWindow::on_pushButton_clicked()
{
Dialog *mDialog = new Dialog(this);
connect(this, SIGNAL(sendString(QString)), mDialog, SLOT(showString(QString)));
mDialog->show();
emit sendString(ui->lineEdit->text());
}
You have to create the connection before the emit.
But in your case you dont need the signal of the of the mainwindow at all. You invoke the showString method directly.

Resources