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. [SOLVED] help with signal and slot
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] help with signal and slot

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.2k 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.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

    The following code runs good without any errors but the signal and slot is not working correctly. In the code, the dialog calls the click signal and in the Dialog the function returns a variable. at the mainwindow, the slot should be then called but it is not. Below is a basic example about what i am trying to do. if the slot works correctly it then sets the text to the pushbutton on the mainwindow. I have verified that the function clicked at the mainwindow is never called. Your help is greatly appreciated. thank you in advanced.

    dialog.cpp
    @#include "dialog.h"
    #include "ui_dialog.h"
    #include "mainwindow.h"

    QString pass1 = "pass";
    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    clicked();
    }

    Dialog::~Dialog()
    {
    delete ui;
    }

    const QString Dialog::clicked()
    {
    emit click();
    return pass1;
    }
    @

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialog.h"
    QString pass;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    tt = new Dialog(this);
    this->connect(tt, SIGNAL(click()), this, SLOT(clicked()));
    tt->show();
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::clicked(){
    pass = tt->clicked();
    ui->pushButton->setText(pass);
    }@

    dialog.h
    @#ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

    const QString clicked() ;
    private slots:

    signals:
    void click();

    private:
    Ui::Dialog *ui;
    };

    #endif // DIALOG_H@

    mainwindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    class Dialog;
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void clicked();

    private:
    Ui::MainWindow *ui;
    Dialog *tt;
    };

    #endif // MAINWINDOW_H@

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      A few issues here.

      Firs of all, in mainwindow.h, declare clicked() like that:
      @
      public slots:
      void clicked();
      @

      Then, look into your implementation: when click() is emitted, it will invoke mainwindow's clicked(), which will in turn invoke dialog's method of the same name, and finally it will re-emit click() signal. Sounds like a loop to me, but I might have overlooked something.

      (Z(:^

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        Another problem is, you call clicked() in dialog constructor, which is called in mainwindow BEFORE signal-slot connection. Therefore, your initial signal will not be caught by main window. Mind you, making the connection before dialog instantiation is also wrong - you have to rethink the whole thing.

        (Z(:^

        1 Reply Last reply
        0
        • ZlatomirZ Offline
          ZlatomirZ Offline
          Zlatomir
          wrote on last edited by
          #4

          The problem is that you call Dialog::clicked (the function that emits the signal) from the constructor and then you have:
          @
          tt = new Dialog(this); //signal emited and call all connected slots (NONE)
          this->connect(tt, SIGNAL(click()), this, SLOT(clicked())); //and after the first signal emit you connect
          @

          https://forum.qt.io/category/41/romanian

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kalster
            wrote on last edited by
            #5

            i got it working. i solved it with the first reply advice plus i did not have a click event. thank you for your help.

            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