QToolbar: Trying to reset value of QSpinBox in QToolbar via QToolbutton results crash
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.
Code:
{
ui->setupUi(this);
....
ui->mainToolBar->addAction(ui->actionLostReset);
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.
Re: QToolbar: Trying to reset value of QSpinBox in QToolbar via QToolbutton results c
Hi Janoschka,
The problem is your definition : QSpinBox *lostSpinBox
in the constructor mainwindow.
Please edit your code like this :
MainWindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *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
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSpinBox>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
public slots:
void on_actionLostReset_triggered();
};
#endif // MAINWINDOW_H
Best regards,
Toto
Re: QToolbar: Trying to reset value of QSpinBox in QToolbar via QToolbutton results c
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