synthrom
synthrom

Reputation: 430

Connect not working properly while using QDialog as a popup

I am having a problem with connect in the program I am currently writing. I first create a "main window" dialog that contains buttons, line edits, etc. (which all work perfectly fine with my custom slots). One of the buttons (the "Add Class" button) should create a new pop up dialog that is a child of the mainWindow dialog. I wrote a new .h and .cpp for this new dialog (addClass.h and addClass.cpp). When I click the button, the dialog modality is set to ApplicationModal and up to this point, the code works; when I click "Add Class" and new dialog shows up as a pop up with all the labels, line edits, and buttons that I want. The problem comes in when I try and use the connect using this new class. Upon clicking the ok button the connect is not executed. The program compiles properly (using qmake and then make) and gives no errors during run time. I also took the .h and .cpp files from the pop up dialog and tested them with their own main.cpp and the connect worked perfectly. I am stumped as to what the problem could be, so any help would be awesome!

Here are some snipets of code that might be helpful:

the custom slot that initiates the pop up dialog in mainWindow.cpp (works and I include "addClass.h" in mainWindow.cpp):

void mainWindow::addClassCombo(){
    addClass aC(win);
}

addClass.h:

#ifndef ADDCLASS_H
#define ADDCLASS_H

#include <QDialog>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QString>

class addClass : public QDialog{

    Q_OBJECT

public:
    addClass(QWidget *parent = 0);

private slots:
    void addToTxt();

private:
    QDialog *addMathClass;
    QVBoxLayout *mainLayout;
    QHBoxLayout *layoutOkCanc;
    QLabel *nameL;          //label for name of math class to be added
    QLineEdit *name;        //line edit for name
    QPushButton *ok;        //ok button
    QPushButton *canc;      //cancel button
};

#endif

addClass.cpp (works with its own main.cpp but not the one with my mainWindow.cpp):

#include <QtGui>
#include <QTextStream>

#include "addClass.h"

#include <iostream>

addClass::addClass(QWidget *parent):QDialog(parent){

    addMathClass = new QDialog(parent);

    mainLayout = new QVBoxLayout(addMathClass);
    layoutOkCanc = new QHBoxLayout();

    nameL = new QLabel("Math Class Name:");
    name = new QLineEdit;
    nameL->setBuddy(name);

    ok = new QPushButton("Ok");
    canc = new QPushButton("Cancel");

    QObject::connect(canc, SIGNAL(clicked()), addMathClass, SLOT(close()) ); //<-works
    QObject::connect(ok, SIGNAL(clicked()), this, SLOT(addToTxt()) );     //<-doesn't work
    QObject::connect(ok, SIGNAL(clicked()), addMathClass, SLOT(close()) );  //<-works

    layoutOkCanc->addStretch();
    layoutOkCanc->addWidget(ok);
    layoutOkCanc->addWidget(canc);

    mainLayout->addWidget(nameL);
    mainLayout->addWidget(name);
    mainLayout->addLayout(layoutOkCanc);

    addMathClass->setWindowModality(Qt::ApplicationModal);
    addMathClass->setWindowTitle("Add Class");
    addMathClass->show();
}

void addClass::addToTxt(){
    std::cout<<"testing"<<std::endl;
}

Upvotes: 2

Views: 1133

Answers (1)

Kamil Klimek
Kamil Klimek

Reputation: 13130

Your addClass aC(win); goes out of scope and is being destroyed. Connection works, but after object destruction is disconnected. That's why you're not getting slot called

Upvotes: 1

Related Questions