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.
  • ZlatomirZ Offline
    ZlatomirZ Offline
    Zlatomir
    wrote on last edited by
    #5

    Also all the calls that access the widgets should be after the setupUi call, so there is a mistake that crashes your program:
    @
    Mypage::Mypage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Mypage)
    {
    //this line should use ui-> and it should be placed after setupUi call
    //numberBox->setRange(0, 1000);

    ui->setupUi(this);
    
    ui->numberBox->setRange(0, 1000);
    QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));
    

    }
    @

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

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

      Okey, I corrected the .h and the .cpp files as you told. Now the problem is at the void Mypage::pushButtonClicked() i think. label and numberBox was the pointers i used in the class from .h The names of QWidgets i have in Designer are : label, spinBox and pushButton

      I deleted the QLabel, QSpinbox and QPushButtons from the .h so now it is:
      .h
      @#ifndef MYPAGE_H
      #define MYPAGE_H

      #include <QWidget>
      #include <QPushButton>
      #include <QLabel>
      #include <QSpinBox>

      class QLabel;
      class QSpinBox;
      class QPushButton;

      namespace Ui {
      class Mypage;
      }

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

      private slots:
      void pushButtonClicked();

      private:
      Ui::Mypage *ui;

      };

      #endif // MYPAGE_H
      @

      and the cpp is :
      @#include "mypage.h"
      #include "ui_mypage.h"
      #include <QtGui>
      #include <QPushButton>
      #include <QLabel>
      #include <QSpinBox>

      Mypage::Mypage(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::Mypage)
      {
      ui->setupUi(this);
      ui->spinBox = new QSpinBox;
      ui->spinBox->setRange(0, 1000);
      ui->label= new QLabel;

      QObject::connect(ui -> pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));
      

      }

      void Mypage::pushButtonClicked()
      {
      ui->label -> setNum( ui->label ->value() );
      }

      @
      What i have to correct in the pushButtonClicked() procedure?

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

        Okey okey, Problem SOlved!! Really thanks for that. It was a really headache for me those days :)

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

          You made a simple mistake: use spinBox to take the value from: @ui->label->setNum( ui->spinBox->value() );@

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

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

            Something more, If we suppose that we want everytime to add the new value that spinbox gives to the previous value that the label had. How can this be done?

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

              Depends on what do you mean by "add": integer addition or string append.
              For both you take text from QLabel (take the "old" value), "convert it to integer":http://qt-project.org/doc/qt-4.8/qstring.html#toInt if you need that, do the add and than set the result back to the QLabel (using setText or setNum depending on what addition you used)

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

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

                I only want Integer. For example. When you run the program the label has the value 0. Doing the procedure that you (thank god) solved, it just replaces the "text"(number) at the label.I want to keep the value, and every time OK is pushed, i want it to add the new value to the old one. So, i thought to make an int a; at the pushButtonClicked() procedure but i cannot give it value at the start because every time i click the button it won't keep the last value, but it will keep changing to 0.

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

                  To the int a you give the value you get from ui->label->text() converted to int (see the documentation link from my previous post) than you add the int you get from spinbox and finally set the result to the label.
                  Try to do it and post again if you get into some troubles.

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

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

                    Well, i am trying now the Conversion of text() to int and i am not sure i really understood the way that it is done. I read the Documentation you gave me but i am not sure i understood how to use QString, and especcially what tool to use.

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

                      Just for informational reasons i managed to do that. So if anyone wants to learn how to use the QString::ToInt class that's how it is done :)

                      @void MainWindow::pushButton1Clicked()
                      {
                      int l;

                         l = ui->spinBox1 ->value();
                         QString mystring1(ui->Score1->text());
                         int c = mystring1.toInt();
                         c = l + c;
                      
                         ui->Score1 -> setNum( c );
                         ui->spinBox1 ->setValue(0);
                      
                      }@
                      

                      Zlatomir, Really thanks for your help. Dunno if i could do it without your help. I really appreciate it :)

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

                        Alright, now i need some help in something else, it is again connected to "Signal and Slots" theme. First of all have a look at the Ui. !http://s17.postimage.org/lmu8852xb/image.jpg()! So, my "new" problem is that i want somehow to connect the Max and Min Buttons with the Done button, and after that i need to check with a bool:
                        1st case. Max Clicked ---> Done clicked ---> +200 points in the Label when the Pushbutton is clicked
                        2nd case. Min Clicked ---> Done Clicked ---> +100 points in the Label when the Pushbutton is clicked
                        3rd case. Max Clicked ---> Done NOT Clicked ---> -200 points in the Label when Pushbutton clicked
                        4th case. Min Clicked ---> Done NOT clicked ---> -100 points in the Label when Pushbutton clicked
                        5th case. All buttons Unclicked, No difference to Label when Pushbutton clicked

                        Let's have a look at the code

                        mypage.h
                        @#ifndef MYPAGE_H
                        #define MYPAGE_H

                        #include <QWidget>
                        #include <QPushButton>
                        #include <QLabel>
                        #include <QSpinBox>
                        #include <QString>
                        #include <QAbstractButton>

                        namespace Ui {
                        class Mypage;
                        }

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

                        // I will explain the "int" at the mypage.cpp code
                        private slots:
                        int on_Done_clicked();
                        int on_MinPoints_clicked();
                        int on_MaxPoints_clicked();
                        void pushButtonClicked();

                        private:
                        Ui::Mypage *ui;

                        };

                        #endif // MYPAGE_H@

                        mypage.cpp

                        @#include "mypage.h"
                        #include "ui_mypage.h"
                        #include <QtGui>
                        #include <QPushButton>
                        #include <QLabel>
                        #include <QSpinBox>
                        #include <QString>
                        #include <QAbstractButton>

                        Mypage::Mypage(QWidget *parent) :
                        QWidget(parent),
                        ui(new Ui::Mypage)
                        {
                        ui->setupUi(this);
                        QObject::connect(ui -> MaxPoints, SIGNAL(clicked()), this, SLOT(on_MaxPoints_clicked()));
                        QObject::connect(ui -> MinPoints, SIGNAL(clicked()), this, SLOT(on_MaxPoints_clicked()));
                        QObject::connect(ui -> Done, SIGNAL(clicked()), this, SLOT(on_Done_clicked()));
                        QObject::connect(ui -> pushButton, SIGNAL(clicked()), this, SLOT(on_Done_clicked()));
                        QObject::connect(ui -> pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));
                        }

                        void MainWindow::pushButton1Clicked()
                        {
                        int l,s;

                           l = ui->spinBox1 ->value();
                           QString mystring1(ui->label->text());
                           int c = mystring1.toInt();
                           c = l + c;
                        

                        //Here is a first explanation of my code.
                        //I need this int s; to take a value from the on_Done_Clicked()
                        //that's why i have turned these functions from void to int
                        s = on_Done_clicked();

                           ui->label -> setNum( c + s );
                           ui->spinBox1 ->setValue(0);
                        
                        }
                        

                        //Check that they are now int, not void as they should be
                        int Mypage::on_MaxPoints_clicked()
                        {
                        //So in these functions i need to take a Signal (true or false) from Buttons MaxPoints
                        //and MinPoints. If they are clicked i need to take true, if not i need to take false
                        //and after that I use it in the "if (Status1 == true) e.t.c."
                        int max;
                        bool Status1;
                        Status1 = ui->MaxPoints->clicked();
                        if (Status1 == true)
                        max = 200;
                        else max =0;
                        return max;
                        }

                        int Mypage::on_MinPoints_clicked()
                        {
                        int min;
                        bool Status2;
                        Status2 = ui->MinPoints->clicked();
                        if (Status2 == true)
                        min = 200;
                        else min =0;
                        return min;
                        }

                        int Mypage::on_Done_clicked()
                        {
                        //So here in this Function i want to take the Values from on_MaxPoints_clicked()
                        //and on_MinPoints_clicked() check what is pressed and what not and return the right
                        // "done" in the pushButton1Clicked()
                        int done,Maxvalue,Minvalue;
                        Maxvalue = on_MaxPoints_clicked();
                        Minvalue = on_MinPoints_clicked();
                        if (Maxvalue!=0 && Minvalue==0)
                        done= Maxvalue;
                        else if (Maxvalue==0 && Minvalue!=0)
                        done= Minvalue;
                        else
                        done=0;
                        return done;
                        }
                        //In Compiling i have really many errors. I want you to explain me
                        //first of all if this that i'm trying can be done
                        //and Secondly if i am following the right way to do it and what i am doing wrong
                        @

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

                          First tell us more about the errors, we can't help you if we don't know.

                          Anyway at a first look i spotted a few things:

                          1. You added slots so you most likely need to Run qmake and than Build/Run your project

                          2. You call the on_Done_clicked slot manually in pushButton1Clicked are you sure you want that?

                          3.1) In many places you call the signal clicked from the slot that is actually connected with the same signal.
                          3.2 You try to get a bool return from the signal "call"
                          Possible fix: maybe you actually need "check-able QPushButton":http://qt-project.org/doc/qt-4.8/qabstractbutton.html#checked-prop
                          You do this in many places, example:
                          @int Mypage::on_MaxPoints_clicked()
                          {
                          //... this code is excuted when MaxPoints button has been clicked so there is no point in calling clicked and also clicked is not returning anything
                          Status1 = ui->MaxPoints->clicked();
                          //...
                          }@
                          4) You use the naming convention for auto-connect and you also connect manually, this will result in two calls of the slot per each click on the button, auto-connect name convention is naming the slot like this: on_WIDGETNAME_SIGNALNAME you can read about it "here":http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html#a-dialog-without-auto-connect (i prefer to avoid it and make the connections myself)

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

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

                            Alright, i solved the problem. My thoughts were correct :) The void -->int change is working. The only thing that i changed is the " Status1 = ui->MaxPoints->clicked(); " . I focused on the Check/uncheck buttons and with the ui-> MaxPointsCheck -> isChecked(); the job that i wanted is now done :) Again thanks for the answers :) You helped me a lot :)

                            1 Reply Last reply
                            0
                            • 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

                                          • Login

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