Results 1 to 7 of 7

Thread: QTabWidget Corner Widget Problem

  1. #1
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QTabWidget Corner Widget Problem

    Hi,
    I have qtabwidget with corner widget. I added QToolButton as corner widget.

    Qt Code:
    1. btnMenu = new QToolButton(this);
    2. btnMenu->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    3. btnMenu->setAutoRaise(true);
    4. btnMenu->setIcon(QIcon::fromTheme("tab-new"));
    5. btnMenu->setText(trUtf8("Pars"));
    6. btnMenu->setShortcut(QKeySequence::AddTab);
    7. btnMenu->setMenu(menu);
    8. btnMenu->setPopupMode(QToolButton::MenuButtonPopup);
    9. setCornerWidget(btnMenu,Qt::TopLeftCorner);
    To copy to clipboard, switch view to plain text mode 

    And I have "mouseRelaseEvent" for tabwidget like this:


    Qt Code:
    1. void PTabWidget::mouseReleaseEvent(QMouseEvent *event)
    2. {
    3. if (event->button() == Qt::MidButton)
    4. {
    5. int index = tabBar()->tabAt(event->pos());
    6. if (index != -1)
    7. {
    8. this->removeTab(index);
    9. qDebug() << QString::number(index);
    10. return;
    11. }
    12. else
    13. {
    14. return;
    15. }
    16. }
    17.  
    18. PTabWidget::mouseReleaseEvent(event);
    19. }
    To copy to clipboard, switch view to plain text mode 

    When I click with mid-button to tabbar, the output is "The program has unexpectedly finished.". But when i remove the tool button, it works fine. Is it bug or I miss something ??

    Sorry for my poor English.
    Best regards.

  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: QTabWidget Corner Widget Problem

    One thing that's certainly invalid is that the event position is calculated relative to the tab widget and you are calling QTabBar::tabAt() which expects a position relative to the tab bar. I understand that you wish to remove a tab when you mid-click on it. If so, convert the coordinates to tab-bar's space and use deleteLater() instead of removeTab().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    isamert (11th February 2011)

  4. #3
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTabWidget Corner Widget Problem

    Yes, I want to remove a tab with mid-click. But how can I convert the coordinates to tab-bar's space? What do you mean with "tab-bar's space" ? I dont understand exactly.
    Thanks for your reply.

  5. #4
    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: QTabWidget Corner Widget Problem

    See QWidget::mapTo family of methods.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTabWidget Corner Widget Problem

    I found diffirent way. I calculate point with buttons width. (My English is not enough for that ).

    Qt Code:
    1. QPoint npoint;
    2. npoint.setX(event->pos().x() - btnMenu->width());
    3. npoint.setY(event->pos().y());
    4. int index = tabBar()->tabAt(npoint);
    To copy to clipboard, switch view to plain text mode 
    New code:
    Qt Code:
    1. void PTabWidget::mouseReleaseEvent(QMouseEvent *event)
    2. {
    3. if (event->button() == Qt::MidButton)
    4. {
    5. QPoint npoint;
    6. npoint.setX(event->pos().x() - btnMenu->width());
    7. npoint.setY(event->pos().y());
    8. int index = tabBar()->tabAt(npoint);
    9. if (index != -1)
    10. {
    11. this->widget(index)->deleteLater();
    12. qDebug() << QString::number(index);
    13. return;
    14. }
    15. else
    16. {
    17. return;
    18. }
    19. }
    20.  
    21. PTabWidget::mouseReleaseEvent(event);
    22. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    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: QTabWidget Corner Widget Problem

    That's not a good approach, you are not taking margins and spacing into consideration. It will fail if you click near a border of a tab. Use QWidget::mapTo() like this:
    Qt Code:
    1. QPoint tabBarPoint = mapTo(tabBar()), event->pos());
    2. int index = tabBar()->tabAt(tabBarPoint);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTabWidget Corner Widget Problem

    Maybe I am not seeing this correctly, but no matter what you do with the middle mouse button, doesn't this code set up an infinite loop if you click with anything except a middle button?

    Qt Code:
    1. void PTabWidget::mouseReleaseEvent(QMouseEvent *event)
    2. {
    3. if (event->button() == Qt::MidButton)
    4. {
    5. // ...
    6. return;
    7. }
    8.  
    9. PTabWidget::mouseReleaseEvent(event); // <<<<
    10. }
    To copy to clipboard, switch view to plain text mode 

    Maybe the OP is lucky and the tab widget handles the click with mousePressEvent so the code never handles that case.

Similar Threads

  1. How to Disabel close button on top corner of widget
    By jsmith in forum Qt Programming
    Replies: 4
    Last Post: 2nd August 2010, 16:57
  2. Replies: 10
    Last Post: 29th May 2010, 18:42
  3. QTabWidget: corner widget is not shown
    By dv_ in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2008, 17:30
  4. Corner widget in QTabWidget doesn't show up
    By ePharaoh in forum Qt Programming
    Replies: 2
    Last Post: 6th April 2006, 17:02
  5. [qt4] QScrollArea corner widget?
    By sertrem in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2006, 21:42

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.