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. SOLVED: Table Widget crashes Application
Forum Updated to NodeBB v4.3 + New Features

SOLVED: Table Widget crashes Application

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.9k Views 1 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.
  • Q Offline
    Q Offline
    Qatto
    wrote on last edited by
    #1

    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);

    Web/Desktop Developer

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      What does your debugger tell you?

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qatto
        wrote on last edited by
        #3

        Everything builds fine, just the crashing after removing a row

        Web/Desktop Developer

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          I asked about your debugger, not your compiler

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            [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.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              Qatto
              wrote on last edited by
              #6

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

              Web/Desktop Developer

              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