Results 1 to 5 of 5

Thread: Implementing a butcon

  1. #1
    Join Date
    May 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Implementing a butcon

    We currently have a montage application on Windows which we want to have on Linux and Solaris as well. It has been decided that we are going to write the application from scratch using Qt. One of those "little" features that now have to be ported is a properties toolbar that allows the user to directly set things line text colour, line/edge colour, fill colour, line width and line type. If it is clicked a pane drops down from which the user can select the different kinds of line types. Is there a way to make something like this in Qt and if so what is the best approach to implement this? I assume that the solutions for the other butcons the solution is more or less the same.

    Thanks in advance for any hints
    Theo

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Implementing a butcon

    I think the best way is to implement a custom widget consisting of 2 tool buttons - one is the actual tool button, the other is the button that activates the menu.
    You will have a horizontal layout, in which you will put the 2 buttons. The drop down menu button should be fixed size and on click should remain toggled.

    You could add various look&feel functionality, depending on what you need.

    Regards

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Implementing a butcon

    No need for two separate buttons

    QToolButton::ToolButtonPopupMode:
    In this mode the tool button displays a special arrow to indicate that a menu is present. The menu is displayed when the arrow part of the button is pressed.
    A custom widget can be easily added to a QMenu by wrapping it into a QWidgetAction. An example follows:
    Qt Code:
    1. QToolButton* button = new QToolButton(this);
    2. button->setPopupMode(QToolButton::MenuButtonPopup);
    3. button->setIcon(QPixmap(":/trolltech/styles/commonstyle/images/viewdetailed-16.png"));
    4.  
    5. QMenu* menu = new QMenu(button);
    6. QCalendarWidget* calendar = new QCalendarWidget(menu);
    7. QWidgetAction* action = new QWidgetAction(menu);
    8. action->setDefaultWidget(calendar);
    9. menu->addAction(action);
    10. button->setMenu(menu);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    ntp (31st July 2007)

  5. #4
    Join Date
    May 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Implementing a butcon

    Thanks for the various hints.
    I ended up sub-classing QMenu and then implemented my own paintEvent():
    Qt Code:
    1. void
    2. JQLineTypeMenu::paintEvent(
    3. QPaintEvent* event
    4. )
    5. {
    6. QPainter p(this);
    7. QRegion emptyArea = QRegion(rect());
    8.  
    9. //draw the items that need updating..
    10. foreach ( QAction* action, actions() )
    11. {
    12. QRect adjustedActionRect = actionGeometry(action);
    13. if ( !event->rect().intersects(adjustedActionRect) )
    14. continue;
    15.  
    16. //set the clip region to be extra safe (and adjust for the scrollers)
    17. QRegion adjustedActionReg(adjustedActionRect);
    18. emptyArea -= adjustedActionReg;
    19. p.setClipRegion(adjustedActionReg);
    20.  
    21. initStyleOption(&opt, action);
    22. opt.rect = adjustedActionRect;
    23. drawMenuItem(&opt, &p, action);
    24. }
    25. ...
    26. }
    27. void
    28. JQLineTypeMenu::drawMenuItem(
    29. const QAction* action
    30. )
    31. {
    32. bool act = opt->state & QStyle::State_Selected;
    33. QBrush fill = opt->palette.brush(act ? QPalette::Highlight : QPalette::Button);
    34. p->fillRect(opt->rect, fill);
    35.  
    36. QPen linePen(static_cast<Qt::PenStyle>(action->data().toInt()));
    37. linePen.setWidth(3);
    38. linePen.setBrush(opt->palette.brush(act ? QPalette::HighlightedText : QPalette::ButtonText));
    39. p->setPen(linePen);
    40. p->drawLine(opt->rect.left(), opt->rect.center().y(), opt->rect.right(), opt->rect.center().y());
    41. }
    To copy to clipboard, switch view to plain text mode 
    The various actions in the menu held the different pen styles (using QAction::setData()). I then instanced my JQLineTypeMenu for the toolbar.
    Note: if you need also the scrollbar and/or tear-off support then it is better creating a custom QWidget and insert instances of those in the menu through QActionWidget.
    The whole Qt library is full of private data members that sometimes makes C++ deriving a big cut&paste exercise

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Implementing a butcon

    Quote Originally Posted by Theo Landman View Post
    The whole Qt library is full of private data members that sometimes makes C++ deriving a big cut&paste exercise
    That's unfortunate but (in most parts of the library) still essential for TT to retain binary compatibility. TT is always open for suggestions when it comes to improvements of any kind, like adding new methods to ease up a common (and sensible) task or so.
    J-P Nurmi

Similar Threads

  1. Implementing paint in a child of a parent
    By drarem in forum Newbie
    Replies: 1
    Last Post: 21st June 2007, 17:28
  2. thread implementing and stopping the thread
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2007, 14:53
  3. Implementing paint()
    By Wirloff in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2007, 11:29
  4. Implementing IME using QT
    By pmohod in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2007, 13:22
  5. Implementing Resize funtionality
    By PiXeL16 in forum Qt Programming
    Replies: 2
    Last Post: 15th February 2007, 00:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.