I am looking to create a digital Rolodex to keep track of contacts in Qt 5.9 and am uncertain how to efficiently create custom widgets. Examples online like the classic clock example show to completely override paint, but I want only to create a widget in design that is a collection of labels and buttons to display a list of contacts, and a few buttons to edit, delete, and create contacts beside it. The element entire arrangement appear multiple times across different parts of the application and it should look exactly the same in each.

enter image description here

I will have to create many other widgets just like this, so I am hoping to be able to drag-and-drop elements in designer rather than programmatically set everything.

Since each contact has its own little panel and picture, each contact is arranged in a scrolling list, and buttons are placed beside, we have:

  1. Create a ContactLabel custom widget with the picture, name, and address labels arranged
  2. Create a ContactsWidget custom widget with a Widget Listview containing a series of ContactLabel widgets, and buttons on the right.
  3. Wherever needed, drop the `contacts widget into a form.

It appears that visual studio addons and Qt Creator only want to create forms (which you can easily drop ui elements onto) or custom widgets, which only create code and do not automatically link in a ui file.

Is this an appropriate way to approach this problem and is there an easy way to drop in custom arrangements of controls?


You can "promote" a widget to your custom widget . The gist is you point the .ui file to a header file that defines your QWidget derived class, and the uic generated header uses that class.

However the visual look is that of the base widget (i.e. a blank frame for QWidget), and these are not added to the toolbox of widgets (you have to drop something else and promote it each time).

  • I guess that the question is about using the created widget as an item in a ListView. As I understand it, there is no functionality to tell ListView to use a widget as an item, only to override paint in a delegate. Sep 13 at 8:16

While I don't know how to do it in classic Qt Widgets, it is very widely used in QML. You supply a delegate Item (ContactLabel in your case) with all the labels and icons, and ListView (ContactsWidget) populates the itself with clones of the delegate. Reusing the created elements in other places is also quite possible.

UPDATE: Yes, you can do it with the classic Qt widgets. QListWidget allows you to set a Widget for each item with setItemWidget method. This way you create your widget in Qt Designer, instantiate it in code and set in into the QListWidget.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.