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

QListWidget - Center icon

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 7.0k 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.
  • P Offline
    P Offline
    pixbyte
    wrote on last edited by
    #1

    Hi,

    I use a QListWidget to navigate in a QDialog (Configuration). How I can set the icon and text inside a item horizontal center?
    I use for the QListwidget:

    @contentsWidget = new QListWidget;
    contentsWidget->setViewMode(QListView::IconMode);
    contentsWidget->setIconSize(QSize(48, 48));
    contentsWidget->setMovement(QListView::Static);
    contentsWidget->setMaximumWidth(128);
    contentsWidget->setFlow(QListView::TopToBottom);@

    And with the item I use @setTextAlignment(Qt::AlignHCenter);@

    But the icon and text is always shown at the left hand border. What is the trick to show them centered?

    Ingo

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

      Hi pixbyte,

      If you want to align your icons horizontally, I'm not sure this is possible with a QListView (even harder with a QListWidget).

      An idea would be to create your own custom delegate, inheriting QItemWidget. If your want to create your own, you can have a look at the doLayout protected method of QItemDelegate.

      Another would be to "hide" your QListWidget in a container with the look you want, and resize/change the position of your QListWidget to obtain the wanted result (typically by centering it in your fake parent container). Have a look at "Qt style sheets":http://doc.qt.digia.com/qt/stylesheet.html then.

      Adeneo Embedded - www.adeneo-embedded.com

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

        I did that using my own delegate indeed. I reimplemented sizeHint and paint to get it to work. I simply did my own rendering of the item, making centering trivial.

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

          True, that's an interesting/simple option. The only drawback is loosing the original look of items/icons.

          Adeneo Embedded - www.adeneo-embedded.com

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

            There is no need to loose that look. I still have the nice glass-like selection and highlight working for my implementation, using a very simple trick. To render the highlight, I created a QStringListModel with a single item of an empty string. Now, in my paint method, I then call the base class implementation (QStyledItemDelegate) with the index of this empty item. That takes care of properly rendering the highlight.

            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