[SOLVED]Qt signals and slots in different classes



  • I made a public slot in this class:
    @class open : public QObject
    {
    Q_OBJECT

    public:
    open();
    QString filename;
    QByteArray ba;
    public slots:
    void openfile(){
    filename=QFileDialog::getOpenFileName(
    0,
    QObject::tr("Open File"),
    ("C://"),
    ("BITMAP(*.bmp)")
    );
    QFile file(filename);

            file.open(QIODevice::ReadOnly);
            ba=file.readAll();
    
    
    }
    

    };@
    I want to connect this slot to my pushbutton's signal in the mainwindow.cpp like this:
    @open x;
    connect(ui->pushButton,SIGNAL(clicked()),
    &x,SLOT(openfile()));
    @
    But nothing happend when i pushed the button. What am is doing wrong?


  • Moderators

    You create "x" on stack and without a parent. It will be destroyed when it goes out of scope, and so it will not respond to the signal.



  • [quote author="sierdzio" date="1397126276"]You create "x" on stack and without a parent. It will be destroyed when it goes out of scope, and so it will not respond to the signal.[/quote]

    Thank you.


Log in to reply
 

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