Custum signal in QDialog does not launch a slot in another class
-
I have three class:
- class A is the main class.
- class B inherits from QWidget.
- class C inherits from QDialog.
In the C class there is a QTableWidget ( QTableWidget* tableShape;) and custom slot (void saveTableValue(int r,int c);) and signal (void load();). The saveTableValue slot is called by B class when the user changes a cell (by the signal cellChanged(int,int);). When the saveTableValue slot is executed another signal (void load();) in C class is emitted and calls the custom slot void reloadIfGeometryChanged(); in A class.
The problem is that that slot void reloadIfGeometryChanged(); (*****) in A class is not executed even though the signal void load(); is emitted (I tested it with a slot in the same class (C) ).
Here part of my code:
class A.h
@class Main_newUI : public QMainWindow
{
Q_OBJECTpublic:
Main_newUI(QString pathPj, QWidget *parent = 0);
~Main_newUI();// Geometry Docks Geometry_UI *geometryDocks; //!< Create and manage geometry docks ShapeDialog *profileDialog;
public slots:
void reloadIfGeometryChanged();
};
}#endif // MAIN_NEWUI_H
@class A.cpp
@Main_newUI::Main_newUI(QString pathPj, QWidget *parent)
: QMainWindow(parent)
{geometryDocks = new Geometry_UI(...); profileDialog = geometryDocks->profDialog; connect(profileDialog, SIGNAL(load()), this, SLOT(reloadIfGeometryChanged())); (******DOES NOT WORK!!!)
}
void Main_newUI::reloadIfGeometryChanged(){
.......
}
@class B.h
@class Geometry_UI : public QWidget
{
Q_OBJECT
public:
explicit Geometry_UI(...);
~Geometry_UI();QMainWindow *mainWindow; ShapeDialog* profDialog;
};
}#endif // GEOMETRY_UI_H
@class B.cpp
@Geometry_UI::Geometry_UI(...)
{
profDialog = new ShapeDialog();
}void Geometry_UI::update(){
QTableWidget* profileTable = new QTableWidget(profDialog);
profDialog = new ShapeDialog(profBtn, profileTable);
connect(profileTable, SIGNAL(cellChanged(int,int)), profDialog, SLOT(saveTableValue(int,int)));
}
@class C.h
@class ShapeDialog : public QDialog {
Q_OBJECT
public:
ShapeDialog(QWidget* parent = 0, QTableWidget* tableSh = NULL);~ShapeDialog(); QTableWidget* tableShape;
public slots:
void saveTableValue(int r,int c);signals:
void load();
};#endif // SHAPEDIALOG_H
@class C.cpp
@ShapeDialog::ShapeDialog(QWidget* parent, QTableWidget* tableSh)
: QDialog(parent),
tableShape(tableSh){}void ShapeDialog::saveTableValue(int r,int c){
.... emit load();
}
@Can someone help me please?
-
check the return value of the connect statement
check the output on the console
this gives you a pretty clear reason whats going wrong.
-
Thank you raven-worx, I have checked if the connect statement works and it does (the bool is 1) and I ve printed something in the saveTableValue slot and it works also....
-
when the connect() returns true the connection is fine and everything will work.
So there must be something else... the posted code looks good. -
exactely I cant find out what's wrong. I tried to emit the same signal in B class and connected in the A class, and it works! the only difference I see is that the case that doesn't work is a QDialog!! what's wrong with that?!
-
should also work. even if you call QDialog::exec() to show it.
Did you try a full rebuild of your project? -
What do you mean with "even if you call QDialog::exec() to show it."?
yes I tried twice to rebuild...nothing
-
[quote author="valeSimu" date="1422369443"]What do you mean with "even if you call QDialog::exec() to show it."?[/quote]
because QDialog::exec() opens a new event-loop. Nevermind.
I don't have any more hints for you sry.
-
Could you show where do you show or exec profDialog.
Is profHullDialog related to profDialog? -
Another question.
How many times do you call Geometry_UI::update()?
In current implementation you create new table on each update. -
- in class B.cpp show the profDialog in thi way
@connect(profBtn, SIGNAL(clicked()), profDialog, SLOT(show()));@
-
and yes proHullDialog and profDialog are the same, I corrected it, thx
-
yes the table is created for every element it finds in a list, for this reason update is called many times
-
The connect in MainWindow ctor uses a Geomentry_UI::profDialog pointer allocated in Geometry_UI ctor.
In Geometry_UI::update() you lose that pointer and allocate another and you use that new pointer to connect with a table widget.
You need to rethink the connections and allocations.@
Main_newUI::Main_newUI(QString pathPj, QWidget *parent)
: QMainWindow(parent)
{
geometryDocks = new Geometry_UI(...); // Creates a pointer to profDialog
profileDialog = geometryDocks->profDialog;
connect(profileDialog, SIGNAL(load()), this, SLOT(reloadIfGeometryChanged())); // Uses that pointer to make a connection.
}void Geometry_UI::update()
{
QTableWidget* profileTable = new QTableWidget(profDialog);
profDialog = new ShapeDialog(profBtn, profileTable); // The pointer previously allocated in ctor is lost here.
connect(profileTable, SIGNAL(cellChanged(int,int)), profDialog, SLOT(saveTableValue(int,int))); // profileTable is connected to new profDialog that is not connected with MainWindow.
}@