PDA

View Full Version : Custom button



Erik-Moscow
1st February 2010, 14:46
Hi!

There is a question to create button (or action for QToolBar) like this:
http://omploader.org/vM2V4cw
Here is two states: checked and unchecked + menu or other additional action.

Any ideas?
PS: sorry, if there exists topic like this...

Coises
1st February 2010, 15:59
Use QToolButton::setPopupMode (http://doc.trolltech.com/latest/qtoolbutton.html#popupMode-prop)(QToolButton::MenuButtonPopup (http://doc.trolltech.com/latest/qtoolbutton.html#ToolButtonPopupMode-enum)) to get the menu drop down, and QAbstractButton::setCheckable (http://doc.trolltech.com/latest/qabstractbutton.html#checkable-prop)(true) (inherited by QToolButton) to make the button checkable. Then supply both "On" and "Off" images for the QIcon you set on the button.

Erik-Moscow
1st February 2010, 16:08
Yes, I created this: http://paste.org.ru/?nw5bfs But don't know how to create action instead menu :(
(really, it's needed to show widget insted menu)

Coises
1st February 2010, 16:37
(really, it's needed to show widget insted menu)

Perhaps it would work to create a QWidgetAction, then use QMenu::addAction(QAction*) to add it as the single item in a menu, then set that as the menu for the QToolButton.

Erik-Moscow
1st February 2010, 16:58
Thanks a lot! Works great! =)
Working code:

#include <QApplication>
#include <QtGui>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QSlider *sl = new QSlider(Qt::Horizontal);
sl->setRange(10, 100);
sl->setFixedWidth(200);
sl->setTickPosition(QSlider::TicksBelow);

QWidgetAction *wa = new QWidgetAction(0);
wa->setDefaultWidget(sl);

QMenu *menu = new QMenu;
menu->addAction(wa);

QToolButton btn;
btn.setText("Checkable");
btn.setMenu(menu);
btn.setPopupMode(QToolButton::MenuButtonPopup);
btn.setCheckable(true);

btn.show();

return app.exec();
}