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:

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

    dialog.h:
    @
    #include <QDialog>
    class Dialog : public QDialog
    {
    Q_OBJECT
    public:
    Dialog(QWidget *parent = 0);
    private slots:
    void execute();
    };
    @

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

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.