Take the 2-minute tour ×

I'm a little new to object oriented programming, and very new to Qt and GUIs in general. I am now playing with this example in Nokia's Qt tutorial: http://doc.qt.nokia.com/4.1/tutorial-t5.html

I tried to extend the code; this is what I have now:

#include <QApplication>
#include <QFont>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
#include <QWidget>

class MyWidget : public QWidget
    MyWidget(QWidget *parent = 0);

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    QPushButton *quit = new QPushButton(tr("Quit"));
    quit->setFont(QFont("Times", 18, QFont::Bold));

    QPushButton* numbase;
    numbase = new QPushButton[4];
    (numbase+1)->setText("Bin"); // Hihihi

    // a[i] == *(a+i)

    QLCDNumber *lcd = new QLCDNumber(8);

    slider->setRange(0, 99);

    connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    connect(slider, SIGNAL(valueChanged(int)),
            lcd, SLOT(display(int)));

    connect(numbase+0, SIGNAL(clicked()), lcd, SLOT(setDecMode()));
    connect(numbase+1, SIGNAL(clicked()), lcd, SLOT(setBinMode()));
    connect(numbase+2, SIGNAL(clicked()), lcd, SLOT(setHexMode()));
    connect(numbase+3, SIGNAL(clicked()), lcd, SLOT(setOctMode()));

    QVBoxLayout *layout = new QVBoxLayout;

    // Segmentation fault if I include those two lines:


int main(int argc, char *argv[])
    QApplication app(argc, argv);
    MyWidget widget;
    return app.exec();

When I include the marked two lines, the program does its job correctly, but if I quit it (and only then) the console shows a "Segmentation fault". I'd like to know why that happens.

And also, is there a better way to reference the 4 widgets? (numbase+2) looks weird, is this really how I am supposed to do this?

share|improve this question
You might want to consider changing your picture, someone's already flagged this post as offensive, and I can't see anything offensive in the post so I assume it's the picture. –  ho1 Oct 17 '10 at 13:07
I find the picture quite nice, and can't imagine that a beautiful naked woman could actually offend anyone. But the majority of stackers won't agree with me, unfortunately. –  Pavel Shved Oct 17 '10 at 13:13
Wait, apparently I had this gravatar thing before. I did not use it on a coding site, though :) I changed the picture. –  Alexx Hardt Oct 17 '10 at 13:19
what was the pic? I missed it. –  Will Oct 18 '10 at 11:45
@Will: just a naked lady (well, the top half of her). I already deleted it, so I can't show you anymore. –  Alexx Hardt Oct 18 '10 at 12:33
add comment

3 Answers

up vote 0 down vote accepted

A better way is to use a array of pointers, you should try it:

QPushButton *buttons[4];

buttons[0] = new QPushButton(this);
buttons[1] = new QPushButton(this);
buttons[2] = new QPushButton(this);
buttons[3] = new QPushButton(this);


connect(buttons[0], SIGNAL(clicked()), this, SLOT(bar()));
share|improve this answer
When will I finally grasp the pointers? :) Thanks. Array of pointers != pointer to array. I'll note that. –  Alexx Hardt Oct 17 '10 at 12:54
@downvoter: Can i know why did you downvote? –  Matias Valdenegro Oct 17 '10 at 13:02
does the crash disappear if you replace your code with one given by Matias ? ... –  Ankur Gupta Oct 17 '10 at 13:04
Why is this -1? This is actually correct I was about to say the same. You need pointers in the array to use QPushButton as QWidget. –  stribika Oct 17 '10 at 13:08
@Ankur: Yes. I'd like to know why exactly the segfault appears, though. –  Alexx Hardt Oct 17 '10 at 13:11
show 1 more comment

The possible reason of crash is that Qt, at deletion of the form, calls delete function for each widget on the form. So, at program exit, when destroying your form, Qt internals would execute the following sequence:

delete numbase[0];
delete numbase[1];
delete numbase[2];
delete numbase[3];

However, the correct way to free the memory would be

delete [] numbase;

because you allocated numbase with a special array allocation new[] operator, which is different from the usual new. And it's "undefined behavior" if you use delete instead of delete[] in C++. So, segmentation fault could happen if it's the case.

You can check if it's the reason, by allocating each emenent in your array separately, with new QPushButton;, as specified in the other answer.

share|improve this answer
Thanks! Where would I enter the delete[] line? –  Alexx Hardt Oct 17 '10 at 12:53
@Alexx, it's not you who enters the delete[] line. It's internal Qt procedures which actually call delete operators (see my other answer stackoverflow.com/questions/3646300/… on this topic), and you can't change them. The best you could do is to allocate your objects in such a way that matches Qt's way of deallocating them. That means, not on stack, and not with new[] you used, but with causal new operator instead. –  Pavel Shved Oct 17 '10 at 12:56
I didn't know I walked on forbidden land :) I'll use pointer arrays from now on, thanks! –  Alexx Hardt Oct 17 '10 at 13:00
add comment
  1. Try replacing 4 by 3 in the loop.

  2. If you want Qt to do garbage collection make sure you set the parent child relationship of widget correctly i.e. QPushButton resides in QWidget so set it in constructor.

  3. For simple UI like these try creating it using Qt Designer and see the generated code.

share|improve this answer
I thought addWidget() takes care of the parent/child thing automatically. The other part of the code is from the Qt website, they didn't use it either. –  Alexx Hardt Oct 17 '10 at 12:58
I am just letting you know about the best practices. Explicit is better than implicit. It's easy to read the code and see who is the parent of whom. –  Ankur Gupta Oct 17 '10 at 13:06
add comment

Your Answer


