Give your cute (Qt) GUI some pop
Close your eyes… take a long breath in… and breathe out… clear up your mind… and think about your favorite application (it should be the one you are working on 😛 ). Keep your eyes closed and visualize all the graphical elements of the application: buttons, frames, panels, texts, scroll-bars, menus… how many do you count ? That’s a lot to think about, isn’t it? If you have scroll-bars, it probably means that there is not enough space to show all the widgets at once, and that you need extra space.
Now, put yourself in the shoes of someone who plays with your application for the first time… don’t you feel overwhelmed by the amount of visual information? Where to click ? What buttons are important ? Which ones are not ?
I’ll skip the pamphlet explaining why it’s a bad idea to have a heavy UI. You can open your eyes now and let me present you another way of showing useful widgets at the right time and hiding them when not needed: ctkPopupWidget
Popup to control a 2D view
Slicer3 with slice view controllers visible |
Slicer4 with slice view controllers as popups |
As its name indicates, ctkPopupWidget is:
- part of CTK (Apache 2 license), used in 3D Slicer
- a popup: with full control over where and how to popup
- a widget: respects all the properties of a Qt widget (e.g. parent/child relationship, background transparency)
Popup to control the range of a slider
QSpinBox* spinBox = new QSpinBox(parentWidget); // the spinbox is the parent of the popup, ctkPopupWidget* popup = new ctkPopupWidget(spinBox); // the popup is placed relative to the spinbox QHBoxLayout* popupLayout = new QHBoxLayout(popup); // populate the popup with a vertical QSlider: QSlider* popupSlider = new QSlider(Qt::Vertical, popup); // add here the signal/slot connection between the slider and the spinbox popupLayout->addWidget(popupSlider); // Control where to display the the popup relative to the parent popupSlider->setAlignment(Qt::Left | Qt::Top); // at the top left corner popupSlider->setHorizontalDirection( Qt::RightToLeft ); // open outside the parent popupSlider->setVerticalDirection(ctkBasePopupWidget::TopToBottom); // at the left of the spinbox sharing the top border // Control the animation popupSlider->setAnimationEffect(ctkBasePopupWidget::ScrollEffect); // could also be FadeEffect popupSlider->setOrientation(Qt::Horizontal); // how to animate, could be Qt::Vertical or Qt::Horizontal|Qt::Vertical popupSlider->setEasingCurve(QEasingCurve::OutQuart); // how to accelerate the animation, QEasingCurve::Type popupSlider->setEffectDuration(100); // how long in ms. // Control the behavior popupSlider->setAutoShow(true); // automatically open when the mouse is over the spinbox popupSlider->setAutoHide(true); // automatically hide when the mouse leaves the popup or the spinbox.
See below the result of the previous code, the slider popup opens on the left of the spinbox.
Popup in a custom QItemDelegate
Here is the complete API for ctkPopupWidget.
See below more popup tuning:
Background fade effect |
Elastic easing curve |
Note that you can also design the popup UI using Qt Designer. All you then need to do is:
QSpinBox* spinBox = new QSpinBox(parentWidget); ctkPopupWidget* popup = new ctkPopupWidget(spinBox); Ui_myPopup::setupUi(popup); // sets the child widgets and the popup properties.
A popup widget follows its parent when moved, hides if the parent is hidden or disabled. Popups can be nested, pinned, unpinned, flattened (dynamically change from a popup mode into a regular widget).
Pin and Unpin
As a side note, you can also consider ctkCollapsibleButton and ctkCollapsibleGroupBox as other alternatives to conveniently “show”/”hide” widgets.
Any question? let me know.
Very cool. I think I’m gonna have to use CTK to build an app soon…
Hi,
I did not find any clear docs How can I use it in my QT application.
Can you please refer me to something that will help me with that?
There are integration examples in the CTK Examples github project:
https://github.com/commontk/Examples
The main idea to integrate CTK into an application is to do:
find_package(CTK)
include(${CTK_USE_FILE})
For more information, please ask on the CTK mailing list:
http://public.kitware.com/cgi-bin/mailman/listinfo/ctk-developers
i am a student from china, i need a resource code of CTK ,so ,if you have,please send to email 947540499@qq.com, thank you very much, american friends!
Hi ChenStar,
You can find the code of CTK here: https://github.com/commontk/CTK
Best regards,
Julien.
Hello,
I would like to use CTK with Qt 5.4.1 … I modified the CmakeList.txt and ctkMacroSetupQt.cmake : CMake configure and generaiton ok, but the Visual Studio 2013 Community solution failed ….
Is CTK compatible with Qt 5 ?
Thanks,
Best regards,
Hi Grégory,
CTK should be compatible with Qt5 (I currently use it with Qt 5.6.2). If you have some issue with it, please report them here: https://github.com/commontk/CTK/issues
Please note that there are some current merge requests that may fix your problem:
https://github.com/commontk/CTK/pulls
Best regards,
Julien.
How exactly to link Qt form ui with this popup. In Qt creator, Ui_myPopup::setupUi(popup); line of code is throwing error. QWidget or QDialog cannot be initialized with type ctkPopupWidget
Thank you for your interest.
Please ask technical questions on the CTK github site: https://github.com/commontk/CTK/issues