Processing is not going to the right function



  • Hi,
    I have the following function:

    void Review::on_pushButton_Fix_clicked()
    {
        Additem *mAdditem = new Additem(this);
        mAdditem->FixFriend (sIDReview, nameReview, whatReview, fileNameReview, materialReview, colorReview, descriptionReview, date, SignedbyReview, historyReview, ageReview, notesReview);
    
        mAdditem->show ();
    }
    

    After this function it continues processing Additem, but from the first function in the class and not from FixFriend.
    FixFriend looks like this:

    void Additem::FixFriend(QString sIDReview, QString nameReview, QString whatReview, QString fileNameReview, QString materialReview, QString colorReview, QString descriptionReview,QString date , QString SignedbyReview, QString historyReview, QString ageReview, QString notesReview)
    {
        sID = sIDReview;
        name = nameReview;
        what = whatReview;
        fileNameChosen = fileNameReview;
        material = materialReview;
        color = colorReview;
        description = descriptionReview;
    //	dateReview = date;
        Signedby = SignedbyReview;
        history = historyReview;
        age = ageReview;
        notes = notesReview;
    
        QPixmap pix1(fileNameChosen);
        int w = 0;
        int h = 0;
        w = ui->display_Label->width ();
        h = ui->display_Label->height ();
        ui->display_Label->setPixmap (pix1.scaled(w,h,Qt::KeepAspectRatio));
        adoptDate = adoptDate.fromString (date,"MM/dd/yyyy");
        AddFriend (sID, name, what, material, color, description, adoptDate, Signedby, history, age, notes);
    }
    

    Any idea why it's not going to FixFriend? Thank you for your help.


  • Moderators

    @gabor53 Could you please explain what you mean? Your current explanation is not understandable.
    What does this mean: "After this function it continues processing Additem, but from the first function in the class and not from FixFriend."
    What is "first function"? What does "processing Additem" mean?
    So, what happens and what should happen?

    One note: you should use lower first letter for function/method names: fixFriend instead of FixFriend. At least you should have one naming schema instead of a mix like now: on_pushButton_Fix_clicked <-> FixFriend.



  • Hi @jsulm
    Here are the 2 cpps involved:

    review.cpp

    #include "review.h"
    #include "ui_review.h"
    
    Review::Review(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Review)
    {
        ui->setupUi(this);
    
        connect(this,&Review::RecAdded,this,&Review::updateTable);
    
    }
    
    Review::~Review()
    {
        delete ui;
    }
    
    void Review::dispRev(QString sID, QString &name, QString &whatChosen, QString &fileNameChosen, QString &materialChosen, QString &colorChosen, QString &description, QDate &adoptDate, QString &SignedbyChosen, QString &history, QString &age, QString &notesTemp)
    {
        name = name.simplified ();
        description = description.simplified ();
    
        ui->ID_display_Label->setText (sID);
        ui->Name_Disp_Label->setText (name);
        ui->What_Display_Label->setText (whatChosen);
        ui->MadeOf_Disp_Label->setText (materialChosen);
        ui->Color_Disp_Label->setText (colorChosen);
        ui->textBrowser_Descr->setText (description);
        ui->AdoptionDate_Disp_Label->setText (adoptDate.toString ("MM/dd/yyyy"));
        ui->SignedBy_Disp_label->setText (SignedbyChosen);
        ui->textBrowser_History->setText (history);
        ui->Age_Display_Label_2->setText (age);
        ui->textBrowser_Notes->setText (notesTemp);
    
    
        QPixmap pix2(fileNameChosen);
        int w = 0;
        int h = 0;
        w = ui->Image_Display_Label->width ();
        h = ui->Image_Display_Label->height ();
        ui->Image_Display_Label->setPixmap (pix2.scaled(w,h,Qt::KeepAspectRatio));
    
        QFile fileReview(fileNameChosen);
    
        if(fileReview.open (QIODevice::ReadOnly))
            {
                fileByteArray = fileReview.readAll ();
            }
    
        nameReview = name;
        whatReview = whatChosen;
        fileNameReview = fileNameChosen;
        materialReview = materialChosen;
        colorReview = colorChosen;
        descriptionReview = description;
        adoptDateReview = adoptDate;
        SignedbyReview = SignedbyChosen;
        historyReview = history;
        sIDReview = sID;
        ageReview = age;
        notesReview = notesTemp;
    
    }
    
    
    void Review::on_pushButton_Add_to_DB_clicked()
    {
    
        date = adoptDateReview.toString ("MM/dd/yyyy");
        qDebug() << "date in review.cpp: " << date;
        QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE","writedb");
        db.setDatabaseName (fileQstring);
    
        QSqlQuery querys(db);
    
    
        if(!db.open ())
    
            {
                qDebug() << "The database is not open (submit)!";
            }
        else
            {
                qDebug() << "The database is open (submit)!";
            }
        querys.prepare("INSERT INTO Items (ID, Name, Pic, Description, AdoptDate, History, Age, Notes, Color, Material, Signed, What) VALUES(:ID, :Name, :Pic, :Description, :AdoptDate, :History, :Age, :Notes, :Color, :Material, :Signed, :What)" );
        querys.bindValue (":ID",sIDReview);
        querys.bindValue (":Name",nameReview);
        querys.bindValue (":Pic",fileByteArray);
        querys.bindValue (":Description",descriptionReview);
        querys.bindValue (":AdoptDate",date);
        querys.bindValue (":History",historyReview);
        querys.bindValue (":Age",ageReview);
        querys.bindValue (":Notes",notesReview);
        querys.bindValue (":Color",colorReview);
        querys.bindValue (":Material",materialReview);
        querys.bindValue (":Signed",SignedbyReview);
        querys.bindValue (":What",whatReview);
    
        bool result = querys.exec ();
    
        if(!result)
            {
                qDebug() <<"Error inserting into the main db!" << querys.lastError ();
    
                QMessageBox::warning (this,"Add to Database Warning","<b><font size='16' color='red'>Error 1002: The Friend was not added to the database.");
    
            }
        else
            {
                qDebug() << "Entered FunctAdd OK loop.";
    
                QMessageBox::information (this,"Confirmation","<b><font size = '16' color = 'green'>The Friend was added to the database.</font>");
    
    
    
                emit RecAdded ();
            }
    }
    
    
    void Review::on_pushButton_Fix_clicked()
    {
        Additem *mAdditem = new Additem(this);
        mAdditem->FixFriend (sIDReview, nameReview, whatReview, fileNameReview, materialReview, colorReview, descriptionReview, date, SignedbyReview, historyReview, ageReview, notesReview);
    
        mAdditem->show ();
    }
    
    
    void Review::updateTable()
    {
        qDebug() << "Entered updateTable!";
        MainWindow *mMainWindow = new MainWindow(this);
    
        mMainWindow->Addview ();
        mMainWindow->show ();
    }
    
    

    additem.cpp

    #include "additem.h"
    #include "ui_additem.h"
    
    Additem::Additem(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Additem)
    {
        ui->setupUi(this);
    
        connection();
    
        AddFriend (sID, name,what, material, color,description, adoptDate, Signedby, history, age, notes);
    
    }
    
    Additem::~Additem()
    {
        delete ui;
    }
    
    void Additem::AddFriend(QString &sID, QString& name, QString& what, QString& material, QString& color, QString& description, QDate &adoptDate, QString &Signedby, QString& history, QString age, QString notes)
    {
    
        //Add ID counter
    
         if(!db.open ())
            {
                QMessageBox::critical (this,"Error 1001","The database can't be opened!");
                qDebug() << "Error 1001: "  << db.lastError ();
            }
        else
            {
                QSqlQuery query("SELECT ID FROM Items ORDER BY ID DESC",db);
    
                if(query.isActive()!= true)
                    {
                        QMessageBox::critical (this,"Error 1002","The query is not active try again later!");
                    }
    
                query.first ();
    
                LastID = query.value(0).toInt ();
    
                ItemID  = LastID+1;
    
                sID = QString::number(ItemID);
    
                ui->ID_Display ->setText (sID);
            }
    
    
    //Getting name
    
        if(name == "")
            {
                name = ui->LineEdit_Name->text ();
            }
        else
            {
                ui->LineEdit_Name->setText (name);
            }
    
        connect(ui->LineEdit_Name, SIGNAL(editingFinished()),this, SLOT(readAndValidate ()));
    
    
    //Getting what
    
        {
    
            if(!db.open ())
                {
                    QMessageBox::critical (this,"Error 1003","The database can not be reached! Try again later.");
                }
    
            QSqlQuery query_what ("SELECT What FROM What_Table ORDER BY What asc",db);
    
            if(query_what.isActive ()==false)
                {
                    QMessageBox::critical (this,"Error 1004","The database can not be reached! Try again later.");
                }
    
            while (query_what.next ())
                {
                    whatItem = query_what.value (0).toString ();
                    ui->What_Combo->addItem (whatItem);
                }
    
        }
    
        if (what != "")
            {
                ui->What_Combo->setCurrentText (what);
            }
    
        whatChosen = ui->What_Combo->currentText ();
    
        connect(ui->What_Combo,SIGNAL(currentIndexChanged(int)),this,SLOT(processcombo(int)));
    
    
    //Getting material (Made of)
    
    
        if(!db.open ())
            {
                QMessageBox::critical (this,"Error 2009","The database can not be reached! Try again later.");
            }
    
    
        QSqlQuery query_Material ("SELECT Material FROM MaterialTable ORDER BY Material asc",db);
    
        if(query_Material.isActive()==true)
            {
    //                qDebug() << "The query is active (Material).";
            }
        else
            {
                QMessageBox::critical (this,"Error 2003: ","The database is unavailable.");
            }
    
        while (query_Material.next ())
            {
                materialItem = query_Material.value (0).toString ();
                ui->Material_Combo->addItem (materialItem);
            }
    
        if(material !="")
            {
                ui->Material_Combo->setCurrentText (material);
            }
    
        materialChosen = ui->Material_Combo->currentText ();
    
        connect (ui->Material_Combo,SIGNAL(currentIndexChanged(int)),this,SLOT(getMaterial(int)));
    
    
    //Get color
    
        if(!db.open ())
            {
    
                QMessageBox::critical (this,"Error 2002: ","The database is unavailable.");
            }
        else
            {
    //                qDebug() << "The database is open (Color)!";
            }
    
        QSqlQuery query_color ("SELECT Color FROM ColorTable ORDER BY Color asc",db);
    
        if(query_color.isActive()==true)
            {
    //                qDebug() << "The query is active (Color).";
            }
        else
            {
                QMessageBox::critical (this,"Error 2005: ","The query is not active.");
            }
    
        while (query_color.next ())
            {
                colorItem = query_color.value(0).toString();
    
                ui->color_Combo->addItem(colorItem);
            }
    
        if(color !="")
            {
                ui->color_Combo->setCurrentText (color);
            }
    
        colorChosen = ui->color_Combo->currentText ();
    
        connect (ui->color_Combo,SIGNAL(currentIndexChanged(int)),this,SLOT(addColor(int)));
    
    
    //Getting descrpition
    
        if(description != "")
            {
                ui->descr_TextEdit->setText (description);
            }
    
        connect(ui->descr_TextEdit, &QTextEdit::textChanged, [&]()
        {
            description = getDescription ();
        });
    
    
        //Getting adoption date
    
    //    ui->Adoption_dateEdit->setDate (QDate::currentDate ());
        ui->Adoption_dateEdit->setDisplayFormat ("MM/dd/yyyy");
    
        adoptDate = ui->Adoption_dateEdit->date ();
        qDebug() << "AdoptDate from Getting adoption date: " << adoptDate;
    
    
    
        connect(ui->Adoption_dateEdit,SIGNAL(dateChanged(QDate)),this,SLOT(getDate()));
    
    
    
    //Add signedby
    
        if(!db.open ())
            {
                qDebug() << "The database is NOT open! (Signedby1)";
            }
        else
            {
    //                qDebug() << "The database is open (Signedby1)!";
            }
    
        QSqlQuery query_signedby ("SELECT Signedby FROM Signed_Table ORDER BY Signedby asc",db);
    
        if(query_signedby.isActive()==true)
            {
    //                qDebug() << "The query is active (Signedby1).";
            }
        else
            {
    //                qDebug() << "The query is NOT active (Signedby1).";
            }
    
        while (query_signedby.next ())
            {
                SignedbyItem = query_signedby.value(0).toString();
    
                ui->signedby_Combo->addItem(SignedbyItem);
            }
    
        if(Signedby != "")
            {
                ui->signedby_Combo->setCurrentText (Signedby);
            }
    
    
        connect(ui->signedby_Combo,SIGNAL(currentIndexChanged(int)),this,SLOT(addSignedby(int)));
    
    
    //Add history
    
        if(history != "")
            {
                ui->history_TextEdit->setText (history);
            }
    
    
        connect(ui->history_TextEdit, &QTextEdit::textChanged, [&]()
        {
            history = getHistory ();
        });
    
    //Add age
    
        if(age != "")
            {
                ui->age_LineEdit->setText (age);
            }
        age = ui->age_LineEdit->text ();
    
    //    connect(ui->age_LineEdit, SIGNAL(editingFinished()), this, SLOT(getAge()));
    
    //Add notes
    
        if(notes != "")
            {
                ui->notes_TextEdit->setText (notes);
            }
    
    
        connect(ui->notes_TextEdit, &QTextEdit::textChanged, [this]()
        {
            QString notes = getNotes ();
        });
    
    }
    
    
    void Additem::FixFriend(QString sIDReview, QString nameReview, QString whatReview, QString fileNameReview, QString materialReview, QString colorReview, QString descriptionReview,QString date , QString SignedbyReview, QString historyReview, QString ageReview, QString notesReview)
    {
        sID = sIDReview;
        name = nameReview;
        what = whatReview;
        fileNameChosen = fileNameReview;
        material = materialReview;
        color = colorReview;
        description = descriptionReview;
    //	dateReview = date;
        Signedby = SignedbyReview;
        history = historyReview;
        age = ageReview;
        notes = notesReview;
    
        QPixmap pix1(fileNameChosen);
        int w = 0;
        int h = 0;
        w = ui->display_Label->width ();
        h = ui->display_Label->height ();
        ui->display_Label->setPixmap (pix1.scaled(w,h,Qt::KeepAspectRatio));
        adoptDate = adoptDate.fromString (date,"MM/dd/yyyy");
    
        connection();
    
        AddFriend (sID, name, what, material, color, description, adoptDate, Signedby, history, age, notes);
    }
    
    
    void Additem::connection()
    {
        connectionNames2 = db.connectionNames ();
    
        if((connectionNames2.contains ("Friend")) == false)
            {
                db = QSqlDatabase::addDatabase ("QSQLITE","Friend");
                db.setDatabaseName (fileQstring);
                qDebug() << "Connection created in connection(). ";
            }
    
    }
    
    
    QString Additem::getDescription()
    {
        description = ui->descr_TextEdit->toPlainText ();
        description = description.simplified ();
    
        return description;
    }
    
    QString Additem::getHistory()
    {
        history = ui->history_TextEdit->toPlainText ();
        history = history.simplified ();
    
        return history;
    }
    
    
    QString Additem::getNotes()
    {
        notesTemp = ui->notes_TextEdit->toPlainText ();
    //    notesTemp = notes.simplified ();
    
        qDebug() << "notesTemp in getNotes: " << notesTemp;
    
        return notesTemp;
    }
    
    
    void Additem::readAndValidate()
    {
        name = ui->LineEdit_Name->text ();
        name = name.simplified ();
    
        if(name.length ()<2)
            {
                ui->incorrect_Label->setText ("The name is too short!");
                QPixmap pix("C:/Programming/Projects/Folkfriends/icons/angry.png");
    
                ui->angry_image_Label->setScaledContents (true);
                ui->angry_image_Label->setPixmap (pix);
            }
        else
            {
                ui->incorrect_Label->setText ("");
                QPixmap pix2("C:/Programming/Projects/FolkFriends/icons/Check-icon.png");
                ui->angry_image_Label->setScaledContents (true);
                ui->angry_image_Label->setPixmap (pix2);
            }
    }
    
    
    QString Additem::processcombo(int)
    {
        whatChosen = ui->What_Combo->currentText ();
        whatChosen = whatChosen.simplified ();
    
        {
            if(!db.open ())
                {
    //                qDebug() << "The database is NOT open! (addWhat)";
                }
            else
                {
    //                qDebug() << "The database is open (addWhat)!";
                }
    
            QSqlQuery query_addWhat(db);
            query_addWhat.prepare ("INSERT OR IGNORE INTO What_Table (What)"
                                   "VALUES(:What)");
    
            query_addWhat.bindValue (":What",whatChosen);
            query_addWhat.exec ();
    
            return whatChosen;
        }
    
    }
    
    QString Additem::getMaterial(int)
    {
    
        materialChosen = ui->Material_Combo->currentText ();
        materialChosen = materialChosen.simplified ();
    
        if(!db.open ())
            {
    //            QMessageBox::critical(this,"Error: the material was not added to the database!");
    
            }
        else
            {
                qDebug() << "The database is open (addMaterial)!";
            }
    
        QSqlQuery query_addMaterial(db);
        query_addMaterial.prepare ("INSERT INTO MaterialTable (Material)"
                                   "VALUES(:Material)");
    
        query_addMaterial.bindValue (":Material",materialChosen);
        query_addMaterial.exec ();
    
        return materialChosen;
    }
    
    
    void Additem::addColor(int)
    {
        colorChosen = ui->color_Combo->currentText ();
        colorChosen = colorChosen.simplified ();
    
        {
            db = QSqlDatabase::addDatabase ("QSQLITE","addingColor");
            db.setDatabaseName (fileQstring);
    
            if(!db.open ())
                {
    //                qDebug() << "The database is NOT open! (addColor)";
                }
            else
                {
    //                qDebug() << "The database is open (addColor)!";
                }
    
            QSqlQuery query_addingColor(db);
            query_addingColor.prepare ("INSERT INTO ColorTable (Color)" "VALUES(:Color)");
            query_addingColor.bindValue (":Color",colorChosen);
            query_addingColor.exec ();
        }
    
    }
    
    //void Additem::addMonth(int)
    //{
    //    monthChosen = ui->month_Combo->currentText ();
    ////    qDebug() <<"Month: " << monthChosen;
    //}
    
    //void Additem::addDay(int)
    //{
    //    dayChosen = ui->day_Combo->currentText ();
    ////    qDebug() <<"Day: " << dayChosen;
    //}
    
    //void Additem::  addYear(int)
    //{
    //    yearChosen = ui->year_Combo->currentText ();
    ////    qDebug() << "Year: " << yearChosen;
    //}
    
    
    void Additem::addSignedby(int)
    {
    //    qDebug() << "addSignedby works!";
    
        SignedbyChosen = ui->signedby_Combo->currentText ();
        SignedbyChosen = SignedbyChosen.simplified ();
    
    //    qDebug() << "The new signedby is " << SignedbyChosen;
    
        {
    //        db = QSqlDatabase::addDatabase ("QSQLITE","addSignedby2");
    //        db.setDatabaseName (fileQstring);
    
            if(!db.open ())
                {
    //                qDebug() << "The database is NOT open! (addSignedby2)";
                }
            else
                {
    //                qDebug() << "The database is open (addSignedby2)!";
                }
    
            QSqlQuery query_addSignedby(db);
            query_addSignedby.prepare ("INSERT INTO Signed_Table (Signedby)"
                                       "VALUES(:Signedby)");
    
            query_addSignedby.bindValue (":Signedby",SignedbyChosen);
            query_addSignedby.exec ();
    
        }
    
    
        //    qDebug() <<"SignedBy: " << newSignedby;
    }
    
    QDate Additem::getDate()
    {
        adoptDate = ui->Adoption_dateEdit->date ();
    
        qDebug() << "Adoption date: " << adoptDate;
    
        return adoptDate;
    }
    
    
    QString Additem::on_Image_Button_clicked()
    {
        QString sPath = "C:/";
    
        QFileDialog *fileDialog = new QFileDialog;
    
        fileName = fileDialog->getOpenFileName(this,
                                               tr("Finding Friend's Image"),sPath, tr("Image Files (*.png *.jpg *.bmp)"));
    
    //    qDebug() << "filename from findimage(): " << fileName;
    //        ui->display_Label->setFixedSize (100,100);
    
        if(fileName == "")
            {
    //            qDebug() << "No image was chosen.";
                QMessageBox::warning (this,"Error 1002","No image was choosen!");
            }
        else
            {
    //            qDebug() << "Image was chosen!";
    
                QPixmap pix(fileName);
    
                int w = ui->display_Label->width ();
                int h = ui->display_Label->height ();
                ui->display_Label->setPixmap (pix.scaled(w,h,Qt::KeepAspectRatio));
    
                QFile file(fileName);
    
                if(file.open (QIODevice::ReadOnly))
                    {
                        //                     qDebug() << "File  is open!";
                        byteArray = file.readAll ();
                    }
                else
                    {
    //                    qDebug() << "File is not open!";
                    }
    
            }
        fileNameChosen = fileName;
        return fileNameChosen;
    }
    
    
    void Additem::on_pushButton_Review_clicked()
    {
    //    connectionNames = db.connectionNames ();
    //    qDebug() << "Connection names: " << connectionNames;
    
    //    db.close ();
    //    QSqlDatabase::removeDatabase ("Friend");
    
        Review  *mReview = new Review(this);
    
    //    qDebug() << "NotesTemp in additem.cpp: " << notesTemp;
        qDebug() << "AdoptDate from pushbutton:" << adoptDate;
        mReview->dispRev (sID, name, whatChosen, fileNameChosen, materialChosen, colorChosen, description, adoptDate, SignedbyChosen, history, age, notesTemp);
    
        mReview->show ();
    }
    
    

    What iss upposed to happen:
    When theuser clicks pushButton_Fix it should execute FixFriend from Additem. Instead it starts processing AddFriend.
    I will use the recommended naming from now own.


  • Moderators

    @gabor53 Well, at the end of FixFriend you call AddFriend.
    Are you sure FixFriend wasn't called? You can simply set a break point in FixFriend and debug your app. Or put some debug out put to console like:

    void Additem::FixFriend(QString sIDReview, QString nameReview, QString whatReview, QString fileNameReview, QString materialReview, QString colorReview, QString descriptionReview,QString date , QString SignedbyReview, QString historyReview, QString ageReview, QString notesReview)
    {
        qDebug() << Q_FUNC_INFO;
    


  • @jsulm
    I ran debug and it skips Additem::FixFriend and goes first to Additem::AddFriend. It generates a bunch of driver not loaded messages.



  • in Review::on_pushButton_Fix_clicked() you call Additem *mAdditem = new Additem(this); in the constructor of Additem you call AddFriend (sID, name,what, material, color,description, adoptDate, Signedby, history, age, notes); so it does indeed execute AddFriend before calling FixFriend. it does not skip anything.

    For this kind of problems, looking at the stack trace helps A LOT!


Log in to reply
 

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