QTextBrowser的setSource崩溃

时间:2011-03-22 06:42:32

标签: html qt crash

我正在用Qt编写一个应用程序,它使用QTextBrowser显示一些本地存储的HTML。在启动时使用它时,它工作正常,但在查询数据库以获取HTML文件的路径后,我得到了奇怪的错误。

以下是代码:

错误的位置是对setSource()的调用。

但请注意,我不认为这只是一个数据库问题,因为即使将完美的HTML文件作为常量参数加载,代码也会失败

void MainWindow::LessonSelection_itemPressedSlot(QListWidgetItem* ITEM)
{
    //selection pressed, run this slot
    qWarning("ITEM PRESSED");
    //display ITEM's name
    qWarning(ITEM->text().toAscii());
    //name of the HTML file
    QString name = ITEM->text();
    //get the HTML Lesson object
    HTML_Lesson lesson = DatabaseHelper::getLessonWithName(name);
    //print the HTML path
    qWarning("HTML PATH:" + lesson.getHTML_Path().toAscii());
    //load the HTML file
    loadHTMLFiles(lesson);
}
void MainWindow::loadHTMLFiles(HTML_Lesson& LESSON)
{
    //ERROR HERE
    HTMLPage->setSource(QUrl(LESSON.getHTML_Path()));
}

失败,

void MainWindow::LessonSelection_itemPressedSlot(QListWidgetItem* ITEM)
{
    //selection pressed, run this slot
        qWarning("ITEM PRESSED");
    //display ITEM's name
    qWarning(ITEM->text().toAscii());
    //name of the HTML file
    QString name = ITEM->text();
    //get the HTML Lesson object
    HTML_Lesson lesson = DatabaseHelper::getLessonWithName(name);

    //load the HTML file
    loadHTMLFiles(lesson);
}
void MainWindow::loadHTMLFiles(HTML_Lesson& LESSON)
{
    //ERROR HERE
    HTMLPage->setSource(“telephone.html);
}

在程序中的任何其他地方调用setSource都不会崩溃。

它不能是对象构造的问题,因为我可以在程序的其他地方向它发送一个HTML文件并让它正常工作。它也不能是数据库,因为它也不能用于绝对路径。即使在除了调用错误产生函数之外的所有代码之后,它也会产生相同的错误。 这是在显示主窗口后无法调用setSource的问题吗?如果是这种情况,我会用什么来在整个程序中显示不同的HTML文件?

请尝试使用您自己的html文件运行代码。

这是调试器输出,如果它有帮助:

