Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why I see: Object::connect: No such slot QDialog::showName(QString) in
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 8Observer88 Offline
    8Observer88 Offline
    8Observer8
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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).

      157

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • 8Observer88 Offline
          8Observer88 Offline
          8Observer8
          wrote on last edited by
          #4

          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]

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

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

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • 8Observer88 Offline
              8Observer88 Offline
              8Observer8
              wrote on last edited by
              #6

              You are right. I see. Thanks!

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved