PDA

View Full Version : Connect Slot in Subclass dont work !



MrNoway
2nd September 2013, 06:44
class Lines : public QMainWindow {
Q_OBJECT
public:
Lines(QWidget *parent = NULL);
~Lines();
QWidget *widget_main;};

Lines::Lines(QWidget *parent) : QMainWindow ( parent ){
ui.setupUi(this);
widget_main = new QWidget(ui.centralwidget);
Dummy d(this);
}




class Dummy : public QMainWindow {
Q_OBJECT
public:
QPushButton *pushButton;
explicit Dummy(Lines *parent = 0);

public slots: void slot_pushButton(); };

Dummy::Dummy(Lines *parent) : QMainWindow(parent){
pushButton = new QPushButton(parent->widget_main);
connect(pushButton, SIGNAL(clicked()), this->parent(), SLOT(slot_pushButton()));
}

void Dummy::slot_pushButton(){

pushButton->hide();


}

WHatever I do, I never jump into the slot_pushButton()

However, If I declare and define slot_pushButton() in the class Lines it does work,
but this is not what I want. I want to reach the slot_pushButton in the Subclass

wagmare
2nd September 2013, 06:49
connect(pushButton, SIGNAL(clicked()), this->parent(), SLOT(slot_pushButton()));
this->parent() is the problem .. the third argument
make just this ..

MrNoway
2nd September 2013, 06:55
I tried just "this" and it doesnt work.



If I make this->parent() I can AT LEAST connect it to the slot of the class Lines.

If I make only "this" nothing works at all.

ChrisW67
2nd September 2013, 07:44
The Dummy object is constructed and connects a clicked() signal to the slot_pushButton() of its parent() QObject. In the code we can see that would be an object of class Lines. That class does not have a slot called slot_pushButton() and you should be seeing a run time warning to that effect. Connections between QObjects are destroyed when the object at either end is destroyed but since this connection is never made...

If you connect to clicked() signal to the slot of the Dummy class, i.e. this, you will at least establish a connection (as wagmare points out). However, your Dummy object is created, constructed and destroyed in the space of one source code line. Connections between QObjects are destroyed when the object at either end is destroyed, in this case the Dummy object is destroyed. The push button widget itself only persists because you are parenting it to another widget that persists past the demise of Dummy.

MrNoway
2nd September 2013, 08:03
I read your answer carefully, ChrisW67
and I think I understood it.

So it is simply not possible to do it the way I wanted, right??


uhmmm...
I tried something similar.

Instead of calling my Dummy class from the Constructor of the Line class,
I called my Dummy class from the main.

Main
int main (int argc, char*argv[] )
{
QApplication a(argc, argv);
a.connect (&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
Lines w;
Dummy d;
w.show();
return a.exec();



If I do like this, I can use the Slots in the Dummy class, but I cannot paint the pushbutton from my Dummy class on the widget from my Mainclass.



pushButton = new QPushButton(parent->widget_main); //<<-- here the debugger crashes


Any Idea how to solve it?

This is what I want to achieve if you shouldnt understand me

9520

http://s14.directupload.net/images/130902/4t2yahaa.jpg
http://s14.directupload.net/images/130902/temp/4t2yahaa.jpg (http://s14.directupload.net/file/d/3368/4t2yahaa_jpg.htm)

ChrisW67
2nd September 2013, 09:56
Yes, make Dummy a QWidget and insert an instance of Dummy into the layout of the widget you wish to contain it. Just like every example in the Qt documentation.

MrNoway
2nd September 2013, 19:10
uhmm, could you be so kind and explain me how to insert the instance?

It just crashes every time I reach the

"pushButton = new QPushButton(parent->widget_main);"

anda_skoa
3rd September 2013, 10:11
It just crashes every time I reach the

"pushButton = new QPushButton(parent->widget_main);"

it crashed because parent is a null pointer.

If you want to access the pointer, pass a valid pointer.

Even better follow ChrisW67's advice: make Dummy a QWidget subclass and properly put it into your central widget.

Cheers,
_