Not able to connect signals and slots - c++

I wrote a basic C++ program for simulating the signal and slot process. I made a push button "button", a QVBoxLayout "layout". I added the button in the layout. Everything is fine till now. But when push button is made to connect by signal and slot, there's are two problem/errors.
"no instance of overloaded function "QObject::connect matches the argument list"."
'QObject::connect': none of the 3 overloads could convert all the argument types.
Question:
I am sure that there's something missing, which I am not able to decipher. Here, the push button "button" has to invoke the function "connectFunc". Instead it gives me above two errors. How to make the button invoke the function?
This is my code.
#include "signalsslots.h"
#include <QtWidgets/QApplication>
#include<qpushbutton.h>
#include<qboxlayout.h>
#include<iostream>
using std::cout;
using std::endl;
void connectFunc()
{
cout << endl << "connected " << endl;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton* button = new QPushButton("Press Here");
QAction* bAction = new QAction;
button->addAction(bAction);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(button);
//layout->SetFixedSize(200, 150);
QObject::connect(bAction, SIGNAL(QPushButton::clicked()), &a,(connectFunc()));
QWidget w;
w.setLayout(layout);
w.setWindowTitle("Signal and Slot Example !!!");
w.show();
return a.exec();
}
FYI, I am using VS 2019 for writing Qt widget applications.

The QObject::connect() function that you use, connects a signal (i.e. a method) of an object to a slot (i.e. a method) of another object.
What you are telling QT is to connect the clicked() method of your QAction object, to the connectFunc() of your QApplication object. Both methods do not exist on those objects!
What you probably want is to connect the clicked() method of your QButton button to the free function connectFunc(). Like this:
QObject::connect(button, &QPushButton::clicked, &connectFunc);
Note that this requires QT5. (See Is it possible to connect a signal to a static slot without a receiver instance?)

Related

Qt - Non Modal Dialog before Main Window is created

I've been struggling to do this : I want to show on a window a QWidget, or a QDialog before the MainWindow is created, but I cannot use exec() because it will enter its loop and won't create my MainWindow before I accept or reject the dialog.
The reason I want to do this is to have a widget showing information while the MainWindow constructs itself. I don't want to keep this extra window once the MainWindow is showing up.
I believe the issue comes from the fact that the main window is already created when a.exec() is called and the window won't show up before a.exec(). The solution I found is to use a QDialog instead and call exec() but it blocks the rest of the code which I don't want to happen.
Code :
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StartUpDialog start; //this is my custom QDialog, can be a QWidget if necessary.
qDebug() << "starting up!";
MainWindow w;
start.exec(); //I tried show() but it won't show up.
w.startApp(&start); //this function will do some stuff.
w.show();
//I don't want start to stay after mainwindow shows up
return a.exec();
}
Here is what I tried so far :
I tried to create and show the StartUpDialog while constructing the MainWindow but it won't work out.
Use start.show(), but it won't show before the mainwindow does, both for a QWidget and a QDialog.
Use start.exec(), this does what I want but it's modal and I couldn't make it non-modal with SetModal(false) or setWindowModality(Qt:NonModal).
I also tried to use start.exec() and attempted to reimplement accepted() and exec() so that it automatically calls accepted() as soon as it appears but it will still close the window.
Hopefully you can help me in that issue, and thanks for reading !
UPDATE : Solved thanks to Trap, here is how I made it :
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StartUpDialog start;
QSplashScreen *splash = new QSplashScreen();
StartUpWidget *start = new StartUpWidget(splash);
splash->resize(350,380);
start->show();
splash->raise();
splash->show();
qDebug() << "starting up!";
MainWindow w;
w.startApp(start);
w.show();
splash->finish(&w);
start->deleteLater();
splash->deleteLater();
return a.exec();
}
My only concern is that I use a Gif inside my widget using QMovie and updating it has to be done manually apparently.
If I understand your problem correctly (showing a dialog until your main window is created), you should have a look at the QSplashScreen class : http://doc.qt.io/qt-5/qsplashscreen.html

How do we pass the value to all the elements of GUI in this QT code

