Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Need some help in Signal and Slots Connections.

Need some help in Signal and Slots Connections.

Scheduled Pinned Locked Moved Mobile and Embedded
29 Posts 4 Posters 10.6k 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.
  • V Offline
    V Offline
    VPellas
    wrote on last edited by
    #18

    Alright, here i am again with my Final Problem. I had to make some changes in my Final Program and now it's the Final Step to complete it. My problem now is Taking an int Value from a Second Window and add the Value to a Label at the First Window. Let's take a look at the U.I. of the program:
    1st Screen : !http://s10.postimg.org/3mwa4ipc9/Main_Window1.jpg()!
    2nd Screen: http://postimg.org/image/kp27co4d9/

    Now. How I want it To Work. First Of all User Presses the Record Button at MainWindow1. MainWindow2 Pops up, User Writes an Int Value on the slot, and when the Done Button is Clicked i want this Value to be copied and added to the previous Value that MainWindow1's label had.

    Here is my Code:
    mainwindow1.h
    @#ifndef MAINWINDOW1_H
    #define MAINWINDOW1_H
    #include <QWidget>
    #include <QPushButton>
    #include<mainwindow2.h>
    namespace Ui {
    class MainWindow1;
    }

    class MainWindow1 : public QWidget
    {
    Q_OBJECT

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

    public slots:
    void openNewWindow();

    private:
    Ui::MainWindow1 *ui;
    class MainWindow2 *mMyNewWindow;

    private slots:
    void on_Record_clicked();
    };

    #endif // MAINWINDOW1_H@

    mainwindow2.h
    @#ifndef MAINWINDOW2_H
    #define MAINWINDOW2_H
    #include <QWidget>
    #include<mainwindow1.h>
    #include <QPushButton>
    namespace Ui {
    class MainWindow2;
    }

    class MainWindow2 : public QWidget
    {
    Q_OBJECT

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

    public slots:
    void openOldWindow();

    private:
    Ui::MainWindow2 *ui;
    class MainWindow1 *mMyOldWindow;

    private slots:
    void on_Done_clicked();

    };

    #endif // MAINWINDOW2_H@

    mainwindow1.cpp
    @#include "mainwindow1.h"
    #include "ui_mainwindow1.h"
    #include <QWidget>
    #include <QPushButton>
    MainWindow1::MainWindow1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWindow1)
    {
    ui->setupUi(this);
    QObject::connect(ui->Record, SIGNAL( clicked() ), this, SLOT(on_Record_clicked()));
    }

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

    void MainWindow1::openNewWindow()
    {
    mMyNewWindow = new MainWindow2();

    mMyNewWindow->show();
    

    }

    void MainWindow1::on_Record_clicked()
    {
    openNewWindow();
    }
    @

    mainwindow2.cpp

    @#include "mainwindow2.h"
    #include "ui_mainwindow2.h"
    #include <QWidget>
    #include <QPushButton>
    MainWindow2::MainWindow2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWindow2)
    {
    ui->setupUi(this);

    QObject::connect(ui->Done, SIGNAL( clicked() ), this, SLOT(on_Done_clicked()));
    

    }

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

    void MainWindow2::openOldWindow()
    {
    mMyOldWindow = new MainWindow1();

    mMyOldWindow->show();
    

    }

    void MainWindow2::on_Done_clicked()
    {
    //Here I am taking the Value that user Entered in the "Score1" as Int
    //Now the problem is that i Want this Value to be added to the previous Window
    //At the Label Slot
    //QString mystring1(ui->Score1->text());
    //int c = mystring1.toInt();

    openOldWindow();
    

    }
    @

    Thanks for your help :)

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VPellas
      wrote on last edited by
      #19

      I've made some progress, although I am still waiting for your help :-/ ... Let's take a look again at the Ui of MainWindow1 and MainWindow2:
      MainWindow1 : !http://s18.postimg.org/53h2p1w0p/Main_Window1.jpg()!
      MainWindow2 : !http://s22.postimg.org/oy6xeu7mp/Main_Window2.jpg()!

      So, My New Goal is to make the proccess that i will explain now work.

      1. User Open the Program
      2. He/she presses the Recount Button and MainWindow2 is coming Up
      3. He/she Enteres a Value at the "QlineEdit" Widget
      4. When he/she presses the Done Button I need the program to Save the Value that user entered, and send it to the MainWindow1
      5. After Pushing the Done Button MainWindow1 is coming up again
      6. When User presses the Count Button i want the Value that he/she entered to appear at one of the Qlabels (you can check them on photos).

      I managed to have a progress, but still it doesn't work, It seems like values are reset after Windows Changes. Have a look at the Code now:

      MainWindow1.h
      @#ifndef MAINWINDOW1_H
      #define MAINWINDOW1_H
      #include <QWidget>
      #include <QPushButton>
      #include<mainwindow2.h>
      namespace Ui {
      class MainWindow1;
      }

      class MainWindow1 : public QWidget
      {
      Q_OBJECT

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

      public slots:
      void openNewWindow();
      void on_Count_clicked();
      void on_Record_clicked();
      void Proccess(int a);

      private:
      Ui::MainWindow1 *ui;
      class MainWindow2 *mMyNewWindow;

      private slots:

      };

      #endif // MAINWINDOW1_H@

      MainWindow2.h
      @#ifndef MAINWINDOW2_H
      #define MAINWINDOW2_H
      #include <QWidget>
      #include<mainwindow1.h>
      #include <QPushButton>
      namespace Ui {
      class MainWindow2;
      }

      class MainWindow2 : public QWidget
      {
      Q_OBJECT

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

      public slots:
      void openOldWindow();
      void on_Done_clicked();
      int GetNumber();

      private:
      Ui::MainWindow2 *ui;
      class MainWindow1 *mMyOldWindow;

      private slots:

      };

      #endif // MAINWINDOW2_H@

      MainWindow1.cpp
      @#include "mainwindow1.h"
      #include "ui_mainwindow1.h"
      #include "mainwindow2.h"
      #include <QWidget>
      #include <QPushButton>

      MainWindow1::MainWindow1(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::MainWindow1)
      {
      ui->setupUi(this);
      QObject::connect(ui->Record, SIGNAL( clicked() ), this, SLOT(on_Record_clicked()));
      QObject::connect(ui->Count, SIGNAL( clicked() ), this, SLOT(on_Count_clicked()));
      }

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

      void MainWindow1::openNewWindow()
      {
      mMyNewWindow = new MainWindow2();
      mMyNewWindow->show();

      }

      void MainWindow1::on_Record_clicked()
      {
      openNewWindow();
      }

      void MainWindow1::Proccess(int a)
      {
      ui->Test->setNum(a);
      }

      void MainWindow1::on_Count_clicked()
      {
      int number;
      MainWindow2 key2;
      key2.on_Done_clicked();
      }@

      MainWindow2.cpp
      @#include "mainwindow2.h"
      #include "ui_mainwindow2.h"
      #include <QWidget>
      #include <QPushButton>
      #include <QString>
      MainWindow2::MainWindow2(QWidget *parent) :

      QWidget(parent),
      ui(new Ui::MainWindow2)
      

      {
      int p;
      ui->setupUi(this);
      p = GetNumber();
      MainWindow1 key;
      key.Proccess(p);

      QObject::connect(ui->Done, SIGNAL( clicked() ), this, SLOT(on_Done_clicked()));
      

      }

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

      int MainWindow2::GetNumber()
      {
      QString mystring1(ui->Score1->text());
      int c = mystring1.toInt();

      ui->label->setNum(c);
      
      return c;
      

      }

      void MainWindow2::on_Done_clicked()
      {
      int k;
      k = GetNumber();
      MainWindow1 key1;
      key1.Proccess(k);
      openOldWindow();
      }

      void MainWindow2::openOldWindow()
      {
      mMyOldWindow = new MainWindow1();
      mMyOldWindow->show();
      }@

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

        Hi,

        It seems that you are creating new widgets every time you click on a button. May I suggest to simplify things a bit ? It seems that what you want to do with MainWindow2 already exists as "QInputDialog":http://qt-project.org/doc/qt-4.8/qinputdialog.html

        I would also suggest to have a look at the examples from the Qt documentation to better understand how widget interaction may be achieved.

        Hope it helps

        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
        • V Offline
          V Offline
          VPellas
          wrote on last edited by
          #21

          I saw the QInputDialog , I read the whole forum, but I can't understand how I can connect this with my program.

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

            Read the "example":http://qt-project.org/doc/qt-4.8/dialogs-standarddialogs.html

            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
            • V Offline
              V Offline
              VPellas
              wrote on last edited by
              #23

              Hmmm. It's interesting. But, is it possible to do it with Widgets? As you can understand this is just the "problem" i have in my main Program. In there when i Press the Recount Button a New Window(main Class QWidget) pops up in which the user enter the values from a Calculator-type keyboard I created...

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

                Sure it is, apply the same techniques used by QInputDialog.

                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
                • V Offline
                  V Offline
                  VPellas
                  wrote on last edited by
                  #25

                  After reading tons of posts, questions, answers, i realized that what i need to do is to make my own "QInputDialog::getInteger()" . I read the example you gave me , I Isolated the part that i am interested in, which is to take Int from the pop up dialog that shows up, but i also realized that this is a Standar procedure in Qt Libraries and i cannot change the Widgets used in it. So is there a way to make MainWindow2 on my code to be my own "QInputDialog::getInteger()" - style Dialog?

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

                    Read the source from QInputDialog, you'll see the pattern used and you can then replicate it for your purpose.

                    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
                    • V Offline
                      V Offline
                      VPellas
                      wrote on last edited by
                      #27

                      Ok problem solved

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        koleygr
                        wrote on last edited by
                        #28

                        For them who want to know how the problem solved:

                        The easier way to take something of your dialog back to your mainwindow is to
                        define an external variable at the "top" of your mainwindow.cpp (for example @extern int c@)
                        and then just to pass it to your dialog (just @int c@ at the top of your dialog cpp).
                        For more details search for external variables.

                        I know that we should avoid external declarations, but this way the life of a
                        non-profesional programmer... is realy much easier....

                        This method, interrupts and the problem of arguments in SLOTS...
                        you don't realy need them.

                        Using:Qt Creator 2.4.1 under UBUNTU

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          koleygr
                          wrote on last edited by
                          #29

                          Another way, is to use a public (non void) function in your dialog
                          and to call this function (after executing the dialog) fron your main window.

                          P.S.: If someone need the code,
                          just ask me to post it.

                          Using:Qt Creator 2.4.1 under UBUNTU

                          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