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. QTableWidget Operations over columns
Forum Updated to NodeBB v4.3 + New Features

QTableWidget Operations over columns

Scheduled Pinned Locked Moved General and Desktop
28 Posts 6 Posters 11.7k Views 3 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.
  • mrjjM mrjj

    Hi
    Put a breakpoint at
    if (item->column() == 4)
    and single step from there. You can then see what values u take etc.

    AbderaoufA Offline
    AbderaoufA Offline
    Abderaouf
    wrote on last edited by VRonin
    #17

    @mrjj
    Thanks mrjj. I think I fixed that based on Chris-kawa previous comments. I have another issue. I want to creat a Qpushbutton that fill the items of the second table based on the data of the first table using an if condition that check the QString of the 2nd column. If the value is "Y", the calculation should be on one way and if the value is "N" the calculation are done in another way. The code is not showing any error but .exe is crashing when I push the button. below is my code, thanks for reviewing it and advising:

    void CFAdvisor::on_Button4_clicked()
        {
        QString str1;
        QString str2 = "Y";
        QString str3 = "N";
    
          for (int row = 0; row < ui->m_ptable_ConvDesign->rowCount(); ++row)
          {
              QTableWidgetItem *stagepulse_item = ui->m_ptable_ConvDesign->item(row, 1);
              str1 = stagepulse_item->data(Qt::DisplayRole).toString();
          }
    
        for (int column = 0; column < ui->m_ptable_Conductor->columnCount(); ++column)
        {
          for (int row = 0; row < ui->m_ptable_Conductor->rowCount(); ++row)
          {
              if (str1 == str2)
               {
    
            QTableWidgetItem *PropConCond_item = ui->m_ptable_Conductor->item(row, 1);
            QTableWidgetItem *PropConConv_item = ui->m_ptable_ConvDesign->item(row,2);
            PropConCond_item->setData(Qt::DisplayRole,PropConConv_item->text().toDouble());
    
            QTableWidgetItem *SlurryVolCond_item = ui->m_ptable_Conductor->item(row, 4);
            QTableWidgetItem *SlurryVolConv_item = ui->m_ptable_ConvDesign->item(row,5);
            SlurryVolCond_item->setData(Qt::DisplayRole,qRound(((ui->Input_DesSLF-> text().toDouble())) * (SlurryVolConv_item->text().toDouble())));
    
            QTableWidgetItem *CleanVolCond_item = ui->m_ptable_Conductor->item(row, 2);
            CleanVolCond_item->setData(Qt::DisplayRole,qRound((SlurryVolCond_item->text().toDouble()) / ( 1 + ((ui->label_PropAVF1->text().toDouble()) * (PropConCond_item->text().toDouble())))));
    
               }
    
               if (str1 == str3)
               {
    
            QTableWidgetItem *PropConCond_item = ui->m_ptable_Conductor->item(row, 1);
            QTableWidgetItem *PropConConv_item = ui->m_ptable_ConvDesign->item(row,2);
            PropConCond_item->setData(Qt::DisplayRole,PropConConv_item->text().toDouble());
    
            QTableWidgetItem *SlurryVolCond_item = ui->m_ptable_Conductor->item(row, 4);
            QTableWidgetItem *SlurryVolConv_item = ui->m_ptable_ConvDesign->item(row,5);
            SlurryVolCond_item->setData(Qt::DisplayRole,SlurryVolConv_item->text().toDouble());
    
            QTableWidgetItem *CleanVolCond_item = ui->m_ptable_Conductor->item(row, 2);
            CleanVolCond_item->setData(Qt::DisplayRole,qRound((SlurryVolCond_item->text().toDouble()) / ( 1 + ((ui->label_PropAVF1->text().toDouble()) * (PropConCond_item->text().toDouble())))));
    
               }
            }
        }
    }
    

    Regards;

    Raouf

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #18

      Hi
      You should check all pointer to QTableWidgetItem * before using them.
      It might return NULL and u crash.

      Still if you place a break point and single step you should find the line that makes you crash.

      AbderaoufA 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        You should check all pointer to QTableWidgetItem * before using them.
        It might return NULL and u crash.

        Still if you place a break point and single step you should find the line that makes you crash.

        AbderaoufA Offline
        AbderaoufA Offline
        Abderaouf
        wrote on last edited by
        #19

        @mrjj I think it is that line, where I convert a QtableWidgetItem to QString:

        str1 = stagepulse_item->data(Qt::DisplayRole).toString();

        The syntax is right, I dont know what is the real reason though.

        mrjjM VRoninV 2 Replies Last reply
        0
        • AbderaoufA Abderaouf

          @mrjj I think it is that line, where I convert a QtableWidgetItem to QString:

          str1 = stagepulse_item->data(Qt::DisplayRole).toString();

          The syntax is right, I dont know what is the real reason though.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #20

          @Abderaouf
          It seems correct. You are getting a QVariant from data and call toString.
          So when debugging it crash on this line?

          AbderaoufA 1 Reply Last reply
          0
          • AbderaoufA Abderaouf

            @mrjj I think it is that line, where I convert a QtableWidgetItem to QString:

            str1 = stagepulse_item->data(Qt::DisplayRole).toString();

            The syntax is right, I dont know what is the real reason though.

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

            @Abderaouf said in QTableWidget Operations over columns:

            I dont know what is the real reason though

            probably stagepulse_item is a null pointer

            also your logic does not work, at the moment the calculation is based on whether the last line, second column in m_ptable_ConvDesign is Y or N

            "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

            AbderaoufA 1 Reply Last reply
            1
            • mrjjM mrjj

              @Abderaouf
              It seems correct. You are getting a QVariant from data and call toString.
              So when debugging it crash on this line?

              AbderaoufA Offline
              AbderaoufA Offline
              Abderaouf
              wrote on last edited by
              #22

              @mrjj When I run it, it works until I click on the button and then It crashes even though all the data are input. when I take out this line, the button doesn't work when I click at it.

              mrjjM 1 Reply Last reply
              0
              • AbderaoufA Abderaouf

                @mrjj When I run it, it works until I click on the button and then It crashes even though all the data are input. when I take out this line, the button doesn't work when I click at it.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #23

                @Abderaouf
                as @VRonin asks,
                could stagepulse_item be NULL ?

                1 Reply Last reply
                1
                • VRoninV VRonin

                  @Abderaouf said in QTableWidget Operations over columns:

                  I dont know what is the real reason though

                  probably stagepulse_item is a null pointer

                  also your logic does not work, at the moment the calculation is based on whether the last line, second column in m_ptable_ConvDesign is Y or N

                  AbderaoufA Offline
                  AbderaoufA Offline
                  Abderaouf
                  wrote on last edited by Abderaouf
                  #24

                  @VRonin how can I check if stagepulse_item is a null pointer??
                  I want to check the value of each line not only the last. I should probably add another for loop for the columns, right??

                  mrjjM 1 Reply Last reply
                  0
                  • AbderaoufA Abderaouf

                    @VRonin how can I check if stagepulse_item is a null pointer??
                    I want to check the value of each line not only the last. I should probably add another for loop for the columns, right??

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #25

                    @Abderaouf

                    if ( ! stagepulse_item) {
                    qDebug () << "NULL !!!";
                    return;
                    }

                    str1 = stagepulse_item->data(Qt::DisplayRole).toString();
                    ...

                    • I want to check the value of each line not only the last. I should probably add another for loop for the columns, right??

                    yes something like that. currently u loop m_ptable_ConvDesign first and set str1 but that should be checked
                    while u loop over the rows. So for a row if COL have Y/N then do it accordingly.

                    AbderaoufA 1 Reply Last reply
                    2
                    • mrjjM mrjj

                      @Abderaouf

                      if ( ! stagepulse_item) {
                      qDebug () << "NULL !!!";
                      return;
                      }

                      str1 = stagepulse_item->data(Qt::DisplayRole).toString();
                      ...

                      • I want to check the value of each line not only the last. I should probably add another for loop for the columns, right??

                      yes something like that. currently u loop m_ptable_ConvDesign first and set str1 but that should be checked
                      while u loop over the rows. So for a row if COL have Y/N then do it accordingly.

                      AbderaoufA Offline
                      AbderaoufA Offline
                      Abderaouf
                      wrote on last edited by
                      #26

                      @mrjj

                      I used the below and it worked fine:

                      QTableWidgetItem *stagepulse_item = ui->m_ptable_ConvDesign->item(row, 1);
                      if(stagepulse_item!=NULL)
                      {
                      str1 = stagepulse_item->data(Qt::DisplayRole).toString();
                      }

                      Cheers;

                      1 Reply Last reply
                      1
                      • AbderaoufA Offline
                        AbderaoufA Offline
                        Abderaouf
                        wrote on last edited by
                        #27

                        Gents;

                        One more challenge. I am trying to create a menu in my QTabWidget using the below basic code:

                        void CFAdvisor::CreateMenu()

                        {
                        fileMenu = MenuBar().addMenu(tr("&File"));
                        fileMenu->addAction(actionExit);
                        fileMenu-> addAction(actionPrint);
                        helpMenu = MenuBar() ->addMenu(tr("&Help"));
                        helpMenu-> addAction(actionContent);
                        helpMenu-> addAction(actionAbout);

                        }

                        I have a the error message though, it says the following:

                        C:\computing\Qt Designer\cfadvisor.cpp:488: error: 'menuBar' was not declared in this scope
                        fileMenu = menuBar()->addMenu(tr("&File"));
                        ^

                        Any advise on how to correct this error. this is the Qt version I have.

                        0_1483725857061_upload-e59f0784-f714-4f46-8b9d-31dd599923a6

                        Cheers

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #28

                          Hi
                          Where is MenuBar() ?
                          if CFAdvisor is not a main window type then you need to create the MenuBar yourself if not already
                          added in Designer.

                          http://www.codeprogress.com/cpp/libraries/qt/QMenuBarAddMenu.php

                          1 Reply Last reply
                          0

                          • Login

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