Nominate our 2022 Qt Champions!

Segmentation fault in a call of a window with the message at closing of the editor of the delegate

  • Hi! I can't fix segmentation fault in a call of a window with the message at closing of the editor of the delegate. This is a code:

    #include <QtGui/QApplication>
    #include "dialog.h"
    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    Dialog w;;
    return a.exec();

    #include <QDialog>
    class Dialog : public QDialog
    Dialog(QWidget *parent = 0);
    private slots:
    void execute();

    #include <QDialog>
    #include <QErrorMessage>
    #include <QVBoxLayout>
    #include <QTableWidget>
    #include <QItemDelegate>
    #include "dialog.h"
    Dialog::Dialog(QWidget parent): QDialog(parent) {
    fieldsTable = new QTableWidget(2, 1);
    QVBoxLayout* layout = new QVBoxLayout();
    QItemDelegate* itemDelegate = new QItemDelegate();
    connect(itemDelegate, SIGNAL(closeEditor(QWidget*)), this, SLOT(execute()));
    fieldsTable->setItemDelegateForColumn(0, itemDelegate);
    void Dialog::execute() {
    if (true) // The testing function will be here
    QErrorMessage msgBox;
    msgBox.showMessage(QObject::trUtf8("A error text"));

    For some reason after end of editing of cell QTableWidget the window with an error message opens twice, and then the program falls out by mistake of segmentation. If instead of a call of window QErrorMessage to substitute easier message qDebug () <<"the Error!", all works normally. This problem exists only in Linux.
    What is wrong?

  • @
    connect(itemDelegate, SIGNAL(closeEditor(QWidget*, ???????)), this, SLOT(execute()));

    ??????? = QAbstractItemDelegate::EndEditHint

    I think, "hint" is a required parameter.

  • It depends on the method you use to get out of the editing cell. Using tab, for example, you can close the editor of the current cell and open the next cell. The close of the first cell triggers your slot, the message box takes way focus and thus triggers the close of the second cell's editor, hence your slot is called twice. Using qDebug() does not trigger this, as there is no popup stealing the focus.

    From a quick look at the docs, I doubt that it is ok to use two QErrorMessage dialogs simultaneously (what you probably do). I would add a member pointing to a pointer to a QErrorMessage dialog in the header, create that in the constructor and reuse that instead of creating a new msgBox every time.

    If that doesn't work, build your app in debug mode and run it through a debugger to get a useful stack trace.

Log in to reply