0

I'm creating a personnel management software for my own use and basically I'd want to get my QListView to be prettier and more informative than by using QStandardItemModel and QStandardItem.

Essentially I'd like for the list to look something like this:

  Mr Smith sick
  Mrs Smith working
  Mr Anderson traveling
  Ms White working

Preferably also with an option of colors, but I couldn't find a quick tutorial for this.

I've understood that this could be accomplished by using QItemDelegate, but I tried to look at the documentation and it made my brain hurt. Could somebody inform how could I add rich-formatted text items to a QListView or point to a "for dummies" -tutorial? Thanks!

Joonas Joensuu
  • 102
  • 2
  • 10
  • 1
    Instead of using a QListView, you could use a QTableView with two or three columns: one for the icon, one for the name (or a single column for both) and the third for their status. That would be better for at least three reasons: 1. programmatically speaking, you're separating the *properties* of each entry; 2. visually speaking, the states will be properly vertically aligned (imagine employees having very short and very long names); 3. you can specify the font features for the specific state, without complicating things using rich text. – musicamante Mar 14 '23 at 15:32
  • I thought abot QListView due to the ease of selecting a single item from the list for editing/analyzing, and I have no clue how to do that with QTableView, as those entries will span over several cells. See mention of "for dummies" in the original post. – Joonas Joensuu Mar 14 '23 at 15:50
  • If you want to automatically select all cells in a row when clicking on an item of a table, use [`setSelectionBehavior(QAbstractItemView.SelectRows)`](//doc.qt.io/qt-5/qabstractitemview.html#selectionBehavior-prop). If I may, using a list view because you don't know how to use a table is not a good reason: from what you're explaining, it's clear that each record will probably have many fields, which is the *exact* reason for which you should *not* use a list. I know that the Qt model/view framework doesn't seem very friendly, but that's because it's extremely extensible (like anything in Qt). – musicamante Mar 14 '23 at 16:31
  • It's important to choose the tools that are correct for your purpose, not only based on your knowledge or experience: if you need to use tables and you don't know how to use them, don't try to make a list to behave "like" a table: learn about tables. Also, if you need to learn about a topic for your program, don't try to learn it by applying its concepts *directly* on your program: start with simple examples and learn from them; for instance, create a basic model with two columns. Then, take your time to read [the model/view documentation](//doc.qt.io/qt-5/model-view-programming.html) and -> – musicamante Mar 14 '23 at 16:31
  • -> then **everything** related to the classes you're using. For example, starting from QTableView -> QAbstractItemView -> QAbstractScrollArea -> QFrame -> QWidget -> QObject, but also QStandardItemModel -> QAbstractItemModel and QStyledItemDelegate -> QAbstractItemDelegate. Then, see some [PyQt resources](//wiki.python.org/moin/PyQt) (eg. the "Sample Code" section). Finally, a possible [rich text delegate](https://stackoverflow.com/q/1956542) implementation, but, seriously, don't use it for the wrong reason. – musicamante Mar 14 '23 at 16:31

1 Answers1

0

For my needs the answer was rather simple (as it usually is). Instead of using QListView directly, I used QListWidget, where I added a QListWidgetItem and to that I added my custom QWidget, where I could set fixed size constraints in a quick and simple manner thus eliminating the need to build custom models and delegates

With QLabel I can use rich text without problems.

Ie. what I asked for was not exactly what I needed. Yes, this is possibly not as an efficient solution as it could be, but for my needs it's more than sufficient.

Joonas Joensuu
  • 102
  • 2
  • 10