Mohamed Anwer
Mohamed Anwer

Reputation: 182

Qt - How to draw a tiled pixmap?

I want to draw rating stars in QTreeWidgetItem Like this: Final shape

How to do this, taking in consideration that I want to do this without using any images, only using QPixmap and QPolygon.

Don't care about shadows, just want to draw the shape of multiple stars using one QPixmap holds one star.

Here is the final shape:

enter image description here

Upvotes: 0

Views: 853

Answers (1)

Jeremy Friesner
Jeremy Friesner

Reputation: 73081

Since you say in the comments you already know how to draw a single star, drawing multiple stars is straightforward: create a QPixmap that is 5 times wider than a single star, and execute your draw-a-star routine multiple times in a loop, drawing that star into the pixmap at different x-offsets as necessary.

To display the stars in your tree widget:

Create an array containing five different QPixmaps, all of the same size: the first with a single star and four blank spaces, the second with two stars and three blank spaces, and so on, with the last one being five stars and no blank spaces.

Then for each of your QTreeWidgetItems, call setIcon(0, pixmapArray[numStars-1]) to set the appropriate QPixmap for that item.

Note: You may also need to call setIconSize() on your QTreeWidget to make sure that your pixmaps are displayed at the size you want (and not e.g. auto-scaled down to fit into a smaller space)

Upvotes: 1

Related Questions