QList::at(int i) Sigsegv crashes by segmentation fault
-
im programming a simple database program using qsqlite i want to bring checkbox controls dynamically for selecteing or unselecting records i hava a like QList<QCheckBox *> select
when i add checkbox to tablewidget it crashes on line select.at(i).
i debugged when i is zero it works quite well but when i inreases to 1 it crashes.i hope for your helps.
-
Hi and welcome to devnet,
In addtion to what whoami asked, are you sure that you have a valid pointer in your QList at that position ?
-
in mainwindow.h
@
private:
QList<QCheckBox*> sec;
@@QWidget *pwidget= new QWidget();
QHBoxLayout *plyout=new QHBoxLayout(pwidget);
QCheckBox *qchk=new QCheckBox("CheckBox"+QString::number(i));
qchk->setText("");
sec.append(qchk);
plyout->addWidget(sec.at(sec.last()));
plyout->setContentsMargins(0,0,0,0);
plyout->setAlignment(Qt::AlignCenter);
pwidget->setLayout(plyout);ui->tableWidget->setCellWidget(i,(alanlar.split(";")).count()-1, pwidget); ui->tableWidget->setColumnWidth(3,50); qchk->setText("");@
this code is from a function which this works well but that gives error when if satir increases to 1
@
void MainWindow::on_ara_clicked() {qDebug()<<"search clicked";
qDebug()<<sqlquery;
QSqlQuery q=dbq.exec(sqlquery);
ui->aramasonuclari->clearContents();
ui->aramasonuclari->setRowCount(0);
ui->aramasonuclari->setColumnCount(0);
foreach (QString s, alanlar.split(";")) {
ui->aramasonuclari->insertColumn(ui->aramasonuclari->columnCount());
}
ui->aramasonuclari->setHorizontalHeaderLabels(alanlar.split(";"));
int satir=0;
while (q.next()) {
ui->aramasonuclari->insertRow(ui->aramasonuclari->rowCount());
qDebug()<<"row inserted";
for(int i=0;i<(alanlar.split(";")).count()-1;i++) {
ui->aramasonuclari->setItem(satir,i, new QTableWidgetItem(q.value(i).toString()));
qDebug()<<"tablewidget content inserted "+QString::number(satir)+"-"+QString::number(i);
}QWidget *pwidget= new QWidget(); QHBoxLayout *plyout=new QHBoxLayout(pwidget); qDebug()<<"counts of Checkbox in sec:"+QString::number(sec.count()); plyout->addWidget(sec.at(satir)); plyout->setContentsMargins(0,0,0,0); plyout->setAlignment(Qt::AlignCenter); pwidget->setLayout(plyout); ui->aramasonuclari->setCellWidget(satir,(alanlar.split(";")).count()-1,pwidget); satir++; }
}
@
-
Might I suggest another solution entirely?
Would it not be much easier and cleaner to use a QTableView instead of a QTableWidget for your data? You can then simply use a QSqlTableModel or a QSqlQueryModel to place your data in your view.
If you want to add a checkbox to select records, may I suggest my own "CheckableProxyModel":/wiki/QSortFilterProxyModel_subclass_to_add_a_checkbox ? That will make it almost trivial to add the requested checkbox, without you having to manage a load of QCheckBox widgets (very inefficient for larger data sets).
-
thanks andre for your reply but iwant to solve the problem in this code,
im new at qt and this is amy first program and it is a tutorial for me,
so i must go on on my way,can anybody help me on my solution. why it doesnt works when satir increases to 1, i think pointers value had been changed already when i call sec.at(satir) so there is not a checkbox
so what must i do if i dont want to change in the pointer sec.at(i) during it runs. -
I only see one QCheckBox instantiation and not in on_ara_clicked so my guess is that you are not allocating the your checkbox correctly
-
So... What are the contents your sec at the moment of the crash? I see that something is appended to it in the top snippet, but it is unclear if that code is ran just once or multiple times. If it is only ran once, then sec will only contain a single item, so your crash is logical: there is no sec.at(1).
-
i found error but dont know how to solve it
@QSqlDatabase dbq;@
in my mainwindow.h and i use it on a function which opens database and writes contents to table widget. that works quiet well
heres code
@
void MainWindow::on_veritabaniac_clicked() {
ui->tableWidget->setShowGrid(true);
dbq=QSqlDatabase::addDatabase("QSQLITE");
if (QFile::exists("test.devel.db")) {dbq.setDatabaseName("test.devel.db"); if (dbq.open()) { veritabaniacik=true; QSqlQuery cnames=dbq.exec("PRAGMA table_info(main)"); while (cnames.next()) { alanlar.append(cnames.value(1).toString()); alanlar.append(";"); ui->tableWidget->insertColumn(ui->tableWidget->columnCount()); qDebug()<<"alanlar oluşturuldu"; } ui->tableWidget->insertColumn(ui->tableWidget->columnCount()); alanlar.append("Seç"); int i=0; ui->tableWidget->setHorizontalHeaderLabels(alanlar.split(";")); QSqlQuery result=dbq.exec("SELECT * FROM main"); while (result.next()) { ui->tableWidget->insertRow(ui->tableWidget->rowCount()); for(int s=0;s<alanlar.count()-1;s++) { ui->tableWidget->setItem(i,s, new QTableWidgetItem(result.value(s).toString())); } ….
}
@yes it works but when i run a similar function like this
result.size() is -1 it has no records@
it=sec.begin(); ui->tableWidget->setRowCount(0); ui->tableWidget->setColumnCount(0); qDebug()<<QString::number(alanlar.split(";").count()); for (int alsay=0;alsay<alanlar.split(";").count();alsay++) ui->tableWidget->insertColumn(ui->tableWidget->columnCount()); int i=0; ui->tableWidget->setHorizontalHeaderLabels(alanlar.split(";")); QSqlQuery rslt=dbq.exec("SELECT * FROM main"); qDebug()<<QString::number(rslt.size()); //it gives -1******* while (rslt.next()) { ui->tableWidget->insertRow(ui->tableWidget->rowCount());, for(int s=0;s<(alanlar.split(";").count());s++) { ui->tableWidget->setItem(i,s, new QTableWidgetItem(rslt.value(s).toString())); }
….
i++;
it++;
}
}
@and i must say i have changed my QList<QCheckBox*> sec;
to QLinkedList<QCheckBox*> sec; and its iterator is
QLinkedList<QCheckBox*>::iterator it;
i hope your helpsEdit: I have cleaned up the thread a bit by removing posts about formatting and attempts to post a re-formatted version of the code here; Andre