QTableWidget Operations over columns
-
The error says it all: you need to capture the value before you can use it inside a lambda. For example if the input is a global static like this
static int foo = 42;
you would capture a reference to it like this:[&foo](QTableWidgetItem* item)
-
below is the code I came up with. @jsulm the static factor is declared in a groupe box in the first tab and is copied in another groupe box in the second tab where the table is so the user can know what factor he s using without going back and forth, the factor is not a global static one. The calculation is done but with a wrong result: colulmn 5 = colulmn 3 without the additional factor * column 4. I am pretty sure there is a mistake in the declaration.
Cheers;
Raouf
static double AVF = ui->Input_PropAVF->text().toDouble();
ui->label_PropAVF1->setText(QString::number(AVF));connect(ui->m_ptable_ConvDesign, &QTableWidget::itemChanged, ui->m_ptable_ConvDesign, [&AVF](QTableWidgetItem* item) { if (item->column() == 2 || item->column() == 3) { auto CleanVol_item = item->tableWidget()->item(item->row(), 2); auto PropVol_item = item->tableWidget()->item(item->row(), 3); auto SlurryVol_item = item->tableWidget()->item(item->row(), 4); double val1 = CleanVol_item->data(Qt::DisplayRole).toDouble(); double val2 = PropVol_item->data(Qt::DisplayRole).toDouble(); SlurryVol_item->setData(Qt::DisplayRole, (AVF * val2) + val1); } });
-
Aww, that doesn't look right.
Why do you use a text input widget when you want a number? Using text to store a double is just weird. Better use something like QDoubleSpinBox and then you get the double value directly.
You don't need a static variable to pass it to that lambda. I only gave that example because you said you have a static input, which you don't really seem to.
Just capturethis
by reference and use the value directly:[&](QTableWidgetItem* item) { ... SlurryVol_item->setData(Qt::DisplayRole, (ui->Input_PropAVF->text().toDouble() * val2) + val1); }
If you used a proper QDoubleSpinBox it would be like this:
[&](QTableWidgetItem* item) { ... SlurryVol_item->setData(Qt::DisplayRole, (ui->Input_PropAVF->value() * val2) + val1); }
-
@Chris-Kawa Wow, this is a kids game for you isn't it. I regret I didn t study this when I had the chance.
Thanks man
-
@Chris-Kawa , hello man;
I have a new challenge, hope this is the last.
The next operation I need to do is calculating the items of a given column, column 4, of a given table, m_ptable_Conductor, from a column 4 of another table, m_ptable_ConvDesign, using a value storedd in QLineEdit; as follows:
SlurryVolCond_item->setData(Qt::DisplayRole, qRound(val1 * (ui->Input_DesSLF-> text().toDouble())));The results will populate the column of table 2 upon click on a push button.
I have prepared the below code, there's no error but nothing is changing in table 2.
void CFAdvisor::on_Button4_clicked() { connect(ui->m_ptable_Conductor, &QTableWidget::itemChanged, ui->m_ptable_ConvDesign, [&](QTableWidgetItem* item) { if (item->column() == 4) { auto SlurryVolConv_item = ui->m_ptable_ConvDesign->item(item->row(), 4); auto SlurryVolCond_item = item->tableWidget()->item(item->row(), 4); double val1 = SlurryVolConv_item->data(Qt::DisplayRole).toDouble(); SlurryVolCond_item->setData(Qt::DisplayRole, qRound(val1 * (ui->Input_DesSLF-> text().toDouble()))); } }); }
Thanks
-
Hi
Put a breakpoint at
if (item->column() == 4)
and single step from there. You can then see what values u take etc. -
@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
-
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.
-
@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.
-
@Abderaouf
It seems correct. You are getting a QVariant from data and call toString.
So when debugging it crash on this line? -
@Abderaouf said in QTableWidget Operations over columns:
I dont know what is the real reason though
probably
stagepulse_item
is a null pointeralso 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
-
@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.
-
@Abderaouf
as @VRonin asks,
could stagepulse_item be NULL ? -
@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?? -
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. -
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;
-
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.
Cheers
-
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