PDA

View Full Version : QStyledItemDelegate crash



folibis
31st October 2013, 03:00
I have a simple list to show list of plugins in my program. To customize it i use delegate class derived from QStyledItemDelegate.
The code:

class PluginListDelegate : public QStyledItemDelegate
{
public:
PluginListDelegate();
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index ) const;
private:
QFont line1;
};


PluginListDelegate::PluginListDelegate() :
line1("Tahoma",10,QFont::Bold,false)
{

}
void PluginListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter,option,index);
...
...
}

QSize PluginListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
int width = 0,height = 0;
QFontMetrics fm1(line1); // <-- crash here
height = fm1.height() + 20;
width = fm1.width(name) + 20;
return QSize(width,height);
}

Really don't understand what I do wrong.

ChrisW67
31st October 2013, 03:41
What is the actual backtrace?

folibis
31st October 2013, 03:58
SIGSEGV
Segmentation fault

it stoped at line 573 in qvariant.h


static T metaType(const QVariant &v)
{
const int vid = qMetaTypeId<T>();
if (vid == v.userType())
return *reinterpret_cast<const T *>(v.constData()); // <-- stopped here
if (vid < int(QMetaType::User)) {
T t;
if (v.convert(vid, &t))
return t;
}
return T();
}

I use Windows XP, Qt 5.1.0

Hmm ... I found now that it crashed only when I debug the program with F5.
If i run the program with Ctrl-R it works normally.

ChrisW67
31st October 2013, 04:03
Yes, all very well, but what is the actual backtrace? That will you how the program arrived there (which doesn't look related to your first location).

folibis
31st October 2013, 05:16
Where can I see this backtrace?

ChrisW67
31st October 2013, 06:53
When you run a debug build of your program in your debugger until it crashes. Then you can generally see the backtrace in your IDE. In Qt Creator the panel is called "Stack" and might contain something like this:


0 MainWindow::crashNow mainwindow.cpp 41 0x4030f3
1 MainWindow::doMoreStuff mainwindow.cpp 36 0x4030d8
2 MainWindow::doStuff mainwindow.cpp 31 0x4030be
3 MainWindow::qt_static_metacall moc_mainwindow.cpp 52 0x40370e
4 QMetaObject::activate(QObject*, QMetaObject const*, int, void**) /usr/lib64/qt4/libQtCore.so.4 0x7ffff6de1c01
5 ?? /usr/lib64/qt4/libQtCore.so.4 0x7ffff6deb3df
6 QObject::event(QEvent*) /usr/lib64/qt4/libQtCore.so.4 0x7ffff6de6ba9
7 QApplicationPrivate::notify_helper(QObject*, QEvent*) /usr/lib64/qt4/libQtGui.so.4 0x7ffff7306c04
8 QApplication::notify(QObject*, QEvent*) /usr/lib64/qt4/libQtGui.so.4 0x7ffff730b9d3
9 QCoreApplication::notifyInternal(QObject*, QEvent*) /usr/lib64/qt4/libQtCore.so.4 0x7ffff6dcdd9c
10 ?? /usr/lib64/qt4/libQtCore.so.4 0x7ffff6dfedd2
11 ?? /usr/lib64/qt4/libQtCore.so.4 0x7ffff6dfc70d
12 ?? /usr/lib64/qt4/libQtCore.so.4 0x7ffff6dfc731
13 g_main_context_dispatch /usr/lib64/libglib-2.0.so.0 0x7ffff65683f2
14 ?? /usr/lib64/libglib-2.0.so.0 0x7ffff6568738
15 g_main_context_iteration /usr/lib64/libglib-2.0.so.0 0x7ffff65687f4
16 QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) /usr/lib64/qt4/libQtCore.so.4 0x7ffff6dfcebf
17 ?? /usr/lib64/qt4/libQtGui.so.4 0x7ffff73a96fe
18 QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) /usr/lib64/qt4/libQtCore.so.4 0x7ffff6dcc8d2
19 QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) /usr/lib64/qt4/libQtCore.so.4 0x7ffff6dccb27
20 QCoreApplication::exec() /usr/lib64/qt4/libQtCore.so.4 0x7ffff6dd1945
... <More>

If you are using a different IDE or different debugger then the output might look different.

The top line is the point of failure (in my example a divide-by-zero), The next line down is where it was called from, and so on. Keep going until you find the first line of your code...

folibis
31st October 2013, 08:03
Thank you, ChrisW67
I will try it.
It is strange but now I cant get to crash. It happened periodically before but now it runs without error both in debug and run modes ))