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. How to make and use custom UI widgets for a list
Forum Updated to NodeBB v4.3 + New Features

How to make and use custom UI widgets for a list

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

    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.

    EDIT: I'm using Qt 5 with C++.

    Any help will be appreciated.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        Hi,
        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");
        m_MyBooks_lst.append(NewBook_ps);

        // Then trigger your view to update when using Model/View (advanced mode) or simply add the Widget to your ListWidget:
        ui->MyBookView.addWidget(NewBook_ps);
        @
        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!

        Greetz, Jeroen

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Estefunny
          wrote on last edited by
          #4

          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:

          For example:
          @
          BookView * NewBook_pc = new BookView();
          @

          instead of

          @
          QWidget * NewBook_pc = new BookView();
          @

          And thank you both for helping me :)

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            Hi,
            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...
            m_MyBooks_lst.append(NewBook_pc);
            ui->MyTreeView.insertItem(dynamic_cast<QWidget*>(NewBook_pc)); // Don't know if the treeview uses the insertItem, or append, addItem etc.
            @

            Greetz, Jeroen

            1 Reply Last reply
            0
            • E Offline
              E Offline
              Estefunny
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Estefunny
                wrote on last edited by
                #7

                @Jeroentje@home:

                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?

                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