I have some problems with inheritance in widgets and connecting slots. I have created an abstract Widget which inherits from QWidget. Here is the prototype :
class WidgetParams : public QWidget
{
Q_OBJECT
public:
explicit WidgetParams(QWidget *parent = 0) : QWidget(parent){}
virtual bool paramChanged() = 0;
protected:
bool paramsChanged;
};
Then I created derivated class from WidgetParams, for example WidgetParamsWindows:
class WidgetParamsWindows : public WidgetParams
{
public:
explicit WidgetParamsWindows(QWidget *parent = 0);
virtual bool paramChanged(){return paramsChanged;}
private:
QFormLayout *layout;
QSpinBox *svertical;
QSpinBox *shorizontal;
signals:
public slots:
void changeSomeParam(int value);
};
In WidgetParamsWindows, I have some QSpinBox, QPushButton etc. to adjust the params.
I connect the QSpinBox in WidetParamsWindows like this :
connect(spinbox,SIGNAL(valueChanged(int)),this,SLOT(changeSomeParam(int));
After that, I created a WidgetParamsWindows and put It in a list of WidgetParams, in order to show the correct WidgetParams when the user clicks on it.
But when I tried to change the value in the QSpinBox, nothing change and I have the following message in the console :
QObject::connect: No such slot WidgetParams::changeSomeParam(int)
I don't know why the parent Widget takes the slot, instead of WidgetParamsWindows, do you have any ideas?
There is no Q_OBJECT macro in WidgetParamsWindow, so moc doesn't resolve slot macros, try to add Q_OBJECT in WidgetParamsWindow
Related
I'm trying to understand how to use signals to when one QCheckBox be checked it uncheck all other checkboxes present in the same QGroupBox
class GroupBox : public QGroupBox
{
public:
GroupBox(QWidget *parent = nullptr) : QGroupBox(parent)
{
}
public slots:
void uncheck();
};
class CheckBox : public QCheckBox
{
public:
CheckBox(QWidget *parent = nullptr) : QCheckBox(parent)
{
connect(this, SIGNAL(checked()), this, SLOT(checked()));
}
public slots:
void checked()
{
qDebug() << "checked";
}
};
When I click on one of the checkboxes it didn't go to the function checked().
QCheckBox inherits from QAbstractButton
You should use clicked or stateChanged signal instead of checked.
e.q.
connect(this, SIGNAL(stateChanged(int)), this, SLOT(checked(int)));
Btw; if using a modern Qt version, you should ditch the SIGNAL and SLOTS macros and instead use the new connect() syntax that's checked at compile time.
Refer: New Signal Slot Syntax
e.p.
connect(this, &QCheckBox::clicked, this, &CheckBox::checked);
I have a class ("controller" for example)
and in this class, I have created many objects of different other classes
with different parents.
How to send signal between that classes and "controller" to call a function in "controller" class?
#include "controller.h"
Controller::Controller(QObject *parent) : QObject (parent){
connect(sender(), SIGNAL(recivedCall(QString)), this, SLOT(alert(QString)));
}
void Controller::onCall(QJsonObject callinfo){
nodes[callinfo["username"].toString()]= new PanelManager();
nodes[callinfo["username"].toString()]->handleCallStateChanged(callinfo);
}
void Controller::alert(QString callinfo){
qDebug()<<callinfo;
}
For example, how to send signal from "recivedCall" in each "PanelManager" object to call "alert" function in "controller" class ?
The object which creates your two components has to set the connections between your signal and your slot. But, you shouldn't expose inner components (i.e. create getter to return a pointer on a attribute).
A way to tackle the last problem with Qt is to create a signal in your parent and let it broadcast the calls.
For example, if I need to connect a QCheckBox to a QLineEdit in two different widgets:
class Parent1: public QWidget
{
Q_OBJECT
public:
Parent1(): QWidget(), myCheckBox(new QCheckBox("Edit", this))
{
connect(myCheckBox, &QCheckBox::clicked, this, &Parent1::editForbidden);
}
private:
QCheckBox* myCheckBox;
signals:
void editForbidden(bool);
};
class Parent2: public QWidget
{
Q_OBJECT
public:
Parent2(): QWidget(), myLineEdit(new QLineEdit("Edit", this))
{
connect(this, &Parent2::forbidEdit, myLineEdit, &QLineEdit::setReadOnly);
}
private:
QLineEdit* myLineEdit;
signals:
void forbidEdit(bool);
};
// In the parent of Parent1 and Parent2 (or in the main if there is no parent)
QObject::connect(p1, &Parent1::editForbidden, p2, &Parent2::forbidEdit);
In this example, when I click on the checkbox in parent1, the lineEdit in parent2 is disabled. But, Parent1 doesn't know anything about Parent2.
I have class printrectangle
class PrintRectangle : public QWidget
{
Q_OBJECT
public:
explicit PrintRectangle(QWidget *parent = 0);
private:
void resetClickedIndex();
void updateIndexFromPoint( const QPoint& point);
public:
int mXIndex;
int mYIndex;
QVector<QPoint> points;
bool clicked[5][5] = {};
teacher tech;
perceptron p[5][5];
double techconst = 0.1;
signals:
public slots:
protected:
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *eventPress);
};
and MainWindow
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_learn_clicked();
void on_classify_clicked();
private:
Ui::MainWindow *ui;
};
When I click button I call to on_learn_clicked() function. I would like to transfer clicked[5][5] array into on_learn_clicked becasue I send this array to other object when user click button. How to do this?
It is not clear what is exactly the relation between MainWindow and the PrintRectangle widget. I suppose the button signal and PrintRectangle slot are connected somewhere in the MainWindow implementation.
One way to solve the problem would be to use to use the QSignalMapper as #Noidea stated.
Another way would be to use a lambda as a slot when connecting. This way you could capture the sender/receiver or other objects in scope and use their members.
You can find some information about the connect syntax in New Signal Slot Syntax
But basically you could write something like:
connect(button, &QPushButton::clicked, this, [this, printRectangle]()
{
// do smth with clicked[5][5] from printRectangle or just
// retrieve it and call another method like:
// this->processClicked(printRectangle->clicked);
// or pass it to another object
}
This way you could modify your on_classify_clicked slot to a regular method with bool[5][5] argument to do the processing.
I am using Qt and C++, after click on menu item second window appears, and after click on a button in second menu (slot saveData()), I want to change object (obj_map) of class MainMenu. Is it possible, and how to do it the best way? Because I now cannot modify obj_map, because it is in different class. I tried to do something with pointers, but the result was a segmentation fault.
Main Window:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
map obj_map;
public Q_SLOTS:
void saveMap();
private:
Ui::MainWindow *ui;
};
Other window which appears after click in on menu item in main window.
namespace Ui
{
class PreferencesWindow;
}
class PreferencesWindow : public QWidget
{
Q_OBJECT
public:
explicit PreferencesWindow(QWidget *parent = 0);
public Q_SLOTS:
void saveData();
private:
Ui::PreferencesWindow *uip;
};
From here I need to change obj_map
void PreferencesWindow::saveData()
{
// FROM HERE I NEED TO CHANGE obj_map
}
Preferences object is created in a slot:
void MainWindow::saveMap()
{
PreferencesWindow *p = new PreferencesWindow();
p->show();
}
You could use signals and slots: when saveData() is called, emit a signal, like emit saveDataClicked() and catch that signal in the MainWindow with a slot called change_obj_map. There, you can do your changes.
So, in MainWindow you can write:
connect (PreferencesWindow, SIGNAL(saveDataClicked()), this, SLOT(change_obj_map());
and then in the slot:
void change_obj_map()
{
// Do your changes here
}
Another way is having a local obj_map in PreferencesWindow that is a pointer to the address of obj_map in MainWindow. So, when you create PreferencesWindow, you can just pass the address of MainWindow's obj_map to the constructor and assign that address to the local variable obj_map.
As PreferencesWindow objects are created by MainWindow, the easiest is to have PreferencesWindow objects store a pointer to MainWindow:
class MainWindow;
class PreferencesWindow : public QWidget
{
Q_OBJECT
public:
explicit PreferencesWindow(MainWindow *parent = 0);
public Q_SLOTS:
void saveData();
private:
Ui::PreferencesWindow *uip;
MainWindow* m_mainwindow;
};
Pass the pointer upon construction:
void MainWindow::saveMap()
{
PreferencesWindow *p = new PreferencesWindow( this );
p->show();
}
Then use it:
PreferencesWindow::PreferencesWindow(MainWindow *parent) :
QWidget( parent ),
m_mainwindow( parent )
{
}
void PreferencesWindow::saveData()
{
// FROM HERE I NEED TO CHANGE obj_map
m_mainwindow->obj_map.....it's accessible!
}
I need to simulate mouse click on UI button using QTest but I can't figure out how to access it.
I've got a MediaPanel class :
class PhMediaPanel : public QWidget
{
Q_OBJECT
public:
explicit PhMediaPanel(QWidget *parent = 0);
//... a lot of functions
private:
Ui::PhMediaPanel *ui;
};
And a MediaPanelTest :
#include "MediaPanelTest.h"
#include <QObject>
class MediaPanelTest : public QObject
{
Q_OBJECT
public:
explicit MediaPanelTest(QObject *parent = 0);
private slots:
//The tests
};
So how can I simulate button click on Ui::PhMediaPanel *ui member?
Try the following approach:
BUTTONCLASS* button = WIDGET->findChild<BUTTONCLASS*>("name of the button");
As far is i know this should give you the widget without exposing the UI pointer.