Results 1 to 5 of 5

Thread: How to create Android Style On/Off-Toggle switch in Qt/C++

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: How to create Android Style On/Off-Toggle switch in Qt/C++

    Alright, this is very, very basic and won't look that nice. But it might be a good starting point.

    Qt Code:
    1. AndroidSwitch::AndroidSwitch(QWidget* parent)
    2. : QAbstractButton(parent)
    3. {
    4. setCheckable(true);
    5. }
    6.  
    7. AndroidSwitch::paintEvent(QPaintEvent* event)
    8. {
    9. QPainter painter(this);
    10. painter.fillRect(rect(), Qt::gray);
    11. QRect switchRect;
    12. QColor switchColor;
    13. QString text;
    14. int halfWidth = width() / 2;
    15. if (isChecked()) {
    16. switchRect = QRect(halfWidth, 0, halfWidth, height());
    17. switchColor = QColor(Qt::cyan);
    18. text = "On";
    19. } else {
    20. switchRect = QRect(0, 0, halfWidth, height());
    21. switchColor = QColor(Qt::darkGray);
    22. text = "Off";
    23. }
    24. painter.fillRect(switchRect, switchColor);
    25. painter.drawText(switchRect, Qt::AlignCenter, text);
    26. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to ChiliPalmer for this useful post:

    rawfool (29th March 2013)

Similar Threads

  1. Replies: 3
    Last Post: 12th March 2013, 10:16
  2. Toggle dialog visibility on application switch
    By gvanvoor in forum Qt Programming
    Replies: 12
    Last Post: 20th June 2012, 11:11
  3. Replies: 2
    Last Post: 6th April 2010, 21:42
  4. Replies: 20
    Last Post: 22nd October 2009, 01:22
  5. How to toggle between 2 forms
    By safknw in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2006, 10:34

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.