how to write integer to QTableWidget
-
Hello,
have a new problem.I get some numbers from a QTableWidget.Then I do some mathematics with these and want to write the result to a second QTableWidget.
Programm does the mathematic but when its up to writing to the second Widget I get a segmentation fault```
double mbar1=0;
double druck1=0;
QString drehz,druck;void MainWindow::on_sparkadv_clicked()
{
int r=m_ui->logfilewidget->rowCount();
for(int i = 1; i <12; ++i)
{ m_ui->average->insertRow(i);}
for (int i = 1; i <r; ++i)
{drehz = m_ui->logfilewidget->item(i,1)->text(); druck = m_ui->logfilewidget->item(i,6)->text(); if (drehz<1000) { druck1 =druck1 + druck.toDouble();} } int durchschnitt1=druck1/r; m_ui->average->item(5,5)->setText(QString::number(durchschnitt1).toStdString().c_str());}
inline void QTableWidgetItem::setText(const QString &atext)
{ setData(Qt::DisplayRole, atext); }Thank you for your help! Leopold -
sorry some text is missing:
here it stops in the qtablewidget.hinline void QTableWidgetItem::setText(const QString &atext) { setData(Qt::DisplayRole, atext); } -
Hi,
If there's a segmentation fault then you are accessing something that does not exists. You can use the debugger to find out what is going on.
I would guess that you are accessing a non existing item.
-
Hi
do note that ->item(row,col) can return a NULL if no item is assigned and that is
instant crash.You do
for(int i = 1; i <12; ++i)
m_ui->average->insertRow(i);}But i don't see you assign any QTableWidgetItem and as far as i know insertRow
inserts empty row so
m_ui->average->item(5,5) might return NULL; -
Hello,
have a new problem.I get some numbers from a QTableWidget.Then I do some mathematics with these and want to write the result to a second QTableWidget.
Programm does the mathematic but when its up to writing to the second Widget I get a segmentation fault```
double mbar1=0;
double druck1=0;
QString drehz,druck;void MainWindow::on_sparkadv_clicked()
{
int r=m_ui->logfilewidget->rowCount();
for(int i = 1; i <12; ++i)
{ m_ui->average->insertRow(i);}
for (int i = 1; i <r; ++i)
{drehz = m_ui->logfilewidget->item(i,1)->text(); druck = m_ui->logfilewidget->item(i,6)->text(); if (drehz<1000) { druck1 =druck1 + druck.toDouble();} } int durchschnitt1=druck1/r; m_ui->average->item(5,5)->setText(QString::number(durchschnitt1).toStdString().c_str());}
inline void QTableWidgetItem::setText(const QString &atext)
{ setData(Qt::DisplayRole, atext); }Thank you for your help! Leopold@Leopold said in how to write integer to QTableWidget:
@mrjj is fully right with the
nullptr. By the way:m_ui->average->item(5,5)->setText(QString::number(durchschnitt1).toStdString().c_str());
can be more simply written as:
m_ui->average->item(5,5)->setText(QString::number(durchschnitt1)); -
Hi mrjj,
I had extra inserted this loop, because i thaught .possibly it will not recognice the rows.No help after deleting that row.@ SGaist and aha_1980
yes debuging should resolve that problem.But Iam not used to debugging.I know already that inserting stops in line 188 from qtablewidget.h , thats why I attached the code where it stops. I do not know if "setData(Qt::DisplayRole, atext); " corresponds with
"setText(QString::number(durchschnitt1).toStdString().c_str()"
Debugger says "Wert" of atext = "0" -
@Leopold said in how to write integer to QTableWidget:
@mrjj is fully right with the
nullptr. By the way:m_ui->average->item(5,5)->setText(QString::number(durchschnitt1).toStdString().c_str());
can be more simply written as:
m_ui->average->item(5,5)->setText(QString::number(durchschnitt1)); -
But Iam not used to debugging.
Then learn it. Take the 15 minutes for this video, for example: https://www.youtube.com/watch?v=B7UsWtyhXh4
that was one of my first tries, always segmentation fault.
If you use the debugger, it will probably tell you that you access a
nullptr.AS @mrjj said:
But i don't see you assign any QTableWidgetItem and as far as i know insertRow
inserts empty row so
m_ui->average->item(5,5) might return NULL;You need to insert a
QTableWidgetItemfirst.Regards
-
Additional note: QTableWidget::insertRow explicitly state:
Inserts an empty row into the table at row.The crucial word here is:empty. There's no item there. -
Thank you for the help, now its working, i have added a counter for the division and changed the write comand.
double mbar1=0; double druck1=0; QString drehz,druck; double durchschnitt1; int durchschnitt; double s=0; void MainWindow::on_sparkadv_clicked() { int r=m_ui->logfilewidget->rowCount(); for (int i = 1; i <r; ++i) { drehz = m_ui->logfilewidget->item(i,1)->text(); druck = m_ui->logfilewidget->item(i,6)->text(); if (drehz<1000) { druck1 = druck1 + druck.toDouble(); s=s+1; } } double durchschnitt1 = druck1 / s; int durchschnitt=durchschnitt1; m_ui->average->setItem(5,5,new QTableWidgetItem(QString::number(durchschnitt))); }