SOLVED: Table Widget crashes Application



  • Hello,
    I have been struggling with a table widget that crashes the application when I call any function that removes a row. The purpose of the table is to display users' photos and names with a checkbox which when checked removes them from the table. Please look through the code below and assist me. ROW is a global array that determines if a row or user is to be shown. FIRST_NAME, LAST_NAME and PHOTO are _QVarLengthArray_s that are accessed by the respective variables.

    @void MainWindow::display_table()
    {
    //Reset Table....................................................
    ui->tableWidget->setRowCount(0);
    ui->tableWidget->setColumnCount(0);

    //Number of Rows Needed............................
    int rows = 0;
    for(int a=0;a<10; a++)
    {
    if(ROW[a]==1){rows++;}
    }
    if(rows==0){
    return;
    }

    QString *accessFirst = FIRST_NAME.data();
    QString *accessLast = LAST_NAME.data();
    QString *accessPhoto = PHOTO.data();
    

    //Table Properties............................
    ui->tableWidget->setRowCount(rows);
    ui->tableWidget->setColumnCount(3);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setColumnWidth(0, 20);
    ui->tableWidget->setColumnWidth(1, 50);
    ui->tableWidget->setColumnWidth(2, 150);

    for(int r=0; r<10; r++)
    {
        if(ROW[r]==1)
        {
            ui->tableWidget->setRowHeight(r, 50);
    

    //CheckBox............................
    QWidget* wdgt = new QWidget;
    QHBoxLayout *layout = new QHBoxLayout(wdgt);

            if(r==0) layout->addWidget(&CheckBox0);
            else if(r==1) layout->addWidget(&CheckBox1);
            else if(r==2) layout->addWidget(&CheckBox2);
            else if(r==3) layout->addWidget(&CheckBox3);
            else if(r==4) layout->addWidget(&CheckBox4);
            else if(r==5) layout->addWidget(&CheckBox5);
            else if(r==6) layout->addWidget(&CheckBox6);
            else if(r==7) layout->addWidget(&CheckBox7);
            else if(r==8) layout->addWidget(&CheckBox8);
            else layout->addWidget(&CheckBox9);
    
            layout->setMargin(0);
            layout->setAlignment( Qt::AlignCenter );
            wdgt->setLayout(layout);
            ui->tableWidget->setCellWidget(r, 0, wdgt);
    

    //Photo............................
    QLabel *imageLabel = new QLabel;
    QPixmap *image = new QPixmap(accessPhoto[r]);
    QPixmap Image = image;
    imageLabel->setPixmap(Image.scaledToHeight(50));
    QWidget
    wdg = new QWidget;
    QHBoxLayout *Layout = new QHBoxLayout(wdg);
    Layout->addWidget(imageLabel);
    Layout->setMargin(0);
    Layout->setAlignment( Qt::AlignCenter );
    wdg->setLayout(Layout);
    ui->tableWidget->setCellWidget(r, 1, wdg);

    //Names............................
    QTableWidgetItem *names = new QTableWidgetItem(accessFirst[r]+" "+accessLast[r]);
    names->setTextAlignment(Qt::AlignCenter);
    ui->tableWidget->setItem(r, 2, names);
    }
    }
    }@

    After some checkboxes are checked, a function remove_checked() is called by a button
    @void MainWindow::remove_checked()
    {
    if(CheckBox0.isChecked()==true) ROW[0]=0;
    if(CheckBox1.isChecked()==true) ROW[1]=0;
    if(CheckBox2.isChecked()==true) ROW[2]=0;
    if(CheckBox3.isChecked()==true) ROW[3]=0;
    if(CheckBox4.isChecked()==true) ROW[4]=0;
    if(CheckBox5.isChecked()==true) ROW[5]=0;
    if(CheckBox6.isChecked()==true) ROW[6]=0;
    if(CheckBox7.isChecked()==true) ROW[7]=0;
    if(CheckBox8.isChecked()==true) ROW[8]=0;
    if(CheckBox9.isChecked()==true) ROW[9]=0;

    //Refresh Table.....................................
    display_table();
    }@

    The application crashes when a row is removed e.g. ui->tableWidget->removeRow(0);



  • What does your debugger tell you?



  • Everything builds fine, just the crashing after removing a row



  • I asked about your debugger, not your compiler


  • Moderators

    [quote author="Qkato" date="1375190230"]Everything builds fine, just the crashing after removing a row[/quote]
    thats the compiler then not the debugger ;)

    show us at least some code of the call were it crashes. The setup of the table seems ok so far on the first glance.



  • I guess I just have to opt for a tableWiget created at runtime


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.