I am trying to learn Qt (using CMake/c++). Here is piece of code that I wrote. I am trying to use connect functionality. There is no compiling error. I used also following command to re-generate moc file.
moc -o moc_SimulationRunner.cpp SimulationRunner.h
However I am still receiving this error while running my project:
QObject::connect: No such slot SimulationRunner::_OpenFileDialog(file_address)
my header file is:
class SimulationRunner : public QWidget
{
Q_OBJECT
public:
explicit SimulationRunner(QWidget *parent = nullptr);
private:
QGroupBox *createFirstExclusiveGroup();
private slots:
bool _OpenFileDialog(QLineEdit* i_line_edit, std::string = "");
};
and my cpp file is:
QGroupBox *SimulationRunner::createFirstExclusiveGroup()
{
QGridLayout *grid = new QGridLayout;
....
QLineEdit *file_address= new QLineEdit();
file_address->setPlaceholderText("File address");
QPushButton *file_address_button = new QPushButton("Browse...");
file_address_button ->setFixedWidth(75);
grid->addWidget(file_address, 0, 0, 1, 1);
grid->addWidget(file_address_button , 1, 1, 1, 1);
group_box->setLayout(grid);
QObject::connect(file_address_button , SIGNAL(clicked()), this, SLOT(_OpenFileDialog(file_address)));
return group_box;
}
bool SimulationRunner::_OpenFileDialog(QLineEdit* i_line_edit, std::string /* = "" */)
{
if (!i_line_edit)
return false;
QString filename = QFileDialog::getOpenFileName(
this,
"Open Document",
QDir::currentPath(),
"All files (*.*) ;; Document files (*.doc *.rtf);; PNG files (*.png)");
if (!filename.isNull())
{
qDebug() << "selected file path : " << filename.toUtf8();
}
i_line_edit->setText(filename);
return true;
}
The arguments (signature) of the signal and the slot have to match.
If you use the old string-based Syntax you have to define the arguments in the SIGNAL() and SLOT() macros. So you could connect for example like this:
QObject::connect(file_address_button, SIGNAL(clicked(bool)), this, SLOT(_ExampleSlotFunction(bool)));
You cannot connect to a slot with more or different arguments. However you can connect to a slot with fewer arguments, e.g. like this:
QObject::connect(file_address_button, SIGNAL(clicked(bool)), this, SLOT(_AnotherExampleSlotFunction()));
If you use the new QT5 Syntax (which is recommended because it should warn you already when compiling) you don't need to specify the arguments yourself:
QObject::connect(file_address_button, &QPushButton::clicked, this, &ExampleClass::_ExampleSlotFunction);
QObject::connect(file_address_button, &QPushButton::clicked, this, &ExampleClass::_AnotherExampleSlotFunction);
QT5 / C++11 workaround
To pass a different argument, you could connect a lambda function to the signal, in which you call the slot or function. So to pass your QLineEdit:
QObject::connect(
file_address_button, &QPushButton::clicked,
this, [file_address]() { _OpenFileDialog(file_address); }
);
Further explanations:
New Syntax
Default Arguments
You're misunderstanding the meaning of the argument in the connect.
the signature of your slot is
OpenFileDialog(QLineEdit*, std::string);
and the signature of your connect is
OpenFileDialog(QLineEdit*)
that's why you're getting the warning. You're not calling a function (so it can be resolved with the default parameter), you're pointing to a function, so the signature has to be the same.
Tip: unless you have to use Qt4, use the Qt5 connect syntax:
QObject::connect(file_address_button, &QPushButton::clicked, this, &SimulationRunner::OpenFileDialog);
for your specific case:
if you always pass file_address in your slot, then just remove the argument from the slot and use it directly in the body.
Otherwise, if you're using Qt4, the only workaround is to make an additional slot with the call with no object.
Qt4 way:
private slots:
void _dummySlot(){
_OpenFileDialog(file_address);
}
QObject::connect(file_address_button , SIGNAL(clicked()), this, SLOT(_dummySlot()));
Qt5 way, use lambda:
QObject::connect(file_address_button, &QPushButton::clicked, this, [this](){
_OpenFileDialog(address_name);
});
Related
I am trying to start a countdown timer in a workerthread when the user clicks a pushbutton. The value at which the timer starts the count down depends on the selected radius button from my GUI.
I use a signalmapper to pass the starting value from the timer as a parameter. However I get this error when trying to compile my code:
static assertion failed: Signal and slot arguments are not compatible
My code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
connect(ui->btnTakeSnap, SIGNAL(clicked()), this, SLOT(startTimerWorker()) );
}
void MainWindow::startTimerWorker()
{
myThread = new QThread(this);
workerObj = new workerTimer();
QSignalMapper* signalMapper = new QSignalMapper (this) ;
connect(myThread, &QThread::started, signalMapper, SLOT(map()) );
if(ui->rdBtntimer1s->isChecked())
{signalMapper -> setMapping (myThread, 1000) ; }
if(ui->rdBtntimer3s->isChecked())
{signalMapper -> setMapping (myThread, 3000) ; }
if(ui->rdBtntimer5s->isChecked())
{signalMapper -> setMapping (myThread, 5000) ;}
connect(signalMapper, SIGNAL(mapped(int)), workerObj, SLOT(&workerTimer::countDown(int)));
workerObj->moveToThread(myThread);
myThread->start();
}
class workerTimer : public QObject
{
Q_OBJECT
public:
explicit workerTimer(QObject *parent = nullptr);
signals:
public slots:
void countDown(int selectedTimer);
};
Could someone explain me what I may be doing incorrectly?
Thanks
connect(myThread, &QThread::started, signalMapper, SLOT(map()) );
connect(signalMapper, SIGNAL(mapped(int)), workerObj, SLOT(&workerTimer::countDown(int)));
You are mixing 2 kinds of connections rendering the entire statement meaningless. see http://doc.qt.io/qt-5/signalsandslots.html
connect(myThread, SIGNAL(started()), signalMapper, SLOT(map()) );
connect(signalMapper, SIGNAL(mapped(int)), workerObj, SLOT(countDown(int)));
Beside IIBeldus's answer, one more reason for this error to happen is that you use the new syntax of QT's connect() function that is :
connect(
sender, &Sender::valueChanged,
receiver, &Receiver::updateValue
);
and the signal (valueChanged) arguments was different from the slot (updateValue) arguments, this will make the compiler to try implicit conversion of the signal and slot arguments and you have one of 2 possibilities:
1. The compiler will try to make auto conversion (implicit conversion) and the conversion succeeded.
2. The compiler will try to make auto conversion (implicit conversion) and the conversion did not succeed.
From QT documentation:
Possibility to automatically cast the types if there is implicit
conversion (e.g. from QString to QVariant)
I posted this answer because i faced the same problem and the accepted answer did not solve my problem, so try this answer if the accepted answer did not help you
I can run the program but the button cannot access the send function. I get this hint:
QObject::connect: No such slot Mail::send(emailInput, pwdInput)
Someone knows what's my mistake?
mail.h:
#ifndef MAIL_H
#define MAIL_H
#include <QWidget>
namespace Ui {
class Mail;
}
class Mail : public QWidget
{
Q_OBJECT
public:
explicit Mail(QWidget *parent = 0);
~Mail();
public slots:
void send(std::string email, std::string pwd);
private:
Ui::Mail *ui;
};
#endif // MAIL_H
mail.cpp:
Mail::Mail(QWidget *parent) :
QWidget(parent)
{
QLineEdit *edt1 = new QLineEdit(this);
grid->addWidget(edt1, 0, 1, 1, 1);
std::string emailInput = edt1->text().toStdString();
...
QObject::connect(acc, SIGNAL(clicked()),this, SLOT(send(emailInput, pwdInput)));
}
void Mail::send(std::string email, std::string pwd){
...
}
In fact, you have 2 mistakes in your code:
the SLOT macro takes the type of arguments as parameter not their name, then the code should be : SLOT(send(std::string, std::string)).
You try to connect a SIGNAL which has less argument than the SLOT which is impossible.
In order to avoid all these problems you can use the new signal/slot syntax (if your are using Qt5):
QObject::connect(acc, &QLineEdit::clicked, this, &Mail::onClicked);
I also invite you to use the QString class instead of std::string when working with Qt, it is a lot easier.
That depends on what you want to do:
If emailInput and pwdInput come from widgets, you have to write an intermediate slot that will get the values and call send.
If they are local variables, the easiest is probably to use a lambda.
Should be
QObject::connect(acc, SIGNAL(clicked()),this, SLOT(send(std::string, std::string)));
Macros SIGNAL and SLOT expect method's signature as argument(s).
Additionally, you can connect a signal to a slot of less arity, not the vice versa; here, QObject would not simply know what should be substituted for slot's arguments. You can use the overload of connect that accepts an arbitrary Functor (an anonymous closure, most likely) as slot:
QObject::connect(acc, SIGNAL(clicked()), [=](){ send(std::string(), std::string()); });
Thirdly, were you to use QString instead of std::string, you would not have that heavy copy overhead when passing by value.
First of all my apologies for big looking question but indeed it's not. I’m reading Foundation of qt development book and while reading fourth chapter author tells the basics of MDI window by showing this example :
MdiWindow::MdiWindow( QWidget *parent ) : QMainWindow( parent ) {
setWindowTitle( tr( "MDI" ) );
QWorkspace* workspace = new QWorkspace;
setCentralWidget( workspace );
connect( workspace, SIGNAL(windowActivated(QWidget *)), this, SLOT(enableActions()));
QSignalMapper* mapper = new QSignalMapper( this );
//my problem is in this line
connect( mapper, SIGNAL(mapped(QWidget*)), workspace, SLOT(setActiveWindow(QWidget*)) );
createActions();
createMenus();
createToolbars();
statusBar()->showMessage( tr("Done") );
enableActions();
}
His this para of explanation completely eluded me (is it me or others having problem understanding it too?) :
Next, a signal mapping object called QSignalMapper is created and
connected. A signal mapper is used to tie the source of the signal to
an argument of another signal. In this example, the action of the menu
item corresponding to each window in the Window menu is tied to the
actual document window. The actions are in turn connected to mapper.
When the triggered signal is emitted by the action, the sending action
has been associated with the QWidget* of the corresponding document
window. This pointer is used as the argument in the mapped(QWidget*)
signal emitted by the signal mapping object.
My question : I still don’t get what is signal mapper class, how it’s used and what's functionality it's doing in the example above?. Can anyone please explain the above para using easy terms? also It’d be awesome if you could please teach me about mapper class’s basics with simple example? possibly in layman’s term?
P.S : A confusion is when we have MDI window, do menu changes (though actions are disabled/enabled) e.g suppose for one particular document we have menu “File/close” and for other document we have “File/remaper” ?
The QSignalMapper is used to re-emit signals with optional parameters. In other words (from the documentation):
This class collects a set of parameterless signals, and re-emits them
with integer, string or widget parameters corresponding to the object
that sent the signal.
A good example (also from the doc - take a look at it) is set as follows:
Suppose we want to create a custom widget that contains a
group of buttons (like a tool palette). One approach is to connect
each button's clicked() signal to its own custom slot; but in this
example we want to connect all the buttons to a single slot and
parameterize the slot by the button that was clicked.
So imagine you have a number of buttons encapsulated in a class, say ButtonWidget, with a custom signal void clicked(const QString &text). Here is the definition:
class ButtonWidget : public QWidget {
Q_OBJECT
public:
ButtonWidget(QStringList texts, QWidget *parent = 0);
signals:
void clicked(const QString &text);
private:
QSignalMapper *signalMapper;
};
The constructor could then be defined like the following:
ButtonWidget::ButtonWidget(QStringList texts, QWidget *parent)
: QWidget(parent)
{
signalMapper = new QSignalMapper(this);
QGridLayout *gridLayout = new QGridLayout;
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
gridLayout->addWidget(button, i / 3, i % 3);
}
connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SIGNAL(clicked(const QString &)));
setLayout(gridLayout);
}
So what happens here? We construct a grid layout and our buttons of type QPushButton. The clicked() signal of each of these is connected to the signal mapper.
One of the forces using QSignalMapper is that you can pass arguments to the re-emitted signals. In our example each of the buttons should emit a different text (due to the definition of our signal), so we set this using the setMapping() method.
Now all that's left to do is map the signal mapper to the signal of our class:
connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SIGNAL(clicked(const QString &)));
Assume we have a testing class called TestClass then ButtonWidget can be used thusly:
TestClass::TestClass() {
widget = new ButtonWidget(QStringList() << "Foo" << "Bar");
connect(widget, SIGNAL(clicked(const QString &)),
this, SLOT(onButtonClicked(const QString &)));
}
void TestClass::onButtonClicked(const QString &btnText) {
if (btnText == "Foo") {
// Do stuff.
}
else {
// Or something else.
}
}
By using the signal mapper this way you don't have to declare and manage all the buttons and their clicked signals, just one signal pr. ButtonWidget.
The buttom line is that the signal mapper is great for bundling multiple signals and you can even set parameters when it re-emits them. I hope that gave some intuition about the usage of QSignalMapper.
Your example code
The explanation (your "para") states that all the actions are each individually mapped to a specific QWidget*. When triggering an action its respective QWidget* will be passed to the slot QWorkspace::setActiveWindow(QWidget*) of workspace, which in turn activates the widget.
Also note that the mapping from action to widget has to happen somewhere in your code. I assume it is done in createActions() or enableActions() perhaps.
A QSignalMapper allows you to add some information to a signal, when you need it. This object internally have a map like QMap<QObject*,QVariant>. Then you connect an object to it, and when the slot is called, it re-emit the signal with the associated value.
Workflow:
mySignalMapper:
[ obj1 -> 42 ]
[ obj2 -> "obiwan" ]
[ obj3 -> myWidget ]
connect(obj1,mySignal,mySignalMapper,SLOT(map())); // idem for obj2 and obj3
(obj2 emits "mySignal")
-> (mySignalMapper::map slot is called)
-> (sender() == obj2, associated data = "obiwan")
-> (mySignalMapper emits mapped("obiwan"))
I was going to add a more detailed example, but Morten Kristensen was faster than me ;)
I'm trying to connect a combo box value and a label such that when the combo box changes the label reflects that. I have googled my heart out trying to find an answer but, as of yet, nothing has worked; I still get the error:no matching function for call to mainWindow::connect(QComboBox*&, const char [38], QString*, const char [26])
I have tried QObject::connect, QWidget::connect and anything else dealing with Qt, but to no avail.
Creating a label that says the combo box value is not my final intention for the program. Rather, I wish to get it working with a simple label then change it to what I want it to display (thus the tempLabel).
mainwindow.h:
class MainWindow : public QMainWindow
{
public:
MainWindow();
private slots:
QString getClass(QComboBox *box);
};
mainwindow.cpp:
MainWindow::MainWindow()
{
QString qMathClassName;
QComboBox* mathClassCombo = new QComboBox;
QLabel* label = new QLabel(qMathClassName);
// omitting layout code...
connect(mathClassCombo, SIGNAL(currentIndexChanged(const QString &)),
&qMathClassName, SLOT(getClass(mathClassCombo)));
}
QString MainWindow::getClass(QComboBox *box)
{
return box->currentText();
}
Any help would be greatly appreciated!
You are connecting a signal to a slot with a different signature. You have to change your slot to something like
getClass(const QString &)
to match currentIndexChanged signal.
I think you need to read Qt's signals and slots documentation. Again, if you've already done so. Pay special attention to their examples.
I think that you had these misconceptions about Qt in C++:
That QLabel takes a reference to a QString, and that it will update its text when that string changes. It won't. QLabel will display the value of the string when you give it the string. That is the only time it will update.
That objects constructed on the stack will not be destroyed at the end of the function. They will not. At the end of the constructor, qMathClassName will be destroyed and any reference to it will become invalid. Thus, you'd not want to make a connection to it, even if you could.
That the third argument of QObject::connect is a pointer to a place to put the return value for the slot. It's not. The third argument is a pointer to the QObject on which to call the slot. The return value of a slot is unused for any calls made to it via QObject::connect.
That you can bind values to slots in your connection. Unfortunately not. Within the SLOT macro, you must put the function signature of the slot. You may not reference any variables. The arguments section must have only class names. That is SLOT(getClass(QComboBox*)), not SLOT(getClass(mathClassCombo)).
The simplest way to ensure the contents of a combo box are displayed in a label are this:
QComboBox* mathClassCombo = new QComboBox;
QLabel* tempLabel = new QLabel;
connect(mathClassCombo, SIGNAL(currentIndexChanged(const QString&)),
tempLabel, SLOT(setText(const QString&)));
If you want to do something more complicated, I recommend just making a slot on your window that can handle those complications. For example:
mainwindow.h:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
private slots:
void updateLabelText(const QString& className);
private:
QComboBox* mathClassCombo;
QLabel* tempLabel;
}
mainwindow.cpp:
MainWindow::MainWindow()
{
mathClassCombo = new QComboBox;
tempLabel = new QLabel;
// omitting layout code...
connect(mathClassCombo, SIGNAL(currentIndexChanged(const QString&)),
this, SLOT(updateLabelText(const QString&)));
}
void MainWindow::updateLabelText(const QString& className)
{
QString newLabelString = className + " is the best class ever!";
tempLabel->setCurrentText(newLabelString);
}
I'm making a little chat messenger program, which needs a list of chat channels the user has joined. To represent this list graphically, I have made a list of QPushButtons, which all represent a different channel. These buttons are made with the following method, and that's where my problem kicks in:
void Messenger::addToActivePanels(std::string& channel)
{
activePanelsContents = this->findChild<QWidget *>(QString("activePanelsContents"));
pushButton = new QPushButton(activePanelsContents);
pushButton->setObjectName("pushButton");
pushButton->setGeometry(QRect(0, 0, 60, 60));
pushButton->setText("");
pushButton->setToolTip(QString(channel.c_str()));
pushButton->setCheckable(true);
pushButton->setChecked(false);
connect(pushButton, SIGNAL(clicked()), this, SLOT(switchTab(channel)));
}
(activePanelContents is a QWidget that holds the list.)
The point is that each button should call the switchTab(string& tabname) method when clicked, including the specific channel's name as variable. This implementation doesn't work though, and I haven't been able to find out how to properly do this.
For strings and integers, you can use QSignalMapper. In your Messenger class, you would add a QSignalMapper mapper object, and your function would look like:
void Messenger::addToActivePanels(std::string& channel)
{
activePanelsContents = this->findChild<QWidget *>(QString("activePanelsContents"));
pushButton = new QPushButton(activePanelsContents);
// ...
connect(pushButton, SIGNAL(clicked()), &mapper, SLOT(map()));
mapper.setMapping(pushButton, QString(channel.c_str()));
}
and after you have added all channels to your active panels, you call
connect(&mapper, SIGNAL(mapped(const QString &)), this, SLOT(switchTab(const QString &)));
Use QSignalMapper to pass variables;
QSignalMapper* signalMapper = new QSignalMapper (this) ;
QPushButton *button = new QPushButton();
signalMapper -> setMapping (button, <data>) ;
connect (signalMapper, SIGNAL(mapped(QString)), this,
SLOT(buttonClicked(QString))) ;
in slot i.e
void class::buttonClicked(QString data){
//use data
// to get sender
QSignalMapper *temp = (QSignalMapper *)this->sender();
QPushButton *btn = (QPushButton *)temp->mapping(data);
// use btn
}
Hope my ans may help you
Don't use the sender method unless you absolutely have to. It ties the function directly to being used only as a slot (can't be called directly). Retain the behavior of having the function accept a string and simply make a mechanism by which you can call it.
One method, among others you might find, is to leverage use of QSignalMapper. It will map objects to values and regenerate signals of the appropriate signature.
I would do it with "relay" objects:
Create TabSwitchRelay which is a sub-class of QObject with this constructor:
TabSwitchRelay::TabSwitchRelay(QObject *parent, Messanger * m, const QString & c)
: QObject(parent), m_messanger(m), m_channel(c)
{
}
It also has a slot clicked():
void TabSwitchRelay::clicked()
{
m_messager->switchTab(m_channel);
}
Now replace the line in your code that does connect with this:
TabSwitchRelay * tabRelay = new TabSwitchRelay(pushButton, this, channel);
connect(pushButton, SIGNAL(clicked()), tabRelay, SLOT(clicked()));
It's not tested but you get teh basic idea.
You could try having your switchTab slot take no argument and use QObject::sender to get the object that sent the signal.
Messenger::switchTab()
{
QObject* sender = this->sender();
QPushButton* button = qobject_cast<QPushButton*>(sender);
if(button)
{
// Do stuff...
}
}
if you're using Qt5, you can do it through lambda:
connect( sender, &Sender::valueChanged, [=](){ myMethod(5); } );
void myMethod(int value)
{
// do stuff
}