简体   繁体   中英

Custom action plugin for Qt Designer

I plan to use a spacer in my toolbar (below) between the 'Pause' and 'Settings' buttons to make the 'Settings' button right-aligned within the QToolBar . The separator below isn't cutting it.

工具栏需要垫片

The following method apparently works, although I haven't actually tried it yet:

QWidget* spacer = new QWidget();
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// toolBar is a pointer to an existing toolbar
toolBar->addWidget(spacer);
toolBar->addAction("Right-aligned button");

(thanks to http://www.ffuts.org/blog/right-aligning-a-button-in-a-qtoolbar/ )

Qt Designer doesn't seem to allow adding widgets to a QToolBar , although QToolBar does have an addWidget() method, as used above. So if I want to be able to add the spacer to my toolbar using Qt Designer (with a plugin, not promotion), should I subclass QAction , QWidget , or QWidgetAction ? Is there even a way to write custom action plugins for Qt Designer?

You cannot subclass QAction and expect to use it gracefully in Qt Creator/Designer. It uses the tags in the ui and to add them to menus and toolbars. You can't specify a class="customclass" element along with a entry like other widgets. Because actions are treated differently, you cant even subclass them so that's out.

As for QWidgetAction/QWidget, you are in the same boat as QActionGroup, if you know what I mean. (I personally think this is a big overlook in the design of the form designer!) So previewing that in the designer is also out.

The last thing you could do (invisible option 3) is QToolbar subclassing, but alas you can subclass it, but still, you wont be seeing anything in the desinger other than what a plain old QToolbar can do. That doesnt mean that it is completely out which is the point I'm coming to:

The real answer would have to be invisible option 4, subclassing QToolbar as a designer plugin (which would require a lot of coding on your part if you dont already know of one that is out there, I dont know of any that actually have designer plug-ins) Form Designer plugins are more not than often, but you could find someone's subclassed toolbar, and make it into a plugin yourself saving you a step (qt creator even has a template for that), then use it in qt designer and your set.

I know this question got asked 2 years ago but when I see an unanswered question, I think of those searchers that might read and get frustrated by a dead-end so if I can contribute I do... I hope I did at least help you or anyone else get on the right track, and good luck to you all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM