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. When a QListWidgetItem is removed, how can I prevent the attached QWidget from being destructed?
Forum Updated to NodeBB v4.3 + New Features

When a QListWidgetItem is removed, how can I prevent the attached QWidget from being destructed?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 269 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.
  • K Offline
    K Offline
    kuqipair
    wrote on last edited by
    #1

    I want to attach a custom QWidget to a QListWidget, but when I need to clear the QListWidget, I don't want the QWidget to be destructed because I need to reuse the QWidget."

    JonBJ 1 Reply Last reply
    0
    • K kuqipair

      I want to attach a custom QWidget to a QListWidget, but when I need to clear the QListWidget, I don't want the QWidget to be destructed because I need to reuse the QWidget."

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @kuqipair said in When a QListWidgetItem is removed, how can I prevent the attached QWidget from being destructed?:

      I want to attach a custom QWidget to a QListWidget

      And what call do you make for that? void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget)?

      Note: The list takes ownership of the widget.

      So assuming that if you delete the QListWidgetItem it will take your widget with it. To prevent that you must either remove it from the QListWidgetItem first (presumably setItemWidget(nullptr)?) or you could use QListWidget::takeItem() which hands ownership of the whole QListWidgetItem (with its content) over to you.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kuqipair
        wrote on last edited by
        #3

        Actually, what I want to achieve is the ability to switch between multiple different lists within the same QListWidget. Each QListWidgetItem is a custom QWidget, and I don't want to call delete every time I switch to a different view. I found that no matter what I do, the QWidget always gets destructed.

        Pl45m4P JonBJ 2 Replies Last reply
        0
        • K kuqipair

          Actually, what I want to achieve is the ability to switch between multiple different lists within the same QListWidget. Each QListWidgetItem is a custom QWidget, and I don't want to call delete every time I switch to a different view. I found that no matter what I do, the QWidget always gets destructed.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @kuqipair

          BTW: Are you aware that adding a QWidget to, say, QListWidget is kinda costly...
          It's recommended to use QListWidgetItem or write your own delegate.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1
          • K kuqipair

            Actually, what I want to achieve is the ability to switch between multiple different lists within the same QListWidget. Each QListWidgetItem is a custom QWidget, and I don't want to call delete every time I switch to a different view. I found that no matter what I do, the QWidget always gets destructed.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @kuqipair
            As @Pl45m4 says adding widgets to QListWidget is costly and to be avoided for preference.

            the ability to switch between multiple different lists within the same QListWidget

            This sounds like a reason not to use a QListWidget, which has its own internal data model. Rather a QListView where you can swatch underlying model at will.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved