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. Help with using parameters with private slots?

Help with using parameters with private slots?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 3.9k 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.
  • A Offline
    A Offline
    acedawg45
    wrote on last edited by
    #1

    I have been looking for about 10 min, no answer. So, I figured I would ask you guys.
    Anyway, my problem is that I am making a QTimer (called "timer", plain and simple), and I connected it to one slot I made, timeDone(void). Well, that works fine, because I don't have to pass anything into it in the connect()'s in main().
    BUT. I have a second slot, timeStop(QTimer timer) - I am not very good with naming things - and what that does it stop the timer by passing "timer" into it and stopping it. Well, that won't work. It says it does not exist or something. Anyway, I will post my code in this if it is necessary, but this seems like a quick, easy answer.
    Thanks for anything!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      Please do not duplicate your posts. If you need to you can modify your post.
      I've deleted other copy.

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

        Hi,

        Yes, some code would be useful otherwise it's just crystal ball debugging.

        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
        • A Offline
          A Offline
          acedawg45
          wrote on last edited by
          #4

          Uh, andreyc, I didn't duplicate this. At least I don't think I did. There is only one of these in "my written posts section":http://qt-project.org/forums/search_results/048fcd648782dd4c8f63aab07245decb/

          Anyway, here is my code:
          MainWindow.cpp:
          @#include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QTimer>
          #include <windows.h>
          #include <QSignalMapper>
          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);
          QTimer *time = new QTimer(this);

          }

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

          void MainWindow::on_setButton_clicked()
          {
          int time = ui->timer1->value() * 1000;
          QTimer *timer = new QTimer(this);

          timer->setInterval(time);
          connect(timer, SIGNAL(timeout()), this, SLOT(timeDone()));
          connect(timer, SIGNAL(timeout()), this, SLOT(timeStop(timer)));
          timer->start();
          

          }

          void MainWindow::timeDone(void){
          MessageBox(NULL, L"TIMES UP", L"NOTE:", MB_OK);
          }

          void MainWindow::timeStop(QTimer *timer){
          //Will do this after I fix problem
          }
          @
          MainWindow.h:
          @#ifndef MAINWINDOW_H
          #define MAINWINDOW_H

          #include <QMainWindow>

          namespace Ui {
          class MainWindow;
          }

          class MainWindow : public QMainWindow
          {
          Q_OBJECT

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

          private slots:
          void on_setButton_clicked();
          void timeDone();
          void timeStop(QTimer *timer);
          private:
          Ui::MainWindow *ui;
          };

          #endif // MAINWINDOW_H
          @

          Anyway, that is what I got. Any problems?

          1 Reply Last reply
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #5

            Hi, this line:
            @ connect(timer, SIGNAL(timeout()), this, SLOT(timeStop(timer)));@

            the timeout() signal does not supply the timer argument that the timestop() slot expects, that's why Qt is complaining (i.e. the timer instance doesn't exist).

            1 Reply Last reply
            0
            • A Offline
              A Offline
              acedawg45
              wrote on last edited by
              #6

              Thank you, but is there a way to fix it? It works for the other connect line. (line 27)

              1 Reply Last reply
              0
              • hskoglundH Offline
                hskoglundH Offline
                hskoglund
                wrote on last edited by
                #7

                Sure, figure it some other way of giving the timeStop() function the timer instance it needs, for example, you could store it in your MainWindow class.

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

                  Why not just use QTimer::singleShot ?

                  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
                  • A Offline
                    A Offline
                    acedawg45
                    wrote on last edited by
                    #9

                    bq. you could store it in your MainWindow class.
                    Well, I did, didn't I? Like, I put it in my private slots in MainWindow.h (line 21).
                    Or is that not what you mean?
                    bq. Why not just use QTimer::singleShot ?
                    Eh, I would, but Idk, I just feel like using a regular timer. I don't know why...

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      acedawg45
                      wrote on last edited by
                      #10

                      Oh, I figured it out! I did change it to singleShot, and I also made the QTimer a member of the MainWindow class, that way i didn't have to pass it as a parameter, but just set the timer in the function. Anyway, thanks for all the advice guys!

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

                        singleShot is a static function, so there's no need for your QTimer member variable

                        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