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. Signal and slot wrong value being sent
QtWS25 Last Chance

Signal and slot wrong value being sent

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorsignal & slotbuttonqlineedit
5 Posts 3 Posters 2.1k Views
  • 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.
  • L Offline
    L Offline
    Lasith
    wrote on 16 Sept 2017, 06:22 last edited by Lasith
    #1

    I have written a small program to send data from one form(MainWindow) to another(Dialog) upon a button click. When the button is clicked the value written in the lineedit of MainWindow is to be displayed on a label in Dialog form!
    When I click the button a value is displayed on the label but it is not the same as the value entered in the line edit!
    following are the respective codes in the 2 header and 2 cpp files!

    MainWindow.h
    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    signals:
    void sendIntData(int data);
    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    MainWIndow.cpp

    void MainWindow::on_pushButton_clicked()
    {
    Dialog *dialog1=new Dialog(this);

    dialog1->setModal(true);
    dialog1->exec();
    
    int o=ui->lineEdit->text().toInt();
    
    emit sendIntData(o);
    
    connect(this, SIGNAL(sendIntData(int)),dialog1, SLOT(setIntData(int)));
    

    }

    Dialog.h
    class Dialog : public QDialog
    {
    Q_OBJECT

    public slots:
    void setIntData(int data);

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

    Dialog.cpp
    DIalog::DIalog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DIalog)
    {
    ui->setupUi(this);
    QString value=QString::number(index);
    ui->label->setText(value);
    }

    Dialog::~Dialog()
    {
    delete ui;
    }
    void Dialog::setIntData(int data)
    {
    index=data;

    }

    eg-When I click 3 and press the button I get a value 7237481! How can I correct this?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 16 Sept 2017, 06:52 last edited by mrjj
      #2

      hi
      You emit signal before you connect and you leak :)

      So try something like this

      void MainWindow::on_pushButton_clicked()
      {
      int value=ui->lineEdit->text().toInt(); // get value
      Dialog dialog1; // use uses exec() so no need for new 
      connect(this, SIGNAL(sendIntData(int)), &dialog1, SLOT(setIntData(int))); // connect first
      emit sendIntData(value); // then emit
      dialog1.exec(); // show it and wait for cancel/ok
      }
      
      L 1 Reply Last reply 16 Sept 2017, 06:57
      1
      • M mrjj
        16 Sept 2017, 06:52

        hi
        You emit signal before you connect and you leak :)

        So try something like this

        void MainWindow::on_pushButton_clicked()
        {
        int value=ui->lineEdit->text().toInt(); // get value
        Dialog dialog1; // use uses exec() so no need for new 
        connect(this, SIGNAL(sendIntData(int)), &dialog1, SLOT(setIntData(int))); // connect first
        emit sendIntData(value); // then emit
        dialog1.exec(); // show it and wait for cancel/ok
        }
        
        L Offline
        L Offline
        Lasith
        wrote on 16 Sept 2017, 06:57 last edited by
        #3

        @mrjj Thanx alot mate it worked :)

        1 Reply Last reply
        1
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 16 Sept 2017, 21:11 last edited by
          #4

          Hi,

          In this case, why not just call setIntData (value); ?

          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
          1
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 16 Sept 2017, 21:42 last edited by
            #5

            Hi
            Yes calling the function directly is actually a better solution in this case.
            Its worth mentioning that the so called slots are 100% normal c++ member functions
            and can be called without any Qt involvement/use of.

            void MainWindow::on_pushButton_clicked() {
            int value=ui->lineEdit->text().toInt(); // get value
            Dialog dialog1; 
            dialog1.sendIntData(value); 
            dialog1.exec(); 
            }
            
            1 Reply Last reply
            0

            1/5

            16 Sept 2017, 06:22

            • Login

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