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
{
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QPushButton *quit = new QPushButton(tr("Quit"));
move(1600,0);
quit->setFont(QFont("Times", 18, QFont::Bold));
QPushButton* numbase;
numbase = new QPushButton[4];
numbase[0].setText("Dec");
(numbase+1)->setText("Bin"); // Hihihi
numbase[2].setText("Hex");
numbase[3].setText("Oct");
// a[i] == *(a+i)
QLCDNumber *lcd = new QLCDNumber(8);
lcd->setSegmentStyle(QLCDNumber::Filled);
lcd->setMode(QLCDNumber::Hex);
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 99);
slider->setValue(0);
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;
layout->addWidget(quit);
layout->addWidget(lcd);
layout->addWidget(slider);
// Segmentation fault if I include those two lines:
for(int i=0;i<4;i++)
layout->addWidget(numbase+i);
// -------------
setLayout(layout);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
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?