I have class
class Files : public QWidget
{
Q_OBJECT
public:
Files();
~Files();
public slots:
void saveFile(Ui::MainWindow * ui);
void openFile(Ui::MainWindow * ui);
void checkOpenFile(Ui::MainWindow * ui);
void newFile(Ui::MainWindow * ui);
void checkNewFile(Ui::MainWindow * ui);
void closeFile(Ui::MainWindow * ui);
};
and i want to connect button placed in MainWindow class with slot void newFile(Ui::MainWindow * ui) of Files class
i tried
files = new Files;
//files->newFile(ui); works as it should
connect(ui->actionExit, SIGNAL(triggered()), files, SLOT(closeFile(ui)));
and I have message:
QObject::connect: No such slot Files::closeFile(ui) in ../qt/src/core/mainwindow.cpp:16
QObject::connect: (sender name: 'actionExit')
How should i connect ui->actionExit with Files::closeFile(ui) slot the right way?
First, you are attempting to connect signal and slot which are not compatible with each other: closeFile slot requires a parameter of Ui::MainWindow * type but the signal which you are attempting to connect to it has no parameters.
Do you really need closeFile to take a pointer to Ui::MainWindow? Can you instead make this and other slots take no parameters? If you really need to access Ui::MainWindow from inside methods/slots within Files, you can just create a setter to pass the pointer to Ui::MainWindow to Files and use that pointer in slots which themselves won't take any parameters.
Second, you are trying to use old, obsolete and slow method of setting up connections between signals and slots. This method is not only bad because it's slow but it is also bad because it only shows you errors during runtime i.e. when you compile and launch your app. If you used pointer to member function based connection syntax, it would refuse to compile should you try to connect incompatible signals and slots. It is much better this way because you can see and fix the errors early.
Related
I want to implement signal and slot between two classes mainwindow and reader.
Inside the reader class I declare the signal SetProgress:
reader.h
class reader :public QObject
{
Q_OBJECT
signals:
void SetProgress(QString sOperation, int nPercentage);
}
reader.cpp
void reader::UpdateProgress(double amount)
{
int nPrecentage = (100 * amount / (max- min));
emit SetProgress(m_sCurrentOperation, nPrecentage);
}
mainwindow.h
public:
reader *MyReader
private slots:
void SlotDisplayProgress(QString sActivity_i, int ProgressPercentage_i);
mainwindow.cpp
void mainwindow :: SlotDisplayProgress(QString sActivity_i, int nProgressPercentage_i)
{
this->ui->QprogressBar->setValue(nProgressPercentage_i);
}
inside Mainwidow.cpp I will declare signal and slot
MyReader = reader::New();
connect ( MyReader, &reader::SetProgress, this, &mainwindow::SlotDisplayProgress );
I tried debugging and everything works correctly till the emit part. However, the slot is never executed.
Try setting Qt::DirectConnection:
connect ( MyReader, &reader::SetProgress, this, &mainwindow::SlotDisplayProgress, ***Qt::DirectConnection***);
I had a problem like this, where I connected the signal and slot, and it only worked when I defined the type of connection.
I hope this helps.
PS. I don't know if this depends on the version of QT but when I connect signals and slots the syntax I write is the following:
ImageURLLoadListener* downloader = new ImageURLLoadListener(&id, socket);
connect(downloader, SIGNAL(imageLoaded(QString*,QTcpSocket*)), this, SLOT(on_resourceImageDownload(QString*,QTcpSocket*)), Qt::DirectConnection);
I don't know if it's related or not...
Is MyReader pointer? Use &MyReader if not so.
I have a parent class with its method to change a labels picture on certain Signals. E.g. When something happens in a QComboBox... (valueChanged, activated)
class parentClass : public QMainWindow
{
Q_OBJECT
...
public slots:
//this is the slot i want to connect to some signal of
//e.g a combo box to change the picture by passed in string
void changePicture(QString fileName);
Then i have this child:
class childClass : public QObject
{
Q_OBJECT
public:
childClass(parentClass *parent, QTabWidget *tab, QStringList *guards=0);
private:
bool readCombo(QXmlStreamReader *xmlreader);
Now inside of readCombo i want to read a string and pass it to change picture:
QString imageFileName = xmlreader->attributes().value("image").toString();
QSignalMapper * signalMapper = new QSignalMapper(parent);
//this is just one of many trials to get this working, i hope you get the picture
connect(combo , SIGNAL(activated(int)), parent, SLOT(changePicture(QString *)));
signalMapper->setMapping(combo, imageFileName);
But this gives me either No such Signal , No such Slot or in the upper case Incompatiple sender/receiver arguments
I would appreciate some help on this one since the syntax is really not being intuitive (imo) and i can't find any good reference that is working for my case (did trial and error a lot before asking)
Some issues with your code.
you really can't connect to a private slot of a QObject
You are creating a new signal mapper every time you call readCombo which you aren't clearing - resulting memleaks.
calling connect multiple times creates multiple connections i.e., you will call the same slot multiple times with single signal.
From your example code, I see that you can solve this either by making the parentClass slot public OR add a signal to the childClass and connect to it in the parentClass.
Other option is change the readCombo like this:
QString imageFileName = xmlreader->attributes().value("image").toString();
parentClass->changePicture(imageFileName);
and your parent class as
class parentClass : public QMainWindow
{
Q_OBJECT
...
public slots:
//this is the slot i want to connect to some signal of
//e.g a combo box to change the picture by passed in string
void changePicture(QString fileName);
I see several errors:
You are connecting to a private slot in parentClass from childClass, you should make it public if you want to connect it.
You connect a signal to a slot with another function signature. Your signal has parameter type int, and slot type QString*. The functions should share the same type of parameters.
In your connect you refer use the parameter type QString* in your slot, but in parentClass the parameter type is QString.
For more information about signals and slots see: Signals & Slots.
PS: your naming of childClass and parentClass is not clear either since they both inherit from QObject. See C++ inheritance.
OK, got it:
QString imageFileName = xmlreader->attributes().value("image").toString();
QSignalMapper * signalMapper = new QSignalMapper(parent);
signalMapper->setMapping(combo, imageFileName);
connect(signalMapper, SIGNAL(mapped(QString)), parent, SLOT(changePicture(QString)));
connect( combo, SIGNAL(activated(int)), signalMapper, SLOT(map()) );
I'm trying to write a method, with two parameters : the Q_PROPERTY name (char *) and the QObject * associated with, that permit to connect the notifySignal (if exists) of the Q_PROPERTY, to a void slot(QVariant), or to a slot dynamically builded which will call a void method(QVariant). The signature of the signal can vary according to the type of the parameter.
How can I achieve that in Qt 5? Maybe it's impossible, but I will not stop searching while I'm not sure of that.
So I think I have 3 solutions:
building dynamically a slot of the exact signature of the signal, from the signal name, and call a method(QVariant) in it, using the old Qt connect way:
connect(sender, SIGNAL (valueChanged(QString,QString)),
receiver, SLOT (updateValue(QString)) );
using the new Qt 5 connect system:
connect(sender, &Sender::valueChanged,receiver, &Receiver::updateValue );
building all slots signatures that can be used with QVariant.
Althougt, I don't know how to build a slot dynamically that call a specified method for the first solution ; I don't know how to retrieve the function pointer from the QMetaMethod of the notifySignal, for the second solution ; maybe the last solution is the best way, and easy to achieve, but it seams a bit extreme.
What do you think about it?
For "building a slot dynamically" there are private APIs in Qt (look for QMetaObjectBuilder or similar classes). However that doesn't solve the problem of the connection.
Note that QObject::connect is overloaded to take QMetaMethods as signals and slots. So you can easily build a "receiver" class object (for the only purpose of remembering which property it's acting upon):
class Receiver : public QObject {
Q_OBJECT
public:
explicit Receiver(const char *property, QObject *parent = 0)
: QObject(parent),
m_property(property)
{}
signals:
void propertyChangedForObject(const char *property, QObject *object);
public slots:
void onPropertyChanged() {
emit propertyChangedForObject(m_property, sender());
}
private:
const char * const m_property;
};
And hook it up in your method.
I have a problem creating a QPushButton and linking its signal to my slots. First, I created a class with the slot:
class A : public QWidget{
public slots:
void handleButton();
};
There is my handleButton function in the .cpp
void A::handleButton(int row, int col){
m_button->setText("Example");
// resize button
m_button->resize(100,100);
}
Then I want to connect the button.
QObject::connect(m_button, SIGNAL(clicked()), qApp, SLOT(handleButton()));
But I got an error when I start the application:
"No such slot"
Make sure that qApp is an object of class A (i.e. where your slot is defined).
That said, the signatures are wrong: a signal links to a slot only if the signature match
http://qt-project.org/doc/qt-4.8/signalsandslots.html
The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot.
And your slot hasn't the right signature:
http://qt-project.org/doc/qt-4.8/qabstractbutton.html#clicked
void QAbstractButton::clicked ( bool checked = false ) [signal]
You have a few errors in this code, if you define "void handlebutton()" then you must implement void handlebutton() NOT void handlebutton(inx x, int y) this code should not even compile.
More: in QT you CAN ONLY connect SIGNALS and SLOTS with the same parameters so you can connect SIGNAL(clicked()) with SLOT(handlebutton()) but not SIGNAL(clicked() with SLOT(handleButton(int, int)).
Another problem is that connect is executed at runtime so You must compile and run before Qt can show you the error.
So a possible solution is:
define and implement the slot void handlebutton() and connect that to the signal clicked(), then define another method handleButton (int x, int y) that you will call from inside handleButton().
I really hope that makes sense to you.
Your class definition should look like :
class A : public QWidget
{
Q_OBJECT
public slots:
void handleButton(int, int);
};
And you should connect it like:
QObject::connect(m_button, SIGNAL(clicked()),qApp, SLOT(handleButton(int a, int b)));
where a and b are variables for row and column.
This should work. Try understanding basic C++. :)
I am trying to connect two slots with on signal. here is my header file where I have defined my signals
class loginChecker : public QObject
{
Q_OBJECT
public:
static loginChecker *instance(void);
signals:
void loginid();
void loginemail();
private:
loginChecker(void) {}
~loginChecker(void) {}
private:
static loginChecker *s_instance;
};
it is sigalton class. here is my slot which is another signalton class named as loginProcess
the slot name in this class in getUserData(QString& userData)
in The loginChecker.cpp
I am trying to connect these declared signals with getUserData(QString userData) slot
loginProcess *lp;
lp = loginProcess::instance();
here I am accessing instance of loginProcess signalton class which has slot
These are two ways I am trying to connect these signal and slot but none are working
QObject::connect(s_instance, SIGNAL(loginid()), es, SLOT(getUserData(userid)));
this is throwing no such slot error at runtime
`QObject::connect(s_instance, &esCoreAuthenticator::loginid, es, &loginProcess::getUserData(uid));`
this is thworing this error cannot call member function 'void loginProcess::getUserData(const QString&)' without object at compile time
Since your signal has no arguments you can't connect it to slot which has some.
And also it seems to me that in first case you should try SLOT(getUserData(QString&)) and in second case just &loginProcess::getUserData without any parenthesis.