[Interest] How can I subclass a QDockWidget?

Rollastre Prostrit rollastre at gmail.com
Mon Jul 30 18:03:15 CEST 2012

Hello list.

I'm in kubuntu 12.04 with Qt 4.8.1.

I have a class called View which is a subclass of QDockWidget. In its 
constructor I do this

View::View(QWidget* parent = NULL)
     : QDockWidget(MainWindow::Instance())
     setFeatures(QDockWidget::DockWidgetClosable | 

if I use the following method to add a view to the main window

void QScaffy::addView(View *view)

     qDebug() << "adding view " << view->getViewTitle();
     w->addDockWidget(Qt::LeftDockWidgetArea, view, Qt::Horizontal); // 
where w is a MainWindow (subclass of QMainWindow)

my question is: what am I doing wrong to get a 'segmentation fault' in 

int QDockWidgetLayout::minimumTitleWidth() const
     QDockWidget *q = qobject_cast<QDockWidget*>(parentWidget()); 
<---------------- this returns a QWidget and upon the cast q points to 0x0

     if (QWidget *title = widgetForRole(TitleBar))
         return pick(verticalTitleBar, title->minimumSizeHint());

     QSize closeSize(0, 0);
     QSize floatSize(0, 0);
     if (hasFeature(q, QDockWidget::DockWidgetClosable)) { 
<------------------------------------- Crash HERE
         if (QLayoutItem *item = item_list[CloseButton])
             closeSize = item->widget()->sizeHint();
     if (hasFeature(q, QDockWidget::DockWidgetFloatable)) {
         if (QLayoutItem *item = item_list[FloatButton])
             floatSize = item->widget()->sizeHint();

     int titleHeight = this->titleHeight();

     int mw = q->style()->pixelMetric(QStyle::PM_DockWidgetTitleMargin, 
0, q);
     int fw = q->style()->pixelMetric(QStyle::PM_DockWidgetFrameWidth, 
0, q);

     return pick(verticalTitleBar, closeSize)
             + pick(verticalTitleBar, floatSize)
             + titleHeight + 2*fw + 3*mw;

However, if my View is just a plain Widget and I manually create a 
QDockWidget like this it works

void QScaffy::addView(QWidget *view)
     QDockWidget *dock = new QDockWidget(tr("New"), w);
     dock->setFeatures(QDockWidget::DockWidgetClosable |

     w->addDockWidget(Qt::LeftDockWidgetArea, dock, Qt::Horizontal);


Do you have any clue what is going on here?

Thanks in advance.

More information about the Interest mailing list