I'm reading a book on QT4 and here's an example from the book:
QApplication a(argc, argv);
QWidget window;
QVBoxLayout* mainLayout = new QVBoxLayout(&window);
QLabel* label = new QLabel("0");
QSpinBox* spinBox = new QSpinBox;
QSlider* slider = new QSlider(Qt::Horizontal);
mainLayout->addWidget(label);
mainLayout->addWidget(spinBox);
mainLayout->addWidget(slider);
QObject::connect(spinBox, SIGNAL(valueChanged(int)), label, SLOT(setNum(int)));
QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
QObject::connect(slider, SIGNAL(valueChanged(int)), label, SLOT(setNum(int)));
QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int)));
window.show();
As the book and compilation show, changing the value of one of the widgets leads to changing the values of the other elements.
What I don't understand is how it happens. How do we pass that value from one widget to the rest? There's no variable that gets changed by means of one widget and gets passed to the others.
PS Conceptually, I do understand the idea of slots and signals and 'connect'. It's how the value is passed from one widget to the rest is the problem.
A signal is a C++ method with code generated by a utility called moc (meta object compiler). A slot is regular C++ method, with code under your control. A connection is a way of letting the signal know what slots to call. When the signal gets emitted, it really means that you call the machine-generated method that iterates the connection list.
Conceptually, the valueChanged signal implementation looks like this:
void valueChanged(int value) {
for (slot : this->slots)
(slot.object->*slot.method)(value);
}
Thus, when the slider "emits" its signal, it calls each slot with a given value. After the connections are made, you should think of the spinbox's valueChanged signal as doing the following:
void SpinBox::valueChanged(int value) {
// 1st connection
label->setNum(value);
// 2nd connection
slider->setVale(value);
}
There's no "variable" that gets changed because the signal-slot mechanism is, at its core, an easier-to-use way of doing indirect method calls (via method pointers and instance pointers).
In modern code (Qt5/C++11), that example would be (this is complete code):
#include <QtWidgets>
int main(int argc, char** argv) {
QApplication a{argc, argv};
QWidget window;
QVBoxLayout mainLayout{&window};
QLabel label{"0"};
QSpinBox spinBox;
QSlider slider{Qt::Horizontal};
mainLayout.addWidget(&label);
mainLayout.addWidget(&spinBox);
mainLayout.addWidget(&slider);
QObject::connect(&spinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
&label, static_cast<void(QLabel::*)(int)>(&QLabel::setNum));
QObject::connect(&spinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
&slider, static_cast<void(QSlider::*)(int)>(&QSlider::setValue));
QObject::connect(&slider, static_cast<void(QSlider::*)(int)>(&QSlider::valueChanged),
&label, static_cast<void(QLabel::*)(int)>(&QLabel::setNum));
QObject::connect(&slider, &QSlider::valueChanged, &spinBox, &QSpinBox::setValue);
window.show();
return a.exec();
}

Click default QPushButton when Enter SpinBox

I need to raise the click signal of my QPushButton when I press Enter on QSpinBox (actually on every input box), but even if my button is the default button, the following code doesn't work.
#include <QApplication>
#include <QHBoxLayout>
#include <QPushButton>
#include <QSpinBox>
#include <QMessageBox>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget* window = new QWidget();
QSpinBox* spinbox = new QSpinBox();
QPushButton* button = new QPushButton("Ok");
button->setDefault(true);
QObject::connect(button, &QPushButton::clicked, []()
{
QMessageBox::question(nullptr, "Test", "Quit?", QMessageBox::Yes|QMessageBox::No);
});
QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(spinbox);
layout->addWidget(button);
window->setLayout(layout);
window->show();
return app.exec();
}
How can I fix it?
You can see in the Qt documentation about QPushButton :
The default button behavior is provided only in dialogs
You are using a QWidget and expect the default button behavior to work. Just use a QDialog instead of QWidget :
QDialog * window = new QDialog();
...
you can use Event
The QObject::installEventFilter() function enables this by setting up
an event filter, causing a nominated filter object to receive the
events for a target object in its QObject::eventFilter() function. An
event filter gets to process events before the target object does,
allowing it to inspect and discard the events as required.
void QObject::installEventFilter ( QObject * filterObj )
Installs an event filter filterObj on this object. For example:
monitoredObj->installEventFilter(filterObj);
An event filter is an object that receives all events that are sent to
this object. The filter can either stop the event or forward it to
this object. The event filter filterObj receives events via its
eventFilter() function. The eventFilter() function must return true if
the event should be filtered, (i.e. stopped); otherwise it must return
false.

Qt 5.3 Signals and Slots, Simple Function and Lambda Expression

