QTabWidget Corner Widget Problem
Hi,
I have qtabwidget with corner widget. I added QToolButton as corner widget.
Code:
btnMenu->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
btnMenu->setAutoRaise(true);
btnMenu
->setIcon
(QIcon::fromTheme("tab-new"));
btnMenu->setText(trUtf8("Pars"));
btnMenu->setMenu(menu);
setCornerWidget(btnMenu,Qt::TopLeftCorner);
And I have "mouseRelaseEvent" for tabwidget like this:
Code:
{
if (event->button() == Qt::MidButton)
{
int index = tabBar()->tabAt(event->pos());
if (index != -1)
{
this->removeTab(index);
qDebug
() <<
QString::number(index
);
return;
}
else
{
return;
}
}
PTabWidget::mouseReleaseEvent(event);
}
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.
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().
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.
Re: QTabWidget Corner Widget Problem
See QWidget::mapTo family of methods.
Re: QTabWidget Corner Widget Problem
I found diffirent way. I calculate point with buttons width. (My English is not enough for that :) ).
Code:
npoint.setX(event->pos().x() - btnMenu->width());
npoint.setY(event->pos().y());
int index = tabBar()->tabAt(npoint);
New code:
Code:
{
if (event->button() == Qt::MidButton)
{
npoint.setX(event->pos().x() - btnMenu->width());
npoint.setY(event->pos().y());
int index = tabBar()->tabAt(npoint);
if (index != -1)
{
this->widget(index)->deleteLater();
qDebug
() <<
QString::number(index
);
return;
}
else
{
return;
}
}
PTabWidget::mouseReleaseEvent(event);
}
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:
Code:
QPoint tabBarPoint
= mapTo
(tabBar
()), event
->pos
());
int index = tabBar()->tabAt(tabBarPoint);
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?
Code:
{
if (event->button() == Qt::MidButton)
{
// ...
return;
}
PTabWidget::mouseReleaseEvent(event); // <<<<
}
Maybe the OP is lucky and the tab widget handles the click with mousePressEvent so the code never handles that case.