Connect in Subclass dont work !



  • @
    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, SLOT(slot_pushButton()));
    }

    @

    @
    void Dummy::slot_pushButton(){

    pushButton->hide();

    }
    @

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


  • Lifetime Qt Champion

    Hi,

    Your Dummy widget goes out of scope at the end of the constructor so it's destroyed.

    Your application design looks a bit strange, what are you trying to achieve ?



  • uhmm I dont really understand of what you are saying.

    Could you post some code of how to fix it?

    I want to split my big main class into many sub-classes,
    otherwise it's getting too messy.

    thanks in advance :)



  • So when you use the magic words "this->parent()" instead of "this"

    And when the "slot_pushButton()" is declared and defined in the class "Lines", it does work.

    but I want to *activate the Slot *in the Subclass.

    How to do this?


  • Lifetime Qt Champion

    Allocate your Dummy on the heap rather than the stack.

    But I would encourage you to go through Qt's application examples and demos to see how you can split classes in functional elements.

    Your current implementation has already started to create dependencies between parent and children that should not exists. You will get a huge pile of complicated and hard to maintain code. You should rather first separate functionalities, build a widget and/or business logic for each of them, and only when you have all the separated pieces working, put everything together.

    Hope it helps


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.