PDA

View Full Version : How to make and use custom UI widgets for a list



Estefunny
22nd May 2014, 09:02
Hi all,

I’m doing a apprenticeship to a Software Developer, so my Know-How is pretty low (also my english, sorry for any mistakes)

I’m currently working on a book libary and i used a QTableWidget to show all books (including image, title, author and a short discription). At this point i fill in every single cell with an QTableWidgetItem, to show a single Information.

Now my instruktor wants me, to write a single UI class that represents a book with all given information, which then I can insert my Table/List. But here comes my problem: I can’t find a good tutorial or even the right keywords.

What I know so far: I think I need to write the UI class (name it BookView) thats need to be a QWidget. Then I could insert this BookView in my Table/List. But I dont have the knowledge, Maybe I’m searching with the wrong keywords.

I'm using Qt 5 with C++.

Any help will be appreciated.

d_stranz
23rd May 2014, 01:25
You need to derive your BookView from QWidget. Use Qt Designer to design the widget - that's easiest. You will probably want to start with a grid or form layout, and add QLabel items that will hold the book image, title, and other strings.

At run time, when you read your list of books, you'll create a BookView widget instance for each book, and set the image on the BookView instance's imageLabel, the title string as the text on titleLabel, and so on. You can then install that in the table widget using QTableWidget::setCellWidget() method. After you are done inserting everything, you'll probably want to call resizeRowsToContents() and resisizeColumnsToContent() on the table widget.