I tried to Write a program using Qt 5.3 and I try to using signals and slots as practice. I wrote the following code (part of the code) :
void exitToWin()
{
exit(0);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QMessageBox EndBox;
QObject::connect((EndBox.button(QMessageBox::Ok)),SIGNAL(clicked()),exitToWin);
w.show();
EndBox.show();
return a.exec();
}
I even change the declaration of the function to static and I checked the expression with parentheses and without them while I am writing the connect command. but although what Qt documented and what its IDE guided to. also I read here and I tested it.
Moreover I tried with lambda expression as below:
QObject::connect((EndBox.button(QMessageBox::Ok)),SIGNAL(clicked()),[=](){
exit(0);
});
but still I receive errors indicate "No matching function call".
And after all I have to say that I am using Microsoft Windows 7.
This works on Qt 5.3:
#include <QtWidgets>
void exitToWin()
{
exit(0);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
QMessageBox endBox;
endBox.addButton(QMessageBox::Ok); // (2)
endBox.connect(endBox.button(QMessageBox::Ok),
&QAbstractButton::clicked, exitToWin); // (1)
/* This works, too:
endBox.connect(endBox.button(QMessageBox::Ok),
&QAbstractButton::clicked,
[] () { exit(0); });
*/
w.show();
endBox.show();
return a.exec();
}
Here's why:
(1) You can use endBox's QObject to do the connection between the QAbstractButton's clicked signal and your exitToWin simple function. You also can't connect a SIGNAL to a simple function or a lambda, so we use the member function variety, instead.
(2) endBox doesn't actually get an OK button by default. When you mention it on line (1) in your code, it creates it, but not in time (apparently) to pass the pointer back to connect, so we create it first here.
Your code won't work for two reasons:
Firstly, QMessageBox does not have such a signal. See the documentation for the signals it does have.
Secondly, when making connections from a signal to a slot (or lambda function), you must define the function signatures, not specific values.
If a signal can pass a variety of values and you only want your slot to perform a certain function on a selection of those values (in this case, only if the value QMessageBox::Ok is passed) it is up to the slot to interrogate the values, not the connect statement.
Since the connect() method is from QObject it has to be called from a QObject child containing the Q_OBJECT macro in its declaration. Running qmake prepare the class to send signals and receive slots.

How to programmatically click a QPushButton

I am using Qt. I have a button in the page added via the Qt Creator. It is connected to the method void MyPage::on_startButton_clicked().
I want to programmatically click this button. I tried ui->startButton->clicked(), it gives,
error C2248: 'QAbstractButton::clicked' : cannot access protected member declared in class 'QAbstractButton'
Please help. Thanks!
Use QAbstractButton::animateClick():
QPushButton* startButton = new QPushButton("Start");
startButton->animateClick();
RA's answer provides the way to do it so that it's visible that the button was clicked. If all you wish for is to emit the signal, what you're doing is correct in Qt 5, where the signals are public.
The error you're facing indicates that you're using Qt 4, where the signals are not public. You can work around this by invoking the signal indirectly:
QMetaObject::invokeMethod(ui->startButton, "clicked");
This calls the method immediately, i.e. the signal will be dispatched and the slots called by the time invokeMethod returns. Alas, most code (mostly your code!) assumes that the signal is emitted from event processing code close to the event loop - i.e. it's not reentrant, rather than from your own code. Thus you should defer the signal emission to the event loop:
// Qt 5.4 & up
QTimer::singleShot(0, ui->startButton, [this]{ ui->startButton->clicked(); });
// Qt 4/5
QTimer::singleShot(0, ui->startButton, "clicked");
The following is a complete example for Qt 5.4 & up:
#include <QtWidgets>
int main(int argc, char ** argv) {
bool clicked = {};
QApplication app{argc, argv};
QPushButton b{"Click Me"};
QObject::connect(&b, &QPushButton::clicked, [&]{ clicked = true; qDebug() << "clicked"; });
Q_ASSERT(!clicked);
b.clicked(); // immediate call
Q_ASSERT(clicked);
clicked = {};
// will be invoked second - i.e. in connect order
QObject::connect(&b, &QPushButton::clicked, &app, &QApplication::quit);
QTimer::singleShot(0, &b, [&]{ b.clicked(); }); // deferred call
Q_ASSERT(!clicked);
app.exec();
Q_ASSERT(clicked);
}
If you do not want the animation stuff, you can also just call the method:
on_startButton_clicked();