Hello,
have you thought about throwing away the SignalHandler? You are using Qt5, and Qt5 provides a couple of new static connect functions. Combining them with lambdas from C++11 the code can be simplified. See http://doc.qt.io/qt-5/qobject.html#connect-4 for a reference.
What I have understood from your example: You have a couple of buttons associated with "themes" and clicking on a button should set the associated theme to your software (currently you are simply showing the theme name on the debug console). To reproduce your setup, I've created a simple application with three buttons pushButton0, ..., pushButton2 in the main window with text "Theme 1", ..., "Theme 2".
For compiling with C++11 (gcc option -std=c++11) add the following line to your .pro file:
CONFIG += c++11
CONFIG += c++11
To copy to clipboard, switch view to plain text mode
Here is the MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private: // methods
void theme(const QString& data);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class QPushButton;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private: // methods
void connectTheme(QPushButton* button);
void theme(const QString& data);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
Observe that this does not contain the declaration of a slot. The private method theme(data) should get called when clicking on a button with the data associated to that button (currently the button text). So the buttons need to get connected to method theme(). To simplify the connection process, I have introduced the function connectTheme(QPushButton*). Here is the MainWindow.cpp containing the implementation of these functions:
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <iostream>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connectTheme(ui->pushButton0);
connectTheme(ui->pushButton1);
connectTheme(ui->pushButton2);
}
MainWindow::~MainWindow()
{
delete ui;
}
{
/*
* Store the data associated with the button into a local variable.
* In this example, the data are just the button text.
*/
auto themeData = button->text();
/*
* Here we connect the clicked(bool) signal of button to a lambda capturing
* the themeData by value. This lambda calls MainWindow::theme() method passing
* it the captured data.
*/
connect(button, &QPushButton::clicked, [this, themeData](){theme(themeData);});
}
void MainWindow::theme(const QString& data)
{
/*
* Just process the data passed in.
*/
std::cout << data.toStdString() << std::endl;
}
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connectTheme(ui->pushButton0);
connectTheme(ui->pushButton1);
connectTheme(ui->pushButton2);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connectTheme(QPushButton* button)
{
/*
* Store the data associated with the button into a local variable.
* In this example, the data are just the button text.
*/
auto themeData = button->text();
/*
* Here we connect the clicked(bool) signal of button to a lambda capturing
* the themeData by value. This lambda calls MainWindow::theme() method passing
* it the captured data.
*/
connect(button, &QPushButton::clicked, [this, themeData](){theme(themeData);});
}
void MainWindow::theme(const QString& data)
{
/*
* Just process the data passed in.
*/
std::cout << data.toStdString() << std::endl;
}
To copy to clipboard, switch view to plain text mode
Some explanation: In the constructor I connect the 3 buttons after ui initialization. The connectTheme(button) function stores the button text in a local variable and then connects the clicked signal of QPushButton button to a lambda function
[this, themeData](){theme(themeData);}
[this, themeData](){theme(themeData);}
To copy to clipboard, switch view to plain text mode
The lambda function captures the this pointer (the pointer to the current MainWindow object) and a copy of the local variable themeData. In the lambda body
theme(themeData);
theme(themeData);
To copy to clipboard, switch view to plain text mode
method theme() of MainWindow is called with the data associated with the button. This lambda function replaces the QSignalMapper. The theme(data) method just processes the data passed in (in this case just print them to cout). Note that there is no need to declare theme() as a slot.
With porting my application to Qt5 and using C++11 I now prefer the procedure explained above over using QSignalMapper.
Best regards
ars
Bookmarks