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

QStandardItemModel setItem crash..



  • Mac Os X 10.15 - Qt 5.12.3 Clang64 C++ - MySql Database on local network Nas

    Hi to all,
    i'm working with QStandardItemModel and when i try to insert data in a column my app crash..

    I've declared it in .h file

    #include <QDialog>
    #include <QSqlTableModel>
    #include <QMimeData>
    #include <QTableView>
    #include <QStandardItemModel>
    #include <QSqlDatabase>
    #include <QSqlQuery>
    #include <QSqlTableModel>
    #include <QSqlQueryModel>
    
    
    class DragSqlTableModel : public QSqlQueryModel{
        Q_DISABLE_COPY(DragSqlTableModel)
    
    public:
        using  QSqlQueryModel::QSqlQueryModel;
        Qt::ItemFlags flags(const QModelIndex &index) const override {return QSqlQueryModel::flags(index) | Qt::ItemIsDragEnabled;}
    
    };
    
    class OverwriteModel : public QStandardItemModel{
        Q_DISABLE_COPY(OverwriteModel)
    
    public:
        using QStandardItemModel::QStandardItemModel;
        bool dropMimeData(const QMimeData *data, Qt::DropAction,
                             int, int, const QModelIndex &parent)  override
        {
            const QByteArray mimeData = data->data("application/x-qabstractitemmodeldatalist");
                QDataStream mimeReader(mimeData);
                int junk;
                QMap<int,QVariant> modelData;
                for(;;){
                    mimeReader.startTransaction();
                    mimeReader >> junk >> junk >> modelData;
                    if(!mimeReader.commitTransaction())
                        break;
                    setItemData(parent,modelData);
                }
            return true;
        }
    };
    
    namespace Ui {
    class pianificaproduzione;
    }
    
    class pianificaproduzione : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit pianificaproduzione(QWidget *parent = nullptr, const QString &Utente="", const QString &Password="");
        ~pianificaproduzione();
    
        QString Utente, Password;
        //OverwriteModel *model;
    
    private slots:
        void UpdateAllWidgets();
    
        void on_pushButton_30gg_clicked();
    
        void on_pushButton_45gg_clicked();
    
        void on_pushButton_60gg_clicked();
    
    private:
        Ui::pianificaproduzione *ui;
    
        //QSqlTableModel *mModel;
        QSqlDatabase aDatabase;
        QStandardItemModel *EModel, *PModel;
        DragSqlTableModel *source;
        OverwriteModel *model;       // declaration
    };
    
    

    in .cpp file i've initialize it :

    OverwriteModel *model = new OverwriteModel(40,6,this);
        //INTESTAZIONE COLONNE
        model->setHorizontalHeaderItem(0, new QStandardItem(QString("MACCHINA")));
        model->setHorizontalHeaderItem(1, new QStandardItem(QString("1")));
        model->setHorizontalHeaderItem(2, new QStandardItem(QString("2")));
        model->setHorizontalHeaderItem(3, new QStandardItem(QString("3")));
        model->setHorizontalHeaderItem(4, new QStandardItem(QString("4")));
        model->setHorizontalHeaderItem(5, new QStandardItem(QString("5")));
        model->setHorizontalHeaderItem(6, new QStandardItem(QString("6")));
        // MACCHINE
        model->setItem(0,0,new QStandardItem(QString("PRESSA T63")));
        model->item(0,0)->setBackground(Qt::lightGray);
        model->setItem(1,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(2,0,new QStandardItem(QString("PRESSA T100")));
        model->item(2,0)->setBackground(Qt::lightGray);
        model->setItem(3,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(4,0,new QStandardItem(QString("PRESSA T160")));
        model->item(4,0)->setBackground(Qt::lightGray);
        model->setItem(5,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(6,0,new QStandardItem(QString("PRESSA T199")));
        model->item(6,0)->setBackground(Qt::lightGray);
        model->setItem(7,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(8,0,new QStandardItem(QString("PRESSA T200")));
        model->item(8,0)->setBackground(Qt::lightGray);
        model->setItem(9,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(10,0,new QStandardItem(QString("PRESSA T201")));
        model->item(10,0)->setBackground(Qt::lightGray);
        model->setItem(11,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(12,0,new QStandardItem(QString("TRAPANI")));
        model->item(12,0)->setBackground(Qt::lightGray);
        model->setItem(13,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(14,0,new QStandardItem(QString("OMERA/PIEGAT")));
        model->item(14,0)->setBackground(Qt::lightGray);
        model->setItem(15,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(16,0,new QStandardItem(QString("CARTATRICE")));
        model->item(16,0)->setBackground(Qt::lightGray);
        model->setItem(17,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(18,0,new QStandardItem(QString("TRONCA/CNC")));
        model->item(18,0)->setBackground(Qt::lightGray);
        model->setItem(19,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(20,0,new QStandardItem(QString("PUNT/MONTA")));
        model->item(20,0)->setBackground(Qt::lightGray);
        model->setItem(21,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(22,0,new QStandardItem(QString("SALDATRICI")));
        model->item(22,0)->setBackground(Qt::lightGray);
        model->setItem(23,0,new QStandardItem(QString("N. PEZZI")));
        model->setItem(24,0,new QStandardItem(QString("ROBOT LUZZARA")));
        model->item(24,0)->setBackground(Qt::lightGray);
        model->setItem(25,0,new QStandardItem(QString("N. PEZZI")));
        for (int i=0; i<26; i++) {
            for (int j=1; j<=6; j++) {
                model->setItem(i,j, new QStandardItem(QString("")));
            }
    

    and all works fine..
    now on pushbutton click i have to read from model:

    QString Dati = model->data(model->index(0,i)).toString()
    

    return no data

    and if i try to write in one specific columns:

    QStandardItem *Cod = new QStandardItem(Codice);
    model->setItem(0,i, Cod);           //(i=0)
    

    my app crash..

    Can you help me to understand why??
    Thanks in advance.


  • Lifetime Qt Champion

    @TheCipo76 said in QStandardItemModel setItem crash..:

    OverwriteModel *model = new OverwriteModel(40,6,this);

    You create a local variable here...


  • Lifetime Qt Champion

    @TheCipo76 said in QStandardItemModel setItem crash..:

    OverwriteModel *model = new OverwriteModel(40,6,this);

    You create a local variable here...



  • This post is deleted!

Log in to reply