PDA

View Full Version : QToolbar: Trying to reset value of QSpinBox in QToolbar via QToolbutton results crash



Janoschka
9th October 2013, 12:36
Hi!

I'm trying to implement a sort of error-counter in a toolbar. For this error-counter i use a QToolbutton with different icons depending on the state (red for error, green for ok). Addionaly I use a QSpinBox for counting the errors.
Until now, everything is fine.

What I want to implement is the resetting of the spinbox-value by clicking on the toolbutton.
With the following code, the program crashes when I hit the button.



MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::Main Window)
{
ui->setupUi(this);
....
ui->mainToolBar->addAction(ui->actionLostReset);
QSpinBox *lostSpinBox = new QSpinBox(ui->mainToolBar);
lostSpinBox.setMaximum(10000);
ui->mainToolBar->addWidget(lostSpinBox);
lostSpinBox->setValue(0); // here it works
...
}
...
void MainWindow::on_actionLostReset_triggered()
{
if (ui->actionLostReset->isChecked())
{
lostSpinBox->setValue(10); // here it crashes
}
}


If I do the same command to a spinbox in a widget everything is fine.
How can this be?
I'm using Qt 4.8.4. and Qwt 6.1 on a WinXP SP3.

Thanx for your help and please excuse my bad english.

myta212
10th October 2013, 08:38
Hi Janoschka,
The problem is your definition : QSpinBox *lostSpinBox
in the constructor mainwindow.

Please edit your code like this :
MainWindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
lostSpinBox = new QSpinBox(ui->mainToolBar);
lostSpinBox->setMaximum(10000);
ui->mainToolBar->addWidget(lostSpinBox);
lostSpinBox->setValue(0); // here it works
}

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

void MainWindow::on_actionLostReset_triggered()
{
if (ui->actionLostReset->isChecked())
{
lostSpinBox->setValue(10); // here it crashes
}
}


MainWindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSpinBox>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

private:
Ui::MainWindow *ui;
QSpinBox *lostSpinBox;

public slots:
void on_actionLostReset_triggered();
};

#endif // MAINWINDOW_H


Best regards,

Toto

Janoschka
10th October 2013, 15:08
Hi Toto!
Thank you very, very much!!!!

I was so dumb and blind, because after your posting I realised I made the correct things in mainwindow.h, but I made some kind of redefinition in mainwindow.c.

Sometimes it's better to have someone to look at the code....

Have a nice day