Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Child QWidget QSettings crash



  • SIGSEGV crash at qobject.h line 324:
    type, types, &SignalType::Object::staticMetaObject);

    //main.cpp
    int main(int argc, char *argv[])
    {  
        QApplication a(argc, argv);
        a.setOrganizationName("HTY");
        a.setApplicationName("HTYDock");
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    //mainwindow.cpp
    QMainWindow
    {
        DatetimeWidget *datetimeWidget = new DatetimeWidget;
    }
    
    //datetimewidget.h
    {
    private:
        enum {
            TEXT_CLOCK,
            DIGITAL_CLOCK,
            ANALOG_CLOCK
        } clock_type;
        QSettings settings;    // add this line cause crash
    }
    
    //datetimewidget.cpp
    DatetimeWidget::DatetimeWidget(QWidget *parent) : QWidget(parent)
       ,settings(QCoreApplication::organizationName(), QCoreApplication::applicationName())
    {
    
    }
    

  • Lifetime Qt Champion

    Hi,

    Based on your incomplete header file, it seems that you are creating a static QSettings object. Don't do that.

    In any case, there's no need to have a class member for QSettings. You should create the object when needed.



  • @SGaist It seems child QWidget can not use static QSettings.

    DatetimeWidget::DatetimeWidget(QWidget *parent) : QWidget(parent)
    {
        QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
    }
    


  • @sonichy said in Child QWidget QSettings crash:

    It seems child QWidget can not use static QSettings.

    What do you mean? Your code shows getting a QSettings object for your application, no more than that. It's not even static. The fact that you have put the code you show inside a widget is neither here nor there. If something goes wrong with what you have written, you have to say so/what....


Log in to reply