sql.html
QFSFileEngine::open: No file name specified
ITEM PRESSED
ANITEM
*** glibc detected *** /home/thomas/codehelp-build-desktop/codehelp: invalid fastbin entry (free): 0x00000000007b1c30 ***
======= Backtrace: =========
/lib/libc.so.6(+0x774b6)[0x7ffff5c474b6]
/lib/libc.so.6(cfree+0x73)[0x7ffff5c4dc83]
/home/thomas/qtsdk-2010.05/qt/lib/libQtCore.so.4(_ZN10QByteArrayaSERKS_+0x32)[0x7ffff699d4b2]
/home/thomas/qtsdk-2010.05/qt/lib/libQtCore.so.4(+0x14a8c2)[0x7ffff6a598c2]
/home/thomas/qtsdk-2010.05/qt/lib/libQtCore.so.4(_ZNK4QUrl7isValidEv+0x35)[0x7ffff6a59b15]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(+0x6dac8a)[0x7ffff74ccc8a]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN12QTextBrowser9setSourceERK4QUrl+0x4f)[0x7ffff74cef3f]
/home/thomas/codehelp-build-desktop/codehelp[0x405261]
/home/thomas/codehelp-build-desktop/codehelp[0x4051a5]
/home/thomas/codehelp-build-desktop/codehelp[0x4067b9]
/home/thomas/qtsdk-2010.05/qt/lib/libQtCore.so.4(_ZN11QMetaObject8activateEP7QObjectPKS_iPPv+0x298)[0x7ffff6ab80f8]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN11QListWidget11itemPressedEP15QListWidgetItem+0x2f)[0x7ffff75ea87f]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN11QListWidget11qt_metacallEN11QMetaObject4CallEiPPv+0x218)[0x7ffff75eeff8]
/home/thomas/qtsdk-2010.05/qt/lib/libQtCore.so.4(_ZN11QMetaObject8activateEP7QObjectPKS_iPPv+0x298)[0x7ffff6ab80f8]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN17QAbstractItemView7pressedERK11QModelIndex+0x22)[0x7ffff757ae32]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN17QAbstractItemView15mousePressEventEP11QMouseEvent+0x4df)[0x7ffff75855af]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN7QWidget5eventEP6QEvent+0x824)[0x7ffff7055e44]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN6QFrame5eventEP6QEvent+0x2b)[0x7ffff745041b]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN19QAbstractScrollArea13viewportEventEP6QEvent+0x17)[0x7ffff74dfb87]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN17QAbstractItemView13viewportEventEP6QEvent+0x195)[0x7ffff7589c35]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(+0x6efb68)[0x7ffff74e1b68]
/home/thomas/qtsdk-2010.05/qt/lib/libQtCore.so.4(_ZN23QCoreApplicationPrivate29sendThroughObjectEventFiltersEP7QObjectP6QEvent+0x7e)[0x7ffff6aa244e]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN19QApplicationPrivate13notify_helperEP7QObjectP6QEvent+0x80)[0x7ffff6ff5d30]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN12QApplication6notifyEP7QObjectP6QEvent+0x172e)[0x7ffff6ffde8e]
/home/thomas/qtsdk-2010.05/qt/lib/libQtCore.so.4(_ZN16QCoreApplication14notifyInternalEP7QObjectP6QEvent+0x84)[0x7ffff6aa20a4]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN19QApplicationPrivate14sendMouseEventEP7QWidgetP11QMouseEventS1_S1_PS1_R8QPointerIS0_Eb+0x177)[0x7ffff6ff8f27]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(+0x28b61d)[0x7ffff707d61d]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(_ZN12QApplication15x11ProcessEventEP7_XEvent+0x1156)[0x7ffff707c866]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(+0x2b5038)[0x7ffff70a7038]
/lib/libglib-2.0.so.0(g_main_context_dispatch+0x1f2)[0x7ffff5521342]
/lib/libglib-2.0.so.0(+0x442a8)[0x7ffff55252a8]
/lib/libglib-2.0.so.0(g_main_context_iteration+0x6c)[0x7ffff552545c]
/home/thomas/qtsdk-2010.05/qt/lib/libQtCore.so.4(_ZN20QEventDispatcherGlib13processEventsE6QFlagsIN10QEventLoop17ProcessEventsFlagEE+0x5c)[0x7ffff6acfbcc]
/home/thomas/qtsdk-2010.05/qt/lib/libQtGui.so.4(+0x2b4bcf)[0x7ffff70a6bcf]
/home/thomas/qtsdk-2010.05/qt/lib/libQtCore.so.4(_ZN10QEventLoop13processEventsE6QFlagsINS_17ProcessEventsFlagEE+0x35)[0x7ffff6aa13f5]
/home/thomas/qtsdk-2010.05/qt/lib/libQtCore.so.4(_ZN10QEventLoop4execE6QFlagsINS_17ProcessEventsFlagEE+0xa6)[0x7ffff6aa1646]
/home/thomas/qtsdk-2010.05/qt/lib/libQtCore.so.4(_ZN16QCoreApplication4execEv+0xb4)[0x7ffff6aa3484]
/home/thomas/codehelp-build-desktop/codehelp[0x403b85]
/lib/libc.so.6(__libc_start_main+0xfe)[0x7ffff5beed8e]
/home/thomas/codehelp-build-desktop/codehelp[0x403879]
======= Memory map: ========
00400000-00409000 r-xp 00000000 08:16 2763162 /home/thomas/codehelp-build-desktop/codehelp
00608000-00609000 r--p 00008000 08:16 2763162 /home/thomas/codehelp-build-desktop/codehelp
00609000-0060a000 rw-p 00009000 08:16 2763162 /home/thomas/codehelp-build-desktop/codehelp
0060a000-013f9000 rw-p 00000000 00:00 0 [heap]
7fffddc5a000-7fffddc5f000 r-xp 00000000 08:16 4066237 /usr/lib/libdatrie.so.1.0.3
7fffddc5f000-7fffdde5e000 ---p 00005000 08:16 4066237 /usr/lib/libdatrie.so.1.0.3
7fffdde5e000-7fffdde5f000 r--p 00004000 08:16 4066237 /usr/lib/libdatrie.so.1.0.3
7fffdde5f000-7fffdde60000 rw-p 00005000 08:16 4066237 /usr/lib/libdatrie.so.1.0.3
7fffdde60000-7fffdde68000 r-xp 00000000 08:16 4066920 /usr/lib/libthai.so.0.1.6
7fffdde68000-7fffde067000 ---p 00008000 08:16 4066920 /usr/lib/libthai.so.0.1.6
7fffde067000-7fffde068000 r--p 00007000 08:16 4066920 /usr/lib/libthai.so.0.1.6
7fffde068000-7fffde069000 rw-p 00008000 08:16 4066920 /usr/lib/libthai.so.0.1.6
7fffde086000-7fffde0d0000 r--p 00000000 08:16 4983728 /usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono-Bold.ttf
7fffde0d0000-7fffde0d9000 r--p 00000000 08:16 4983878 /usr/share/fonts/type1/gsfonts/s050000l.pfb
7fffde0d9000-7fffde45c000 r--p 00000000 08:16 4983771 /usr/share/fonts/truetype/unfonts/UnBatang.ttf
7fffde45c000-7fffde94d000 r--p 00000000 08:16 4983775 /usr/share/fonts/truetype/wqy/wqy-microhei.ttc
7fffde94d000-7fffde96a000 r--p 00000000 08:16 917578 /usr/share/fonts/truetype/msttcorefonts/Webdings.ttf
7fffde96a000-7fffde980000 r--p 00000000 08:16 4983699 /usr/share/fonts/truetype/thai/Sawasdee.ttf
7fffde980000-7fffde9cd000 r--p 00000000 08:16 4983658 /usr/share/fonts/truetype/freefont/FreeMono.ttf
7fffde9cd000-7fffde9f9000 r--p 00000000 08:16 4983670 /usr/share/fonts/truetype/openoffice/opens___.ttf
7fffde9f9000-7fffdea29000 r--p 00000000 08:16 4983739 /usr/share/fonts/truetype/ttf-indic-fonts-core/Pothana2000.ttf
7fffdea29000-7fffdea42000 r--p 00000000 08:16 4983733 /usr/share/fonts/truetype/ttf-indic-fonts-core/Kedage-n.ttf
7fffdea42000-7fffdeb7d000 r--p 00000000 08:16 4983666 /usr/share/fonts/truetype/freefont/FreeSerif.ttf
7fffdeb7d000-7fffdeb9a000 r--p 00000000 08:16 917558 /usr/share/fonts/truetype/msttcorefonts/Arial_Black.ttf
7fffdeb9a000-7fffdebeb000 r--p 00000000 08:16 4983731 /usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif.ttf
7fffdebeb000-7fffdec3a000 r--p 00000000 08:16 4983729 /usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf
7fffdec3a000-7fffdec84000 r--p 00000000 08:16 917565 /usr/share/fonts/truetype/msttcorefonts/Courier_New.ttf
7fffdec84000-7fffdf277000 r--p 00000000 08:16 4983671 /usr/share/fonts/truetype/takao/TakaoPGothic.ttf
7fffdf277000-7fffdf2d5000 r--p 00000000 08:16 4983740 /usr/share/fonts/truetype/ttf-indic-fonts-core/Rachana_04.ttf
7fffdf2d5000-7fffdf2ef000 r--p 00000000 08:16 4983691 /usr/share/fonts/truetype/thai/Norasi.ttf
7fffdf2ef000-7fffdf316000 r--p 00000000 08:16 4983748 /usr/share/fonts/truetype/ttf-indic-fonts-core/utkal.ttf
7fffdf316000-7fffdf32b000 r--p 00000000 08:16 4983741 /usr/share/fonts/truetype/ttf-indic-fonts-core/Rekha.ttf
7fffdf32b000-7fffdf33d000 r--p 00000000 08:16 4983743 /usr/share/fonts/truetype/ttf-indic-fonts-core/gargi.ttf
7fffdf33d000-7fffdf34a000 r--p 00000000 08:16 4983749 /usr/share/fonts/truetype/ttf-kacst-one/KacstOne.ttf
7fffdf34a000-7fffdf3bb000 r--p 00000000 08:16 4983662 /usr/share/fonts/truetype/freefont/FreeSans.ttf
7fffdf3bb000-7fffdf40d000 r--p 00000000 08:16 4983736 /usr/share/fonts/truetype/ttf-indic-fonts-core/Meera_04.ttf
7fffdf40d000-7fffdf421000 r--p 00000000 08:16 4983746 /usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_hi.ttf
7fffdf421000-7fffdf435000 r--p 00000000 08:16 4983745 /usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_gu.ttf
7fffdf435000-7fffdf458000 r--p 00000000 08:16 4983744 /usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_bn.ttf
7fffdf458000-7fffdf674000 r--p 00000000 08:16 4983773 /usr/share/fonts/truetype/unfonts/UnDotum.ttf
7fffdf674000-7fffdf6a2000 r--p 00000000 08:16 4983737 /usr/share/fonts/truetype/ttf-indic-fonts-core/MuktiNarrow.ttf
7fffdf6a2000-7fffdf6e3000 r--p 00000000 08:16 4983750 /usr/share/fonts/truetype/ttf-khmeros-core/KhmerOS.ttf
7fffdf6e3000-7fffdf6f9000 r--p 00000000 08:16 4983725 /usr/share/fonts/truetype/thai/Waree.ttf
7fffdf6f9000-7fffdf70d000 r--p 00000000 08:16 4983818 /usr/share/fonts/type1/gsfonts/n019003l.pfb
7fffdf70d000-7fffdf751000 r--p 00000000 08:16 917592 /usr/share/fonts/truetype/msttcorefonts/Arial.ttf
7fffdf751000-7fffdf774000 r--p 00000000 08:16 925046 /usr/share/fonts/truetype/msttcorefonts/Verdana.ttf
7fffdf774000-7fffdf803000 r--p 00000000 08:16 4983726 /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf
7fffdf803000-7fffdf865000 r--p 00000000 08:16 4983768 /usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-BI.ttf
7fffdf865000-7fffdf8c5000 rw-s 00000000 00:04 14778389 /SYSV00000000 (deleted)
7fffdf8c5000-7fffdf960000 r--p 00000000 08:16 4983727 /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf
7fffdf960000-7fffdf9b7000 r-xp 00000000 08:16 3026932 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqtiff.so
7fffd
f9b7000-7fffdfbb6000 ---p 00057000 08:16 3026932 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqtiff.so
7fffdfbb6000-7fffdfbb9000 rw-p 00056000 08:16 3026932 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqtiff.so
7fffdfbb9000-7fffdfc17000 r-xp 00000000 08:16 3556753 /home/thomas/qtsdk-2010.05/qt/lib/libQtSvg.so.4.7.0
7fffdfc17000-7fffdfe16000 ---p 0005e000 08:16 3556753 /home/thomas/qtsdk-2010.05/qt/lib/libQtSvg.so.4.7.0
7fffdfe16000-7fffdfe19000 rw-p 0005d000 08:16 3556753 /home/thomas/qtsdk-2010.05/qt/lib/libQtSvg.so.4.7.0
7fffdfe19000-7fffdfe1e000 r-xp 00000000 08:16 3026930 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqsvg.so
7fffdfe1e000-7fffe001e000 ---p 00005000 08:16 3026930 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqsvg.so
7fffe001e000-7fffe001f000 rw-p 00005000 08:16 3026930 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqsvg.so
7fffe001f000-7fffe0076000 r-xp 00000000 08:16 3026929 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqmng.so
7fffe0076000-7fffe0276000 ---p 00057000 08:16 3026929 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqmng.so
7fffe0276000-7fffe027a000 rw-p 00057000 08:16 3026929 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqmng.so
7fffe027a000-7fffe02b4000 r-xp 00000000 08:16 3026928 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqjpeg.so
7fffe02b4000-7fffe04b4000 ---p 0003a000 08:16 3026928 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqjpeg.so
7fffe04b4000-7fffe04b5000 rw-p 0003a000 08:16 3026928 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqjpeg.so
7fffe04b5000-7fffe04bc000 r-xp 00000000 08:16 3026927 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqico.so
7fffe04bc000-7fffe06bb000 ---p 00007000 08:16 3026927 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqico.so
7fffe06bb000-7fffe06bc000 rw-p 00006000 08:16 3026927 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqico.so
7fffe06bc000-7fffe06c3000 r-xp 00000000 08:16 3026931 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqgif.so
7fffe06c3000-7fffe08c3000 ---p 00007000 08:16 3026931 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqgif.so
7fffe08c3000-7fffe08c4000 rw-p 00007000 08:16 3026931 /home/thomas/qtsdk-2010.05/qt/plugins/imageformats/libqgif.so
7fffe08c4000-7fffe08c8000 r-xp 00000000 08:16 4333843 /usr/lib/kde4/plugins/imageformats/kimg_xview.so
7fffe08c8000-7fffe0ac7000 ---p 00004000 08:16 4333843 /usr/lib/kde4/plugins/imageformats/kimg_xview.so
7fffe0ac7000-7fffe0ac8000 r--p 00003000 08:16 4333843 /usr/lib/kde4/plugins/imageformats/kimg_xview.so
7fffe0ac8000-7fffe0ac9000 rw-p 00004000 08:16 4333843 /usr/lib/kde4/plugins/imageformats/kimg_xview.so
7fffe0ac9000-7fffe0ad8000 r-xp 00000000 08:16 4333847 /usr/lib/kde4/plugins/imageformats/kimg_xcf.so
7fffe0ad8000-7fffe0cd7000 ---p 0000f000 08:16 4333847 /usr/lib/kde4/plugins/imageformats/kimg_xcf.so
7fffe0cd7000-7fffe0cd8000 r--p 0000e000 08:16 4333847 /usr/lib/kde4/plugins/imageformats/kimg_xcf.so
7fffe0cd8000-7fffe0cd9000 rw-p 0000f000 08:16 4333847 /usr/lib/kde4/plugins/imageformats/kimg_xcf.so
7fffe0cd9000-7fffe0cdd000 rw-p 00000000 00:00 0 
7fffe0cdd000-7fffe0ce2000 r-xp 00000000 08:16 4333825 /usr/lib/kde4/plugins/imageformats/kimg_tga.so
7fffe0ce2000-7fffe0ee1000 ---p 00005000 08:16 4333825 /usr/lib/kde4/plugins/imageformats/kimg_tga.so
7fffe0ee1000-7fffe0ee2000 r--p 00004000 08:16 4333825 /usr/lib/kde4/plugins/imageformats/kimg_tga.so
7fffe0ee2000-7fffe0ee3000 rw-p 00005000 08:16 4333825 /usr/lib/kde4/plugins/imageformats/kimg_tga.so
7fffe0ee3000-7fffe0eec000 r-xp 00000000 08:16 4333822 /usr/lib/kde4/plugins/imageformats/kimg_rgb.so
7fffe0eec000-7fffe10ec000 ---p 00009000 08:16 4333822 /usr/lib/kde4/plugins/imageformats/kimg_rgb.so
7fffe10ec000-7fffe10ed000 r--p 00009000 08:16 4333822 /usr/lib/kde4/plugins/imageformats/kimg_rgb.so
7    fffe10ed000-7fffe10ee000 rw-p 0000a000 08:16 4333822 /usr/lib/kde4/plugins/imageformats/kimg_rgb.so
7fffe10ee000-7fffe10f3000 r-xp 00000000 08:16 4333846 /usr/lib/kde4/plugins/imageformats/kimg_ras.so
7fffe10f3000-7fffe12f2000 ---p 00005000 08:16 4333846

剩下的我不得不切断

1 个答案:

答案 0 :(得分:1)

我建议将问题分成两步。不使用QTextBrowser::setSource,而是使用QFile手动阅读文件内容,然后使用QString将浏览器内容设置为QTextEdit::setHtml。这允许您检查从数据库返回的文件名是否有效,HTML内容是否有效(通过检查从QFile读取的QString)以及QTextBrowser :: setSource使用的QUrl和search path的构造是这不是问题。该方法应该解决问题并允许在解析后再次使用QTextBrowser :: setSource。

堆栈转储似乎意味着试图释放一个迷路指针,因为free是跟踪中的最后一个函数。然而,这可能在Qt来源中很深。上面的方法可能比通过大型代码库进行排序更快地达到解决方案。