I'm building a timer with an interval that can adjusted by a slider. Here's a shot of my ui:

yooeye.jpg

The upper lcdNumber counts up by one every second. I want to use the slider to speed it up and slow it down. Here is my header file

Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5.  
  6. namespace Ui {
  7. class MainWindow;
  8. }
  9.  
  10. class MainWindow : public QMainWindow
  11. {
  12. Q_OBJECT
  13.  
  14. public:
  15. explicit MainWindow(QWidget *parent = 0);
  16. ~MainWindow();
  17. QTimer *timer; // here the timer is made public
  18.  
  19. private:
  20. Ui::MainWindow *ui;
  21.  
  22. private slots:
  23. void inkup();
  24. void on_horizontalSlider_valueChanged(int value);
  25. };
  26.  
  27. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 

And here is my application

Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "qapplication.h"
  4. #include <QtWidgets>
  5.  
  6. MainWindow::MainWindow(QWidget *parent) :
  7. QMainWindow(parent),
  8. ui(new Ui::MainWindow)
  9. {
  10. ui->setupUi(this);
  11.  
  12. QTimer *timer = new QTimer(this);
  13. connect(timer, SIGNAL(timeout()), this, SLOT(inkup()));
  14. timer->start(1000);
  15. }
  16.  
  17. void MainWindow::inkup() {
  18. int eks = ui->lcdNumber->value();
  19. eks += 1;
  20. ui->lcdNumber->display(eks);
  21. }
  22.  
  23. MainWindow::~MainWindow()
  24. {
  25. delete ui;
  26. }
  27.  
  28. void MainWindow::on_horizontalSlider_valueChanged(int speed)
  29. {
  30. ui->lcdNumber_2->display(timer->remainingTime());
  31.  
  32. ui->lcdNumber_2->display(speed);
  33.  
  34. timer->setInterval(speed); // this causes a crash at run time
  35. }
To copy to clipboard, switch view to plain text mode 

In the horizontal slider event handler, I can display in the second lcdNumber the timer property remainingTime(). No problem. I can display the changed slider value so I know it's okay. But when I try and set the timer interval I get a run time crash. In fact most of the properties that pop up for the timer when I type timer-> also cause a crash. What am I doing wrong?