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. List placeholder
Forum Updated to NodeBB v4.3 + New Features

List placeholder

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 246 Views 2 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.
  • E Offline
    E Offline
    equeim
    wrote on last edited by
    #1

    So, I wanted to implemented a placeholder text for a list (QListView) which would be displayed if the list has no items.

    Currently I'm doing it using QStackedLayout but it requires quite a bit of boilerplate. You need to create a wrapper QWidget that will hold QStackedLayout, and also another wrapper QWidget for placeholder text so it can be centered:

            auto stackedLayoutWrapper = new QWidget(parent);
            auto stackedLayout = new QStackedLayout(stackedLayoutWrapper);
    
            stackedLayout->addWidget(listView);
    
            auto placeholderWrapper = new QWidget();
            auto placeholderLayout = new QVBoxLayout(placeholderWrapper);
            auto placeholder = new QLabel("No items");
            placeholderLayout->addItem(placeholder, 0, Qt::AlignCenter);
            stackedLayout->addWidget(placeholderWrapper);
    

    And then I discovered another neat way to do it, using QListView::viewport():

            auto placeholderLayout = new QVBoxLayout(listView->viewport());
            auto placeholder = new QLabel("No items");
            placeholderLayout->addWidget(placeholder, 0, Qt::AlignCenter);
    

    This works right now, but question is, how safe is it set a layout on the viewport() like this?

    JonBJ 1 Reply Last reply
    0
    • E equeim

      So, I wanted to implemented a placeholder text for a list (QListView) which would be displayed if the list has no items.

      Currently I'm doing it using QStackedLayout but it requires quite a bit of boilerplate. You need to create a wrapper QWidget that will hold QStackedLayout, and also another wrapper QWidget for placeholder text so it can be centered:

              auto stackedLayoutWrapper = new QWidget(parent);
              auto stackedLayout = new QStackedLayout(stackedLayoutWrapper);
      
              stackedLayout->addWidget(listView);
      
              auto placeholderWrapper = new QWidget();
              auto placeholderLayout = new QVBoxLayout(placeholderWrapper);
              auto placeholder = new QLabel("No items");
              placeholderLayout->addItem(placeholder, 0, Qt::AlignCenter);
              stackedLayout->addWidget(placeholderWrapper);
      

      And then I discovered another neat way to do it, using QListView::viewport():

              auto placeholderLayout = new QVBoxLayout(listView->viewport());
              auto placeholder = new QLabel("No items");
              placeholderLayout->addWidget(placeholder, 0, Qt::AlignCenter);
      

      This works right now, but question is, how safe is it set a layout on the viewport() like this?

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

      @equeim
      Would QStackedWidget instead of QStackedLayout reduce how many things you have to create for the first approach?

      If your simpler viewport() code works I cannot see why you should not use it.

      E 1 Reply Last reply
      0
      • JonBJ JonB

        @equeim
        Would QStackedWidget instead of QStackedLayout reduce how many things you have to create for the first approach?

        If your simpler viewport() code works I cannot see why you should not use it.

        E Offline
        E Offline
        equeim
        wrote on last edited by
        #3

        @JonB Right, I forgot that QStackedWidgets exists too.

        My problem with viewport() is that it feels like an implementation detail of QListView, so I'm afraid to break in some edge cases or on different platforms.

        Chris KawaC 1 Reply Last reply
        0
        • E equeim

          @JonB Right, I forgot that QStackedWidgets exists too.

          My problem with viewport() is that it feels like an implementation detail of QListView, so I'm afraid to break in some edge cases or on different platforms.

          Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @equeim viewport() is a documented API, so it's not an implementation detail, but you're doing a lot of work that is not really needed.
          You can simply subclass the listview and draw the text there, e.g.

          void MyListView::paintEvent(QPaintEvent* evt)
          {
              QListView::paintEvent(evt);
          
              if (!model() || model()->rowCount() == 0)
              {
                  QPainter p(viewport());
                  p.drawText(rect(), Qt::AlignCenter, "No items");
              }
          }
          
          1 Reply Last reply
          4
          • jeniferzaraJ Offline
            jeniferzaraJ Offline
            jeniferzara
            Banned
            wrote on last edited by
            #5
            This post is deleted!
            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