Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signal doesn't work properly

Signal doesn't work properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
32 Posts 4 Posters 6.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I have the following signal:
    review.h.

    signals:
        void RecAdded();
    

    review.cpp:
    constructor:

    Review::Review(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Review)
    {
        ui->setupUi(this);
    
        connect(this,&Review::RecAdded,this,&Review::updateTable);
    }
    

    emitting signals in Review.cpp

    void Review::on_pushButton_Add_to_DB_clicked()
    {
        QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE","writedb");
        db.setDatabaseName (fileQstring);
    //    bool OKadd = db.open ();
        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, Month, Day, Year, History, Age, Notes, Color, Material, Signed, What) VALUES(:ID, :Name, :Pic, :Description, :Month, :Day, :Year, :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 (":Month",monthReview);
        querys.bindValue (":Day",dayReview);
        querys.bindValue (":Year",yearReview);
        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>");
    
                db.close ();
    
                emit RecAdded ();
            }
    }
    

    function update:

    void Review::updateTable()
    {
       qDebug() << "Entered updateTable!";
        MainWindow mMainWindow;
        mMainWindow.Addview();
    }
    

    mainwindow.cpp:

    void MainWindow::Addview()
    {
    
        QSqlQuery query_main  ("SELECT ID, Name,Pic, Description FROM Items ORDER BY NAME ASC ");
    
        if(query_main.isActive()==true)
            {
                qDebug() << "The query is active.";
            }
        else
            {
                qDebug() << "The query is NOT active." << query_main.lastError ();
            }
    
        QStandardItemModel *smodel = new QStandardItemModel(this);
        ui->tableView->setModel (smodel);
    

    When the signal RecAdded() emitted updateTable() and Addview starts, but after the

      if(query_main.isActive()==true)
    

    is executed control goes back to the updateTable(). Basically it goes in a cycle between updateTable() and the beginning of Addview. Please help me figure why.

    raven-worxR 1 Reply Last reply
    0
    • G gabor53

      Hi,
      I have the following signal:
      review.h.

      signals:
          void RecAdded();
      

      review.cpp:
      constructor:

      Review::Review(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::Review)
      {
          ui->setupUi(this);
      
          connect(this,&Review::RecAdded,this,&Review::updateTable);
      }
      

      emitting signals in Review.cpp

      void Review::on_pushButton_Add_to_DB_clicked()
      {
          QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE","writedb");
          db.setDatabaseName (fileQstring);
      //    bool OKadd = db.open ();
          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, Month, Day, Year, History, Age, Notes, Color, Material, Signed, What) VALUES(:ID, :Name, :Pic, :Description, :Month, :Day, :Year, :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 (":Month",monthReview);
          querys.bindValue (":Day",dayReview);
          querys.bindValue (":Year",yearReview);
          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>");
      
                  db.close ();
      
                  emit RecAdded ();
              }
      }
      

      function update:

      void Review::updateTable()
      {
         qDebug() << "Entered updateTable!";
          MainWindow mMainWindow;
          mMainWindow.Addview();
      }
      

      mainwindow.cpp:

      void MainWindow::Addview()
      {
      
          QSqlQuery query_main  ("SELECT ID, Name,Pic, Description FROM Items ORDER BY NAME ASC ");
      
          if(query_main.isActive()==true)
              {
                  qDebug() << "The query is active.";
              }
          else
              {
                  qDebug() << "The query is NOT active." << query_main.lastError ();
              }
      
          QStandardItemModel *smodel = new QStandardItemModel(this);
          ui->tableView->setModel (smodel);
      

      When the signal RecAdded() emitted updateTable() and Addview starts, but after the

        if(query_main.isActive()==true)
      

      is executed control goes back to the updateTable(). Basically it goes in a cycle between updateTable() and the beginning of Addview. Please help me figure why.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @gabor53

      void Review::updateTable()
      {
         qDebug() << "Entered updateTable!";
          MainWindow mMainWindow;
          mMainWindow.Addview();
      }
      

      you are creating a MainWindow object on the stack. Means it will get deleted immediately after the Addview() call.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      G 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @gabor53

        void Review::updateTable()
        {
           qDebug() << "Entered updateTable!";
            MainWindow mMainWindow;
            mMainWindow.Addview();
        }
        

        you are creating a MainWindow object on the stack. Means it will get deleted immediately after the Addview() call.

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        Hi @raven-worx
        How to do it correctly?

        raven-worxR 1 Reply Last reply
        0
        • G gabor53

          Hi @raven-worx
          How to do it correctly?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @gabor53
          you need a pointer to your MainWindow object and call AddView() on it instead.
          I guess somewhere you have already created a MainWindow?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          G 2 Replies Last reply
          2
          • raven-worxR raven-worx

            @gabor53
            you need a pointer to your MainWindow object and call AddView() on it instead.
            I guess somewhere you have already created a MainWindow?

            G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            @raven-worx
            Yes, I have MainWindow as a class.

            raven-worxR 1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              This probably won't do what you intended but it works

              void Review::updateTable()
              {
              qDebug() << "Entered updateTable!";
              MainWindow* mMainWindow=new mMainWindow(NULL);
              mMainWindow->Addview();
              mMainWindow->show();
              }

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • G gabor53

                @raven-worx
                Yes, I have MainWindow as a class.

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by raven-worx
                #7

                @gabor53
                i meant an instance of the MainWindow already.
                Since you named it "MainWindow" you might want to create i tin the main() function.

                Then somewhere you need to make the connection at a point where you have pointers of both objects/windows.

                You could also directly connect the Review window with the MainWindow (declare AddView() as slot):

                connect(mReviewWindow,&Review::RecAdded,mMainWindow,&MainWindow::AddView);
                

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                2
                • raven-worxR raven-worx

                  @gabor53
                  you need a pointer to your MainWindow object and call AddView() on it instead.
                  I guess somewhere you have already created a MainWindow?

                  G Offline
                  G Offline
                  gabor53
                  wrote on last edited by
                  #8

                  @raven-worx
                  I did the following in Review.cpp:

                  {
                      qDebug() << "Entered updateTable!";
                      MainWindow *mMainWindow = new MainWindow(this);
                      mMainWindow->Addview();
                  }
                  
                  It goes to Addview() in MainWindow.cpp, but after processing 
                  

                  ``ui->tableView->setModel (smodel);`

                  it jumps back to 
                  

                  void Review::updateTable()

                  and the actual table in MainWindow is not updated.
                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9

                    Deleted my previous post as it was wrong, I'm surprised it compiles with duplicate symbols as mMainWindow seems to be in your code

                    what you are trying to do is insert a row in the db, then delete your model completely and rebuild it from scratch. would it be easier to pass the new line as arguments to RecAdded and use them to append the row?

                    to put my solution here I need to know the type of:

                    • sIDReview
                    • nameReview
                    • fileByteArray (QByteArray?)
                    • descriptionReview
                    • monthReview (int?)
                    • dayReview (int?)
                    • yearReview (int?)
                    • historyReview
                    • ageReview (int?)
                    • notesReview
                    • colorReview
                    • materialReview
                    • SignedbyReview (QString?)
                    • whatReview

                    EDIT:

                    it jumps back to

                    it's not jumping back, you are creating a new MainWindow and it's constructor is calling a new review, it's not a jump back, it's a different instance

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    G 1 Reply Last reply
                    0
                    • VRoninV VRonin

                      Deleted my previous post as it was wrong, I'm surprised it compiles with duplicate symbols as mMainWindow seems to be in your code

                      what you are trying to do is insert a row in the db, then delete your model completely and rebuild it from scratch. would it be easier to pass the new line as arguments to RecAdded and use them to append the row?

                      to put my solution here I need to know the type of:

                      • sIDReview
                      • nameReview
                      • fileByteArray (QByteArray?)
                      • descriptionReview
                      • monthReview (int?)
                      • dayReview (int?)
                      • yearReview (int?)
                      • historyReview
                      • ageReview (int?)
                      • notesReview
                      • colorReview
                      • materialReview
                      • SignedbyReview (QString?)
                      • whatReview

                      EDIT:

                      it jumps back to

                      it's not jumping back, you are creating a new MainWindow and it's constructor is calling a new review, it's not a jump back, it's a different instance

                      G Offline
                      G Offline
                      gabor53
                      wrote on last edited by
                      #10

                      @VRonin
                      If it helps I can post the whole code.

                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        the contents of review.h should be enough

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        G 2 Replies Last reply
                        0
                        • VRoninV VRonin

                          the contents of review.h should be enough

                          G Offline
                          G Offline
                          gabor53
                          wrote on last edited by
                          #12

                          @VRonin
                          review.h
                          review.cpp
                          mainwindow.cpp
                          Thank you.

                          1 Reply Last reply
                          0
                          • VRoninV VRonin

                            the contents of review.h should be enough

                            G Offline
                            G Offline
                            gabor53
                            wrote on last edited by
                            #13

                            @VRonin
                            sIDReview (QString)
                            nameReview (QString)
                            fileByteArray (QByteArray)
                            descriptionReview ((QString))
                            monthReview (QString)
                            dayReview (QString)
                            yearReview (QString)
                            historyReview (QString)
                            ageReview (QString)
                            notesReview (QString)
                            colorReview (QString)
                            materialReview (QString)
                            SignedbyReview (QString)
                            whatReview (QString)

                            1 Reply Last reply
                            0
                            • VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by
                              #14

                              What's the link between Review and MainWindow?

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              G 1 Reply Last reply
                              0
                              • VRoninV VRonin

                                What's the link between Review and MainWindow?

                                G Offline
                                G Offline
                                gabor53
                                wrote on last edited by
                                #15

                                @VRonin
                                In Review the data added to the database and I want to update the table in MainWindow using the record just added to the db.

                                VRoninV 1 Reply Last reply
                                0
                                • G gabor53

                                  @VRonin
                                  In Review the data added to the database and I want to update the table in MainWindow using the record just added to the db.

                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by
                                  #16

                                  @gabor53 Sorry, I did not explained myself. Review is a Dialog, where do you make this dialog appear?

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  G 1 Reply Last reply
                                  0
                                  • VRoninV VRonin

                                    @gabor53 Sorry, I did not explained myself. Review is a Dialog, where do you make this dialog appear?

                                    G Offline
                                    G Offline
                                    gabor53
                                    wrote on last edited by
                                    #17

                                    @VRonin
                                    In MainWindow line 75

                                    void MainWindow::on_actionAdd_a_Friend_triggered()
                                    {
                                        Additem *mAddItem = new Additem;
                                        mAddItem->exec ();
                                    }
                                    
                                    1 Reply Last reply
                                    0
                                    • VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by VRonin
                                      #18

                                      Now you lost me completely.
                                      typedef Review Additem; ?? I guess not because

                                      void Review::on_pushButton_Fix_clicked()
                                      {
                                          Additem *mAdditem = new Additem(this);
                                      

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      G 1 Reply Last reply
                                      0
                                      • VRoninV VRonin

                                        Now you lost me completely.
                                        typedef Review Additem; ?? I guess not because

                                        void Review::on_pushButton_Fix_clicked()
                                        {
                                            Additem *mAdditem = new Additem(this);
                                        
                                        G Offline
                                        G Offline
                                        gabor53
                                        wrote on last edited by
                                        #19

                                        @VRonin
                                        Originally, after main.cpp MainWindow.cpp loads. MainWindow has the tableview that displays 4 columns from the database. The user can call Additem.cpp from MainWindow to add more items to the db. After data entry Review.cpp is called where the user can review the data he/she entered. Here there are two choices: either click PushButton_fix to correct errors (this takes back to Additem.cpp) or if all correct you can click Add_to_Db and the item is added to the db. After adding RecAdded signal emitted to add the new record to the tableview in MainWindow. I hope this helps.

                                        1 Reply Last reply
                                        0
                                        • VRoninV Offline
                                          VRoninV Offline
                                          VRonin
                                          wrote on last edited by
                                          #20

                                          cry

                                          Ok, your design is "borderline". Could you please post additem.h and additem.cpp too please?

                                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                          ~Napoleon Bonaparte

                                          On a crusade to banish setIndexWidget() from the holy land of Qt

                                          G 1 Reply Last reply
                                          3

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved