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: Application crashes when removing tableWidget
Forum Updated to NodeBB v4.3 + New Features

SOLVED: Application crashes when removing tableWidget

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.3k 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 issues related to tableWidgets. Now my app crashes whenever I try to delete a table from it's parent widget. Below is the code for MainWindow.cpp, addButton and removeButton connect to the two functions. So could you please show me why the application crashes when removeButton is clicked?

    MainWindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QTableWidget>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    connect(ui->addButton, SIGNAL(clicked()), this, SLOT(add_table()));
    connect(ui->removeButton, SIGNAL(clicked()), this, SLOT(remove_table()));
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::add_table()
    {
    //Create table
    QTableWidget *tableWidget = new QTableWidget(ui->widget);

    tableWidget->setRowCount(1);
    tableWidget->setColumnCount(1);
    
    QTableWidgetItem *item = new QTableWidgetItem("hello");
    tableWidget->setItem(0, 0, item);
    
    tableWidget->show();
    

    }

    void MainWindow::remove_table()
    {
    //Remove table by removing all children
    QList<QWidget *> Widgets = ui->widget->findChildren<QWidget *>();
    foreach(QWidget * child, Widgets)
    {
    delete child;
    }
    }
    @

    Web/Desktop Developer

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2
      1. call delete directly is not a good, give a try to QObject::deleteLater()

      2. Change you code to

      @
      ui->widget->findChildren<QWidget *>(QString(), Qt::FindDirectChildOnly)
      @

      if they have sub children.

      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