Search code examples
c++qtsizesegmentation-faultqwidget

Calling width(), height(), size() or rect() inside subclass of QWidget ends with segfault


I have a problem with QWidget's width(), height(), size() or rect() function; when it is called it gets a segfault. It is Qt 4.7.

Here is the header of the problematic class:

class PlotCanvas : public QWidget
{ 
    void paintEvent(QPaintEvent * e);
    uint64_t smallestDiv();
    uint64_t longestLength();
    void drawGrid(QPainter * painter);
    QVector<Plot*> plots;
    int calculateHeight() const;
    int calculateWidth() const;
    uint32_t cursorPosition;
public:
    PlotCanvas(QWidget * parent = 0) : QWidget(parent) 
    {
        setStyleSheet("background-color: black");
        setAutoFillBackground(true);
        setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
        setFocusPolicy(Qt::StrongFocus);
        update();
    }
    void addPlot(Plot * plot);
    int getDivCount();
};

and actual segfaulting code:

int PlotCanvas::getDivCount()
{
   QRect bounds = rect(); //segfaults
   qDebug() << bounds.size().width();
   qDebug() << bounds.size().height();
   return 10;
}

traced into Qt's qrect.h:

inline int QRect::width() const
{ return  x2 - x1 + 1; } //this line finally kills it

Which seems like there is something not initialized properly inside QWidget, but I have no clue what, because as you can see I am not really changing initialization of QWidget that much.

PlotCanvas widget resides in subclass of QAbstractScrollArea (which is not implemented yet, so it is just plain QAbstractScrollArea:

class PlotCanvasScrollArea : public QAbstractScrollArea
{
public:
    PlotCanvasScrollArea(QWidget * parent = 0) : QAbstractScrollArea(parent) 
    {

    }
};

I just need to determine size of QWidget painting area, because I am plotting in transition diagram and I need to know how many time divisions will fit into current widget size, because obviously application window is resizable.

EDIT:

Code where PlotCanvas is constructed:

Board(QString & name) : name(name)
{
    hbox = new QHBoxLayout();
    commonKnobGroup = new QGroupBox("Common settings:");
    commonKnobGroup->setMinimumWidth(190);
    commonKnobGroup->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
    commonKnobs = new CommonKnobs(commonKnobGroup);
    hsplitter = new QSplitter();
    plotGroupBox = new QGroupBox("Measurement plots:", hsplitter);
    settingsGroupBox = new QGroupBox("Plot settings:", hsplitter);
    plotSplitter = new QSplitter();
    plotHbox = new QHBoxLayout();
    plotHbox->addWidget(plotSplitter);
    plotGroupBox->setLayout(plotHbox);
    settingsVLayout = new QVBoxLayout();
    plotNamesColumn = new QListWidget(plotSplitter);
    scrollArea = new PlotCanvasScrollArea(plotSplitter);
    //scrollArea->setWidgetResizable(true);
    plotsCanvas = new PlotCanvas();
    scrollArea->setViewport(plotsCanvas);
    scrollArea->setBackgroundRole(QPalette::Dark);
    specializedKnobGroup = new QGroupBox("Probe settings:");
    plotSettings = new QStackedWidget(specializedKnobGroup);
    settingsVLayout->addWidget(commonKnobGroup);
    settingsVLayout->addWidget(specializedKnobGroup);
    settingsGroupBox->setLayout(settingsVLayout);
    hbox->addWidget(hsplitter);
    setLayout(hbox);
    connect(plotNamesColumn, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), this,
            SLOT(changePageNames(QListWidgetItem*, QListWidgetItem*)));
    connect(commonKnobs, SIGNAL(scaleUpdated()), plotsCanvas, SLOT(update()));

};

EDIT2:
Complete stacktrace:

