Results 1 to 3 of 3

Thread: Adding buttons on the tab part of a tabwidget

  1. #1
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Adding buttons on the tab part of a tabwidget

    I was wondering if anyone has any idea how I should go about adding an "X" button onto certain tabs of my tabWidget. I am not talking about adding a button onto the body of the tabwidget. I'm talking about actually adding a clickable image inside the tab.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding buttons on the tab part of a tabwidget

    Subclass QTabBar and reimplement its mousePressEvent. Check the click coordinates there and act if it happened inside the "button". You might also want to reimplement other events to make the button appear "clickable".

  3. #3
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding buttons on the tab part of a tabwidget

    Wysota, thanks for the tip! I got the basics of it working perfect. I figured I would show part of my code just incase anyone needs it in the future. Please let me know if you see any potential problems with it:

    Qt Code:
    1. class TabWidget : public QTabWidget
    2. {
    3. public:
    4. TabWidget(Widget *parent=0);
    5. }
    6. class Tab : public QTabBar
    7. {
    8. Q_OBJECT
    9. public:
    10. Tab(QWidget *parent=0);
    11. protected:
    12. void paintEvent(QPaintEvent *event);
    13. QSize tabSizeHint(int index) const;
    14. void mousePressEvent(QMouseEvent *event);
    15. private:
    16. QWidget *pParent;
    17. TabWidget *tabWidget;
    18. }
    19.  
    20. Tab::Tab(QWidget *parent) : QTabBar(parent)
    21. {
    22. pParent = parent;
    23. tabWidget = (TabWidget*) pParent;
    24. }
    25.  
    26. void Tab::paintEvent(QPaintEvent *event)
    27. {
    28. QTabBar::paintEvent(event);
    29. QPainter p(this);
    30. QRect r;
    31. int count = this->count() -1;
    32. for (int i=0; i< count; i++)
    33. {
    34. r = this->tabRect(i);
    35. p.drawPixmap(r.right()-22, r.top()+5, QPixmap(":/icons/x.png");
    36. }
    37. r = this->tabRect(count);
    38. p.drawPixmap(r.left()+4, r.top()+5, QPixmap(":/icons/add.png"));
    39. }
    40.  
    41. void Tab::tabSizeHint(int index) const
    42. {
    43. QSize size = QTabBar::tabSizeHint(index);
    44. if (tabWidget->tabText(index) == "")
    45. size.setWidth(size.width() + 2);
    46. else
    47. size.setWidth(size.width() + 32);
    48. return size;
    49. }
    50.  
    51. void Tab::mousePressEvent(QMouseEvent *event)
    52. {
    53. QRect r;
    54. for (int i=0; i < this->count()-1; i++)
    55. {
    56. r = this->tabRect(i);
    57. if ((event->x() >= r.right()-22) && (event->x() <= r.right()-8))
    58. {
    59. ...
    60. tabWidget->removeTab(i);
    61. return;
    62. ...
    63. }
    64. }
    65. QTabBar::mousePressEvent(event);
    66. }
    67.  
    68. TabWidget::TabWidget(QWidget *Parent) : QTabWidget(parent)
    69. {
    70. Tab *bar = new Tab(this);
    71. setTabBar(bar);
    72. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!
    Last edited by forrestfsu; 20th December 2006 at 18:54.

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

    pawer (1st August 2007)

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.