Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Help with using parameters with private slots?



  • 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!



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


  • Lifetime Qt Champion

    Hi,

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



  • 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?



  • 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).



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



  • 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.


  • Lifetime Qt Champion

    Why not just use QTimer::singleShot ?



  • 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...



  • 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!


  • Lifetime Qt Champion

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


Log in to reply