Why I see: Object::connect: No such slot QDialog::showName(QString) in



  • Hi,

    Why I see: Object::connect: No such slot QDialog::showName(QString) in ../DialogByButton/mainwindow.cpp:29

    DialogByButton.pro
    [CODE]
    SOURCES +=
    main.cpp
    mainwindow.cpp
    dialog.cpp

    HEADERS +=
    mainwindow.h
    dialog.h
    [/CODE]

    dialog.h
    [CODE]
    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>
    #include <QtGui>

    class Dialog : public QDialog
    {
    Q_OBJECT
    public:
    explicit Dialog(QWidget *parent = 0);

    signals:

    public slots:
    void showName(QString name);

    private:
    QLabel *lblName;
    };

    #endif // DIALOG_H
    [/CODE]

    dialog.cpp
    [CODE]
    #include "dialog.h"

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent)
    {
    // Controls
    lblName = new QLabel(tr("Hahah"));

    // Layouts
    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(lblName);
    
    // Set Layout
    this->setLayout(mainLayout);
    

    }

    void Dialog::showName(QString name) {
    //lblName->setText(name);
    }
    [/CODE]

    mainwindow.h
    [CODE]
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtGui>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    explicit MainWindow(QWidget *parent = 0);

    signals:
    void sendName(QString name);

    private slots:
    void callDialog();

    private:
    QLineEdit *leName;
    };

    #endif // MAINWINDOW_H
    [/CODE]

    mainwindow.cpp
    [CODE]
    #include "mainwindow.h"
    #include "dialog.h"
    #include <QWidget>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    // Controls
    QLabel *lblName = new QLabel(tr("Enter your name:"));
    leName = new QLineEdit;
    QPushButton *btnCallDialog = new QPushButton(tr("Call Dialog"));

    // Layout
    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(lblName, 0, 0);
    mainLayout->addWidget(leName, 0, 1);
    mainLayout->addWidget(btnCallDialog, 1, 0, 1, 2);
    
    // Set center widget
    QWidget *widget = new QWidget;
    widget->setLayout(mainLayout);
    this->setCentralWidget(widget);
    
    connect(btnCallDialog, SIGNAL(clicked()), this, SLOT(callDialog()));
    

    }

    void MainWindow::callDialog() {
    QDialog *dialog = new QDialog(this);
    connect(this, SIGNAL(sendName(QString)), dialog, SLOT(showName(QString)));
    emit sendName(leName->text());
    dialog->show();
    }
    [/CODE]

    main.cpp
    [CODE]
    #include <QApplication>
    #include "mainwindow.h"

    int main (int argc, char *argv[]) {
    QApplication app(argc, argv);

    MainWindow window;
    window.show();
    
    return app.exec();
    

    }
    [/CODE]

    Thank you!


  • Moderators

    Hi,

    Because you are creating Object of QDialog and it doesnot have Slot showName(QString). It should be Dialog which is your class and it contains showName(QString).


  • Moderators

    because - as the error message says - the QDialog class doesn't have such a slot.
    @
    QDialog *dialog = new QDialog(this);
    connect(this, SIGNAL(sendName(QString)), dialog, SLOT(showName(QString)));
    @

    You should rather create a instance of your class ("Dialog") where you defined this slot rather than QDialog.



  • Thank you very much.

    I write instead:
    [CODE]
    void MainWindow::callDialog() {
    QDialog *dialog = new QDialog(this);
    connect(this, SIGNAL(sendName(QString)), dialog, SLOT(showName(QString)));
    emit sendName(leName->text());
    dialog->show();
    }[/CODE]

    This code:
    [CODE]
    void MainWindow::callDialog() {
    Dialog dialog;
    connect(this, SIGNAL(sendName(QString)), &dialog, SLOT(showName(QString)));
    emit sendName(leName->text());
    dialog.exec();
    }}[/CODE]


  • Lifetime Qt Champion

    Hi,

    With the last version of your code, there is no need for the connection just use:

    @dialog.showName(leName->text());@



  • You are right. I see. Thanks!


Log in to reply
 

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