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

Crashing with GUI?



  • I want to design a trivial counter GUI. Once the user presses the counter button, the counter starts counting.

    @void Window::BCounterClicked() const
    {
    static int COUNTER(0);
    do {
    QString count_str = QString::number(COUNTER);
    label->setText(count_str);
    ++COUNTER;
    }while(true);
    } @

    Once I press the button, the app crashes. Why this occurs?
    Second question, I want to use pause system. Which method should I use?



  • with while(true) you are entering an infinite loop; that's definitely not something you want to happen in a click-signal; i.e. you'll never leave this loop


  • Qt Champions 2017

    label is pointer variable. Hope you have initialized it. If you have not initialized it will crash. Also you are constructing too many string variable in infinitely. This could the reason for crash.

    Regarding the puase, I suggest you make this function as slot. Start the timer at some interval and connect to this slot. Slot should be increment counter ans set label. In this case you don't to have to worry about the infinite loop and pause etc.

    Also you have defined the method as const. It means that is ready only method. It is not suppose to change the value of any member variable.

    Alternatively you can use the QLCD class itself which gives the counter functionality.

    My suggestion is that you can make the second question as separate question. You may get a other answer as ell.



  • @JazzKatua, going in an infinite loop doesn't justify the crashing problem.

    @dheerendra, Yes I did initialized the label. Also, this function is a private slot function. Would you please elaborate about "Slot should be increment counter and set label". What do you mean exactly? With const function, it works if there is no do-while loop. It means that there is no problem with setting it as const.


  • Qt Champions 2017

    Here is the code snippet

    @QTimer timer;
    timer.setIntervaal(10Sec) - (Imean to say give appropriate interval)
    timer.start()
    connect(&timer,SIGNAL(timeout()),this,SLOT(counterClicked()) )@

    @void Window::counterClicked() const
    {
    static int COUNTER(0);
    QString count_str = QString::number(COUNTER);
    label->setText(count_str);
    ++COUNTER;
    }
    @
    Change the timer interval accordingly. This will give the same functionality what you are asking. Hope this solves your problem.



  • @Dheerendra, thanks for being helpful


  • Qt Champions 2017

    It is my pleasure. If solution has helped you, can put your request into solved state ?


Log in to reply