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. QListWidget crash when reusing the widget associated with item
Forum Updated to NodeBB v4.3 + New Features

QListWidget crash when reusing the widget associated with item

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 350 Views
  • 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.
  • D Offline
    D Offline
    developer-123 0
    wrote on last edited by
    #1

    I have a custom simple widget inheriting from QWidget and I add it to a QListWidget like this :

    void MainWindow::AddToWidgetList(const QString &tag, const QString &html)
    {
        HtmlItem *html_item = new HtmlItem();
        html_item->set_tag(tag);
        html_item->set_html(html);
        connect(html_item, SIGNAL(RemoveIt(uintptr_t)), this, SLOT(on_RmBtn_clicked(uintptr_t)));
        QListWidgetItem *list_item = new QListWidgetItem();
        html_item->set_list_item(list_item);
        list_item->setSizeHint(html_item->sizeHint());
        ui->CodeBlocks->addItem(list_item);
        ui->CodeBlocks->setItemWidget(list_item, html_item);
    }
    

    I then want to move the selected element up when a button is pressed

    void MainWindow::on_UpArrowBtn_clicked()
    {
        if (ui->CodeBlocks->count() < 2)
            return;
        int current_row = ui->CodeBlocks->currentRow();
        if (current_row == 0)
            return;
        HtmlItem *item_widget = (HtmlItem*)ui->CodeBlocks->itemWidget(ui->CodeBlocks->item(current_row));
        QListWidgetItem *item = ui->CodeBlocks->takeItem(current_row);
        ui->CodeBlocks->insertItem(current_row - 1, item);
        ui->CodeBlocks->setItemWidget(item, item_widget);
    }
    

    but I get crash in this line :

    ui->CodeBlocks->setItemWidget(item, item_widget);
    
    jsulmJ 1 Reply Last reply
    0
    • D developer-123 0

      I have a custom simple widget inheriting from QWidget and I add it to a QListWidget like this :

      void MainWindow::AddToWidgetList(const QString &tag, const QString &html)
      {
          HtmlItem *html_item = new HtmlItem();
          html_item->set_tag(tag);
          html_item->set_html(html);
          connect(html_item, SIGNAL(RemoveIt(uintptr_t)), this, SLOT(on_RmBtn_clicked(uintptr_t)));
          QListWidgetItem *list_item = new QListWidgetItem();
          html_item->set_list_item(list_item);
          list_item->setSizeHint(html_item->sizeHint());
          ui->CodeBlocks->addItem(list_item);
          ui->CodeBlocks->setItemWidget(list_item, html_item);
      }
      

      I then want to move the selected element up when a button is pressed

      void MainWindow::on_UpArrowBtn_clicked()
      {
          if (ui->CodeBlocks->count() < 2)
              return;
          int current_row = ui->CodeBlocks->currentRow();
          if (current_row == 0)
              return;
          HtmlItem *item_widget = (HtmlItem*)ui->CodeBlocks->itemWidget(ui->CodeBlocks->item(current_row));
          QListWidgetItem *item = ui->CodeBlocks->takeItem(current_row);
          ui->CodeBlocks->insertItem(current_row - 1, item);
          ui->CodeBlocks->setItemWidget(item, item_widget);
      }
      

      but I get crash in this line :

      ui->CodeBlocks->setItemWidget(item, item_widget);
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @developer-123-0 said in QListWidget crash when reusing the widget associated with item:

      but I get crash in this line

      what kind of crash is that? Did you already debug to see what happens?
      Is item_widget a valid pointer?
      After this line you do not check the pointer:

      HtmlItem *item_widget = (HtmlItem*)ui->CodeBlocks->itemWidget(ui->CodeBlocks->item(current_row));
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      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