Results 1 to 3 of 3

Thread: QToolbar: Trying to reset value of QSpinBox in QToolbar via QToolbutton results crash

  1. #1
    Join Date
    Oct 2013
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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.
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::Main Window)
    2. {
    3. ui->setupUi(this);
    4. ....
    5. ui->mainToolBar->addAction(ui->actionLostReset);
    6. QSpinBox *lostSpinBox = new QSpinBox(ui->mainToolBar);
    7. lostSpinBox.setMaximum(10000);
    8. ui->mainToolBar->addWidget(lostSpinBox);
    9. lostSpinBox->setValue(0); // here it works
    10. ...
    11. }
    12. ...
    13. void MainWindow::on_actionLostReset_triggered()
    14. {
    15. if (ui->actionLostReset->isChecked())
    16. {
    17. lostSpinBox->setValue(10); // here it crashes
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by Janoschka; 9th October 2013 at 13:41.

  2. #2
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. lostSpinBox = new QSpinBox(ui->mainToolBar);
    10. lostSpinBox->setMaximum(10000);
    11. ui->mainToolBar->addWidget(lostSpinBox);
    12. lostSpinBox->setValue(0); // here it works
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void MainWindow::on_actionLostReset_triggered()
    21. {
    22. if (ui->actionLostReset->isChecked())
    23. {
    24. lostSpinBox->setValue(10); // here it crashes
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

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

    Best regards,

    Toto

  3. #3
    Join Date
    Oct 2013
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Red face 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

Similar Threads

  1. Unable to add QToolButton to QToolbar on QWidget
    By rawfool in forum Qt Programming
    Replies: 0
    Last Post: 17th August 2012, 11:52
  2. Hiding a QToolButton in a QToolBar
    By elcuco in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2009, 21:35
  3. drag and drop QToolButton in QToolBar
    By NBilal in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 21:11
  4. QToolBar
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2007, 20:48
  5. QToolBar help
    By Erlendhg in forum Qt Programming
    Replies: 2
    Last Post: 6th November 2006, 16:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.