How to make and use custom UI widgets for a list
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.
EDIT: I'm using Qt 5 with C++.
Any help will be appreciated.
I myself would opt to use QTreeWidget (or QTreeView) and for your "Book" class I would recommend to inherit from QTreeWidgetItem, with various pieces of information presented in different columns.
Or you can go full-awesome with QML, but that is a whole different story.
AND WELCOME TO QT!!! Always fun to have new people get to know the power of Qt!
Yes, making a inherited class of QWidget is probably the way to go. In here you store all information about the book into member variables and also make up the GUI. (might be done with Designer).
So for every book you will have a QWidget derived class. Put them in a QList to keep track of them and you're almost there. Ok, now you probably wondering how the get this done? Did you hear about base classes? In C++ when different classes have the same base class (a class that they derive from), all classes can be accessed by pointers to those base classes. So in this case, the QWidget pointer is your base class.
QList <QWidget *> m_MyBooks_lst; // for example in your MainWindow class
Then when a new book needs to be added in a slot in MainWindow?
QWidget * NewBook_pc = new BookView(); // Or add arguments with the book information
NewBook_ps->addTitle("Harry Potter - Phylosiphers stone");
NewBook_ps->addInformation("Read it, nice book about Wizards");
// Then trigger your view to update when using Model/View (advanced mode) or simply add the Widget to your ListWidget:
When using a ListWidget you do not need to keep track of the QList<Widget*> yourself. It will be kept in the ListWidget, so pointers to the book class can be obtained from there.
IYAM it's always good to keep track of it yourself in case you switch to Model/View based GUI.
Hope this helps a bit!
I will try the QtreeView anyway, since I need to learn as much as possible :)
I tried the way Jeroentje@home gave me before, but did some mistakes, I see now:
BookView * NewBook_pc = new BookView();
QWidget * NewBook_pc = new BookView();
And thank you both for helping me :)
Both ways are doable. What sierdzio mentioned is probably easier to understand, because you are familiar with the QTreeWidget / QTableWidget.
Hmm, you could also keep a list of BookView pointer in a list and add with a dynamic or static cast the widget pointer to the QTreeWidget.
BookView * NewBook_pc = new BookView();
// Add info...
ui->MyTreeView.insertItem(dynamic_cast<QWidget*>(NewBook_pc)); // Don't know if the treeview uses the insertItem, or append, addItem etc.
I've came accross some problems with further improvements, which the list should have:
Each book should have his own item (BookView) but is seperated into the given parts (title, author, etc.) But all "culumns" must have the same length (according to the longest title, author and so on)
The header, which needs to contain the labels title, author and so on, need to assign to the length of the relevant "cells".
This all have to be dynamically, because I want to hide some culumns at some time (for example hide the description of the book).
Yeah I know, some high requirements, and i don't need any solution, just tips.
When i tried to add a QWidget to the ListWidget, I only find addItem(QListWidgetItem* item. So I need to inherit from the QWidgetItem instead of QWWidget?