#0  0x000000010000b9ac in QWidget::rect() const at /Volumes/Data/shinji/QtSDK/Desktop/Qt/474/gcc/include/QtGui/qwidget.h:1007
#1  0x000000010000b454 in PlotCanvas::getDivCount() ()
#2  0x000000010000ab39 in Board::getDivCount() ()
#3  0x0000000100009fbc in CommonKnobs::updateScaleByBeginEnd() ()
#4  0x00000001000015b4 in CommonKnobs::qt_metacall(QMetaObject::Call, int, void**) ()
#5  0x0000000100d09b66 in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) ()
#6  0x000000010070df9d in QSpinBox::valueChanged(int) ()
#7  0x00000001004d9684 in QSpinBoxPrivate::emitSignals(EmitPolicy, QVariant const&) ()
#8  0x000000010043b321 in QAbstractSpinBoxPrivate::setValue(QVariant const&, EmitPolicy, bool) ()
#9  0x000000010043c11a in QAbstractSpinBoxPrivate::_q_editorTextChanged(QString const&) ()
#10 0x000000010043cca7 in QAbstractSpinBox::qt_metacall(QMetaObject::Call, int, void**) ()
#11 0x000000010070e435 in QSpinBox::qt_metacall(QMetaObject::Call, int, void**) ()
#12 0x0000000100d09b66 in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) ()
#13 0x000000010048a1e6 in QLineEdit::textChanged(QString const&) ()
#14 0x000000010048d23a in QLineEdit::qt_metacall(QMetaObject::Call, int, void**) ()
#15 0x0000000100d09b66 in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) ()
#16 0x000000010070c9f9 in QLineControl::textChanged(QString const&) ()
#17 0x0000000100494337 in QLineControl::finishChange(int, bool, bool) ()
#18 0x0000000100496664 in QLineControl::processKeyEvent(QKeyEvent*) ()
#19 0x000000010048b8eb in QLineEdit::keyPressEvent(QKeyEvent*) ()
#20 0x00000001001056bd in QWidget::event(QEvent*) ()
#21 0x000000010048c18b in QLineEdit::event(QEvent*) ()
#22 0x000000010043d9f0 in QAbstractSpinBox::keyPressEvent(QKeyEvent*) ()
#23 0x00000001001056bd in QWidget::event(QEvent*) ()
#24 0x000000010043bb1b in QAbstractSpinBox::event(QEvent*) ()
#25 0x00000001004d7be5 in QSpinBox::event(QEvent*) ()
#26 0x00000001000a9e8d in QApplicationPrivate::notify_helper(QObject*, QEvent*) ()
#27 0x00000001000b198b in QApplication::notify(QObject*, QEvent*) ()
#28 0x0000000100d0321c in QCoreApplication::notifyInternal(QObject*, QEvent*) ()
#29 0x00000001000a9f2c in qt_sendSpontaneousEvent(QObject*, QEvent*) ()
#30 0x0000000100125cc1 in QKeyMapper::sendKeyEvent(QWidget*, bool, QEvent::Type, int, QFlags<Qt::KeyboardModifier>, QString const&, bool, int, unsigned int, unsigned int, unsigned int, bool*) ()
#31 0x0000000100126b43 in QKeyMapperPrivate::translateKeyEvent(QWidget*, OpaqueEventHandlerCallRef*, OpaqueEventRef*, void*, bool) ()
#32 0x000000010006072e in qt_dispatchKeyEvent(void*, QWidget*) ()
#33 0x000000010005478b in -[QCocoaView keyDown:] ()
#34 0x00007fff815a90c7 in -[NSWindow sendEvent:] ()
#35 0x0000000100059891 in -[QCocoaWindow sendEvent:] ()
#36 0x00007fff814ddafa in -[NSApplication sendEvent:] ()
#37 0x000000010005cf0a in -[QNSApplication sendEvent:] ()
#38 0x00007fff814746de in -[NSApplication run] ()
#39 0x0000000100066c04 in QEventDispatcherMac::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) ()
#40 0x0000000100def774 in QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) ()
#41 0x0000000100defa94 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) ()
#42 0x0000000100df10bc in QCoreApplication::exec() ()
#43 0x0000000100001cf6 in main ()

Solution

  • I have it sorted out, it was using parent() as pointer to Board (casted with scatic_cast, i thought that cast to wrong object with static_cast would kill it in that point, but no) from PlotCanvas and another class I am using, when actually parent() refereced to PlotCanvasScrollArea in case of PlotCanvas and QGroupBox in case of the other class.
    What is strange that debugger shown that I actually jumped into Board's code even thought address of called object was plain wrong and what is even more strange that it crashed in PlotCanvas code not in origin (Board's code).
    But now it's ok. Thank you guys :)